perf_test.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. import os
  30. import subprocess
  31. import sys
  32. import time
  33. def Usage():
  34. print('Usage: perf_test.py [-kakadu] [-i filelist.csv] [-o out.csv] [-q]')
  35. sys.exit(1)
  36. opj_decompress_path = 'opj_decompress'
  37. opj_compress_path = 'opj_compress'
  38. kdu_expand_path = 'kdu_expand'
  39. kdu_compress_path = 'kdu_compress'
  40. in_filename = 'perf_test_filelist.csv'
  41. out_filename = None
  42. i = 1
  43. quiet = False
  44. kakadu = False
  45. while i < len(sys.argv):
  46. if sys.argv[i] == '-o' and i + 1 < len(sys.argv):
  47. i += 1
  48. out_filename = sys.argv[i]
  49. elif sys.argv[i] == '-i' and i + 1 < len(sys.argv):
  50. i += 1
  51. in_filename = sys.argv[i]
  52. elif sys.argv[i] == '-q':
  53. quiet = True
  54. elif sys.argv[i] == '-kakadu':
  55. kakadu = True
  56. else:
  57. Usage()
  58. i += 1
  59. i = 0
  60. while i < 10 * 1024 * 1024:
  61. i += 1
  62. out_file = None
  63. if out_filename is not None:
  64. out_file = open(out_filename, 'wt')
  65. out_file.write('filename,iterations,threads,command,comment,time_ms\n')
  66. total_time = 0
  67. for line in open(in_filename, 'rt').readlines()[1:]:
  68. line = line.replace('\n', '')
  69. filename, num_iterations, num_threads, command, comment = line.split(',')
  70. num_threads = int(num_threads)
  71. num_iterations = int(num_iterations)
  72. start = time.time()
  73. for i in range(num_iterations):
  74. env = None
  75. if kakadu:
  76. if command == 'DECOMPRESS':
  77. args = [kdu_expand_path,
  78. '-i', filename,
  79. '-num_threads', str(num_threads),
  80. '-o', 'out_perf_test.pgm']
  81. elif command == 'COMPRESS':
  82. args = [kdu_compress_path,
  83. '-i', filename,
  84. '-num_threads', str(num_threads),
  85. 'Creversible=yes',
  86. '-o', 'out_perf_test.jp2']
  87. else:
  88. assert False, command
  89. else:
  90. env = os.environ
  91. if num_threads > 1:
  92. env['OPJ_NUM_THREADS'] = str(num_threads)
  93. else:
  94. env['OPJ_NUM_THREADS'] = '0'
  95. if command == 'DECOMPRESS':
  96. args = [opj_decompress_path,
  97. '-i', filename, '-o', 'out_perf_test.pgm']
  98. elif command == 'COMPRESS':
  99. args = [opj_compress_path,
  100. '-i', filename, '-o', 'out_perf_test.jp2']
  101. else:
  102. assert False, command
  103. p = subprocess.Popen(args,
  104. stdout=subprocess.PIPE,
  105. stderr=subprocess.PIPE,
  106. close_fds=True,
  107. env=env)
  108. p.wait()
  109. stop = time.time()
  110. if os.path.exists('out_perf_test.pgm'):
  111. os.unlink('out_perf_test.pgm')
  112. if os.path.exists('out_perf_test.jp2'):
  113. os.unlink('out_perf_test.jp2')
  114. spent_time = stop - start
  115. total_time += spent_time
  116. if not quiet:
  117. if len(comment) != 0:
  118. print('%s (%s), %d iterations, %d threads, %s: %.02f s' %
  119. (filename, comment, num_iterations, num_threads,
  120. command, spent_time))
  121. else:
  122. print('%s, %d iterations, %d threads, %s: %.02f s' %
  123. (filename, num_iterations, num_threads, command, spent_time))
  124. if out_file is not None:
  125. out_file.write('%s,%d,%d,%s,%s,%d\n' %
  126. (filename, num_iterations, num_threads, command,
  127. comment, spent_time * 1000))
  128. if not quiet:
  129. print('Total time: %.02f s' % total_time)
  130. if out_file is not None:
  131. out_file.write('%s,,,,,%d\n' % ('TOTAL', total_time * 1000))