Merge pull request #1380 from rouault/fix_verify_indent_pr
[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] == '-i' and i + 1 < len(sys.argv):
55         i += 1
56         in_filename = sys.argv[i]
57     elif sys.argv[i] == '-q':
58         quiet = True
59     elif sys.argv[i] == '-kakadu':
60         kakadu = True
61     else:
62         Usage()
63     i += 1
64
65 i = 0
66 while i < 10 * 1024 * 1024:
67     i += 1
68
69 out_file = None
70 if out_filename is not None:
71     out_file = open(out_filename, 'wt')
72     out_file.write('filename,iterations,threads,command,comment,time_ms\n')
73
74 total_time = 0
75 for line in open(in_filename, 'rt').readlines()[1:]:
76     line = line.replace('\n', '')
77     filename, num_iterations, num_threads, command, comment = line.split(',')
78     num_threads = int(num_threads)
79     num_iterations = int(num_iterations)
80     start = time.time()
81     for i in range(num_iterations):
82         env = None
83         if kakadu:
84             if command == 'DECOMPRESS':
85                 args = [kdu_expand_path,
86                         '-i', filename,
87                         '-num_threads', str(num_threads),
88                         '-o', 'out_perf_test.pgm']
89             elif command == 'COMPRESS':
90                 args = [kdu_compress_path,
91                         '-i', filename,
92                         '-num_threads', str(num_threads),
93                         'Creversible=yes',
94                         '-o', 'out_perf_test.jp2']
95             else:
96                 assert False, command
97         else:
98             env = os.environ
99             if num_threads > 1:
100                 env['OPJ_NUM_THREADS'] = str(num_threads)
101             else:
102                 env['OPJ_NUM_THREADS'] = '0'
103             if command == 'DECOMPRESS':
104                 args = [opj_decompress_path,
105                         '-i', filename, '-o', 'out_perf_test.pgm']
106             elif command == 'COMPRESS':
107                 args = [opj_compress_path,
108                         '-i', filename, '-o', 'out_perf_test.jp2']
109             else:
110                 assert False, command
111         p = subprocess.Popen(args,
112                              stdout=subprocess.PIPE,
113                              stderr=subprocess.PIPE,
114                              close_fds=True,
115                              env=env)
116         p.wait()
117     stop = time.time()
118     if os.path.exists('out_perf_test.pgm'):
119         os.unlink('out_perf_test.pgm')
120     if os.path.exists('out_perf_test.jp2'):
121         os.unlink('out_perf_test.jp2')
122     spent_time = stop - start
123     total_time += spent_time
124     if not quiet:
125         if len(comment) != 0:
126             print('%s (%s), %d iterations, %d threads, %s: %.02f s' %
127                   (filename, comment, num_iterations, num_threads,
128                    command, spent_time))
129         else:
130             print('%s, %d iterations, %d threads, %s: %.02f s' %
131                   (filename, num_iterations, num_threads, command, spent_time))
132     if out_file is not None:
133         out_file.write('%s,%d,%d,%s,%s,%d\n' %
134                        (filename, num_iterations, num_threads, command,
135                         comment, spent_time * 1000))
136
137 if not quiet:
138     print('Total time: %.02f s' % total_time)
139 if out_file is not None:
140     out_file.write('%s,,,,,%d\n' % ('TOTAL', total_time * 1000))