More bench tweaks.
[libdcp.git] / test / bench.cc
1 /*
2     Copyright (C) 2015 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include "data.h"
21 #include "test.h"
22 #include "util.h"
23 #include "version.h"
24 #include <sys/time.h>
25 #include <iostream>
26
27 using std::cout;
28 using std::cerr;
29 using boost::shared_ptr;
30
31 /** Run some basic benchmarks of JPEG2000 encoding / decoding */
32 int
33 main (int argc, char* argv[])
34 {
35         if (argc < 2) {
36                 cerr << "Syntax: " << argv[0] << " private-test-path\n";
37                 exit (EXIT_FAILURE);
38         }
39
40         int const decompress_count = 100;
41         int const compress_count = 100;
42         int const j2k_bandwidth = 100000000;
43
44         struct timeval start;
45         dcp::Data j2k (boost::filesystem::path (argv[1]) / "thx.j2c");
46
47         gettimeofday (&start, 0);
48
49         shared_ptr<dcp::OpenJPEGImage> xyz;
50         for (int i = 0; i < decompress_count; ++i) {
51                 xyz = dcp::decompress_j2k (j2k, 0);
52                 cout << (i + 1) << " ";
53                 cout.flush ();
54         }
55         cout << "\n";
56
57         struct timeval stop;
58         gettimeofday (&stop, 0);
59
60         double start_seconds = start.tv_sec + double(start.tv_usec) / 1000000;
61         double stop_seconds = stop.tv_sec + double(stop.tv_usec) / 1000000;
62         if (dcp::built_with_debug) {
63                 cout << "Decompress (debug build): ";
64         } else {
65                 cout << "Decompress: ";
66         }
67         cout << decompress_count / (stop_seconds - start_seconds) << " fps.\n";
68
69         gettimeofday (&start, 0);
70
71         for (int i = 0; i < compress_count; ++i) {
72                 dcp::compress_j2k (xyz, j2k_bandwidth, 24, false, false);
73                 cout << (i + 1) << " ";
74                 cout.flush ();
75         }
76         cout << "\n";
77
78         gettimeofday (&stop, 0);
79
80         start_seconds = start.tv_sec + double(start.tv_usec) / 1000000;
81         stop_seconds = stop.tv_sec + double(stop.tv_usec) / 1000000;
82         if (dcp::built_with_debug) {
83                 cout << "Compress (debug build) ";
84         } else {
85                 cout << "Compress ";
86         }
87
88         cout << (j2k_bandwidth / 1000000) << "Mbps: "
89              << compress_count / (stop_seconds - start_seconds) << " fps.\n";
90 }