Skip to content

Commit d2150d9

Browse files
committed
benchmarks cache fixtures
1 parent 6bddb2e commit d2150d9

1 file changed

Lines changed: 15 additions & 10 deletions

File tree

bench/util.py

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
11
# SPDX-License-Identifier: (Apache-2.0 OR MIT)
22

33
import os
4-
import json
4+
import orjson
55
import lzma
66

7-
87
dirname = os.path.join(os.path.dirname(__file__), '../data')
98

9+
STR_CACHE = {}
1010

11-
def read_fixture_str(filename):
12-
if filename.endswith('.xz'):
13-
with lzma.open(os.path.join(dirname, filename), 'r') as fileh:
14-
return fileh.read().decode('utf-8')
15-
else:
16-
with open(os.path.join(dirname, filename), 'r') as fileh:
17-
return fileh.read().decode('utf-8')
11+
OBJ_CACHE = {}
1812

13+
def read_fixture_str(filename):
14+
if not filename in STR_CACHE:
15+
if filename.endswith('.xz'):
16+
with lzma.open(os.path.join(dirname, filename), 'r') as fileh:
17+
STR_CACHE[filename] = fileh.read().decode('utf-8')
18+
else:
19+
with open(os.path.join(dirname, filename), 'r') as fileh:
20+
STR_CACHE[filename] = fileh.read().decode('utf-8')
21+
return STR_CACHE[filename]
1922

2023
def read_fixture_obj(filename):
21-
return json.loads(read_fixture_str(filename))
24+
if not filename in OBJ_CACHE:
25+
OBJ_CACHE[filename] = orjson.loads(read_fixture_str(filename))
26+
return OBJ_CACHE[filename]

0 commit comments

Comments
 (0)