e4c9116708119629e3527c8c4f7886c5c5fae37d
[openjpeg.git] / tests / performance / perf_test.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Copyright (c) 2017, IntoPIX SA
5 # Contact: support@intopix.com
6 # Author: Even Rouault
7 #
8 # Redistribution and use in source and binary forms, with or without
9 # modification, are permitted provided that the following conditions
10 # are met:
11 # 1. Redistributions of source code must retain the above copyright
12 #    notice, this list of conditions and the following disclaimer.
13 # 2. Redistributions in binary form must reproduce the above copyright
14 #    notice, this list of conditions and the following disclaimer in the
15 #    documentation and/or other materials provided with the distribution.
16 #
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
18 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 # ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
28 #
29
30 import os
31 import subprocess
32 import sys
33 import time
34
35
36 def Usage():
37     print('Usage: perf_test.py [-kakadu] [-i filelist.csv] [-o out.csv] [-q]')
38     sys.exit(1)
39
40 opj_decompress_path = 'opj_decompress'
41 opj_compress_path = 'opj_compress'
42 kdu_expand_path = 'kdu_expand'
43 kdu_compress_path = 'kdu_compress'
44
45 in_filename = 'perf_test_filelist.csv'
46 out_filename = None
47 i = 1
48 quiet = False
49 kakadu = False
50 while i < len(sys.argv):
51     if sys.argv[i] == '-o' and i + 1 < len(sys.argv):
52         i += 1
53         out_filename = sys.argv[i]
54     elif sys.argv[i] == '-q':
55         quiet = True
56     elif sys.argv[i] == '-kakadu':
57         kakadu = True
58     else:
59         Usage()
60     i += 1
61
62 i = 0
63 while i < 10 * 1024 * 1024:
64     i += 1
65
66 out_file = None
67 if out_filename is not None:
68     out_file = open(out_filename, 'wt')
69     out_file.write('filename,iterations,threads,command,comment,time_ms\n')
70
71 total_time = 0
72 for line in open(in_filename, 'rt').readlines()[1:]:
73     line = line.replace('\n', '')
74     filename, num_iterations, num_threads, command, comment = line.split(',')
75     num_threads = int(num_threads)
76     num_iterations = int(num_iterations)
77     start = time.time()
78     for i in range(num_iterations):
79         env = None
80         if kakadu:
81             if command == 'DECOMPRESS':
82                 args = [kdu_expand_path,
83                         '-i', filename,
84                         '-num_threads', str(num_threads),
85                         '-o', 'out_perf_test.pgm']
86             elif command == 'COMPRESS':
87                 args = [kdu_compress_path,
88                         '-i', filename,
89                         '-num_threads', str(num_threads),
90                         'Creversible=yes',
91                         '-o', 'out_perf_test.jp2']
92             else:
93                 assert False, command
94         else:
95             env = os.environ
96             if num_threads > 1:
97                 env['OPJ_NUM_THREADS'] = str(num_threads)
98             else:
99                 env['OPJ_NUM_THREADS'] = '0'
100             if command == 'DECOMPRESS':
101                 args = [opj_decompress_path,
102                         '-i', filename, '-o', 'out_perf_test.pgm']
103             elif command == 'COMPRESS':
104                 args = [opj_compress_path,
105                         '-i', filename, '-o', 'out_perf_test.jp2']
106             else:
107                 assert False, command
108         p = subprocess.Popen(args,
109                              stdout=subprocess.PIPE,
110                              stderr=subprocess.PIPE,
111                              close_fds=True,
112                              env=env)
113         p.wait()
114     stop = time.time()
115     if os.path.exists('out_perf_test.pgm'):
116         os.unlink('out_perf_test.pgm')
117     if os.path.exists('out_perf_test.jp2'):
118         os.unlink('out_perf_test.jp2')
119     spent_time = stop - start
120     total_time += spent_time
121     if not quiet:
122         if len(comment) != 0:
123             print('%s (%s), %d iterations, %d threads, %s: %.02f s' %
124                   (filename, comment, num_iterations, num_threads,
125                    command, spent_time))
126         else:
127             print('%s, %d iterations, %d threads, %s: %.02f s' %
128                   (filename, num_iterations, num_threads, command, spent_time))
129     if out_file is not None:
130         out_file.write('%s,%d,%d,%s,%s,%d\n' %
131                        (filename, num_iterations, num_threads, command,
132                         comment, spent_time * 1000))
133
134 if not quiet:
135     print('Total time: %.02f s' % total_time)
136 if out_file is not None:
137     out_file.write('%s,,,,,%d\n' % ('TOTAL', total_time * 1000))