Fixes for the bad distros.
[libdcp.git] / test / bench.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This file is part of libdcp.
5
6     libdcp is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     libdcp is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with libdcp.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "data.h"
21 #include "test.h"
22 #include "util.h"
23 #include "version.h"
24 #include "j2k.h"
25 #include "openjpeg_image.h"
26 #include <sys/time.h>
27 #include <iostream>
28 #include <cstdio>
29
30 using std::cout;
31 using std::cerr;
32 using boost::shared_ptr;
33
34 class Timer
35 {
36 public:
37         Timer ()
38                 : _total (0)
39         {
40
41         }
42
43         void start ()
44         {
45                 gettimeofday (&_start, 0);
46         }
47
48         void stop ()
49         {
50                 struct timeval stop;
51                 gettimeofday (&stop, 0);
52                 _total += (stop.tv_sec + stop.tv_usec / 1e6) - (_start.tv_sec + _start.tv_usec / 1e6);
53         }
54
55         double get ()
56         {
57                 return _total;
58         }
59
60 private:
61         double _total;
62         struct timeval _start;
63 };
64
65 /** Run some basic benchmarks of JPEG2000 encoding / decoding */
66 int
67 main (int argc, char* argv[])
68 {
69         if (argc < 2) {
70                 cerr << "Syntax: " << argv[0] << " private-test-path\n";
71                 exit (EXIT_FAILURE);
72         }
73
74         int const count = 100;
75         int const j2k_bandwidth = 100000000;
76
77         dcp::Data j2k (boost::filesystem::path (argv[1]) / "thx.j2c");
78
79         Timer decompress;
80         Timer compress;
81
82         dcp::Data recomp;
83         for (int i = 0; i < count; ++i) {
84                 decompress.start ();
85                 shared_ptr<dcp::OpenJPEGImage> xyz = dcp::decompress_j2k (j2k, 0);
86                 decompress.stop ();
87                 compress.start ();
88                 recomp = dcp::compress_j2k (xyz, j2k_bandwidth, 24, false, false);
89                 compress.stop ();
90                 cout << (i + 1) << " ";
91                 cout.flush ();
92         }
93         cout << "\n";
94
95         cout << "Decompress: " << count / decompress.get() << " fps.\n";
96         cout << "Compress:   " << count / compress.get() << " fps.\n";
97
98         FILE* f = fopen ("check.j2c", "wb");
99         fwrite (recomp.data().get(), 1, recomp.size(), f);
100         fclose (f);
101 }