Skip to content

Commit

Permalink
Add tests for benchmark utility
Browse files Browse the repository at this point in the history
Signed-off-by: Rodrigo Tobar <rtobar@icrar.org>
  • Loading branch information
rtobar committed Nov 30, 2023
1 parent 9cd01e2 commit f2f45e3
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@ source = ijson
[report]
exclude_lines =
raise ImportError\('no backends available'\)
if stdout.isatty():
65 changes: 65 additions & 0 deletions test/test_benchmark.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import os
import subprocess
import sys
import unittest
import tempfile

from test.test_base import JSON


class BenchmarkTests(unittest.TestCase):

def _do_test_benchmark(self, method="basic_parse", backend="python", multiple_values=True, extra_args=None):
# Use python backend to ensure multiple_values works
if multiple_values:
env = dict(os.environ)
env['IJSON_BACKEND'] = 'python'
# Ensure printing works on the subprocess in Windows
# by using utf-8 on its stdout
if sys.platform == 'win32':
env = dict(os.environ)
env['PYTHONIOENCODING'] = 'utf-8'
cmd = [sys.executable, '-m', 'ijson.benchmark', '-m', method, '-p', '', '-s', '1']
if multiple_values:
cmd.append('-M')
if extra_args:
cmd += extra_args
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
out, err = proc.communicate()
status = proc.wait()
self.assertEqual(0, status, "out:\n%s\nerr:%s" % (out.decode('utf-8'), err.decode('utf-8')))

def _test_benchmark(self, method):
self._do_test_benchmark(method, "python")
self._do_test_benchmark(method, "python", extra_args=['-c'])
self._do_test_benchmark(method, "python", extra_args=['-a'])

def test_basic_parse(self):
self._test_benchmark('basic_parse')

def test_parse(self):
self._test_benchmark('parse')

def test_kvitems(self):
self._test_benchmark('kvitems')

def test_items(self):
self._test_benchmark('items')

def test_list(self):
self._do_test_benchmark(extra_args=['-l'])

def test_more_iterations(self):
self._do_test_benchmark(extra_args=['-I', '1'])
self._do_test_benchmark(extra_args=['-I', '2'])
self._do_test_benchmark(extra_args=['-I', '3'])

def test_input_files(self):
fd, fname = tempfile.mkstemp()
os.write(fd, JSON)
os.close(fd)
try:
self._do_test_benchmark(extra_args=[fname])
self._do_test_benchmark(extra_args=[fname, fname])
finally:
os.unlink(fname)

0 comments on commit f2f45e3

Please sign in to comment.