Skip to content

Commit f2f45e3

Browse files
committed
Add tests for benchmark utility
Signed-off-by: Rodrigo Tobar <[email protected]>
1 parent 9cd01e2 commit f2f45e3

File tree

2 files changed

+66
-0
lines changed

2 files changed

+66
-0
lines changed

.coveragerc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ source = ijson
3030
[report]
3131
exclude_lines =
3232
raise ImportError\('no backends available'\)
33+
if stdout.isatty():

test/test_benchmark.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import os
2+
import subprocess
3+
import sys
4+
import unittest
5+
import tempfile
6+
7+
from test.test_base import JSON
8+
9+
10+
class BenchmarkTests(unittest.TestCase):
11+
12+
def _do_test_benchmark(self, method="basic_parse", backend="python", multiple_values=True, extra_args=None):
13+
# Use python backend to ensure multiple_values works
14+
if multiple_values:
15+
env = dict(os.environ)
16+
env['IJSON_BACKEND'] = 'python'
17+
# Ensure printing works on the subprocess in Windows
18+
# by using utf-8 on its stdout
19+
if sys.platform == 'win32':
20+
env = dict(os.environ)
21+
env['PYTHONIOENCODING'] = 'utf-8'
22+
cmd = [sys.executable, '-m', 'ijson.benchmark', '-m', method, '-p', '', '-s', '1']
23+
if multiple_values:
24+
cmd.append('-M')
25+
if extra_args:
26+
cmd += extra_args
27+
proc = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
28+
out, err = proc.communicate()
29+
status = proc.wait()
30+
self.assertEqual(0, status, "out:\n%s\nerr:%s" % (out.decode('utf-8'), err.decode('utf-8')))
31+
32+
def _test_benchmark(self, method):
33+
self._do_test_benchmark(method, "python")
34+
self._do_test_benchmark(method, "python", extra_args=['-c'])
35+
self._do_test_benchmark(method, "python", extra_args=['-a'])
36+
37+
def test_basic_parse(self):
38+
self._test_benchmark('basic_parse')
39+
40+
def test_parse(self):
41+
self._test_benchmark('parse')
42+
43+
def test_kvitems(self):
44+
self._test_benchmark('kvitems')
45+
46+
def test_items(self):
47+
self._test_benchmark('items')
48+
49+
def test_list(self):
50+
self._do_test_benchmark(extra_args=['-l'])
51+
52+
def test_more_iterations(self):
53+
self._do_test_benchmark(extra_args=['-I', '1'])
54+
self._do_test_benchmark(extra_args=['-I', '2'])
55+
self._do_test_benchmark(extra_args=['-I', '3'])
56+
57+
def test_input_files(self):
58+
fd, fname = tempfile.mkstemp()
59+
os.write(fd, JSON)
60+
os.close(fd)
61+
try:
62+
self._do_test_benchmark(extra_args=[fname])
63+
self._do_test_benchmark(extra_args=[fname, fname])
64+
finally:
65+
os.unlink(fname)

0 commit comments

Comments
 (0)