def test_performance(basename, prefix): # define prefixes for indexing idx = [prefix] re_idx = [] fname_in = basename + ".json" fname_out = basename + "_min.json" json_in = io.BufferedReader(open(fname_in, "rb"), 128 * 1024) #128kB of read buffer json_min = io.BufferedWriter(open(fname_out, "wb+"), 128 * 1024) #128kB of write buffer json_index = open(basename + "_idx.json", "w") before = time.time() index = buildJSONindex(json_in, json_min, idx, re_idx) after = time.time() json.dump(index, json_index, indent=4) json_index.close() json_min.close() json_in.close() #print some statistics size_in = os.stat(fname_in).st_size size_out = os.stat(fname_out).st_size print("Original file size: %d bytes" % size_in) print("Minified file size: %d bytes (%d%%)" % (size_out, round(100 * (size_out - size_in) / size_in, 0))) print("Elapsed time: %.3f ms" % (1000 * (after - before)))
def test_build_index_regexp(self): self.reset() re_idx = [ "geometry\.[^\.]+" ] #match only 'geometry.coordinates' and not 'geometry.item*' index = buildJSONindex(self._in, self._out, regexp_prefixes=re_idx) self.assertDictEqual(index, {"geometry.coordinates": (216, 431)})
def test_build_index_all(self): _in = io.BytesIO(b"{ }") _out = io.BytesIO() idx = [""] index = buildJSONindex(_in, _out, idx) self.assertDictEqual(index, {"": (0, 2)}) self.assertEqual(b"{}", _out.getvalue())
def test_minify_json(self): self.reset() idx = [] re_idx = [] index = buildJSONindex(self._in, self._out, idx, re_idx) self._out.close() f1 = open("test.json", "r") json_orig = json.load(f1) f1.close() f2 = open("test_out.json", "r") json_minified = json.load(f2) f2.close() self.assertDictEqual(json_orig, json_minified)
def test_sub_json(self): self.reset() idx = ["geometry.coordinates"] index = buildJSONindex(self._in, self._out, idx) self._out.close() self._in.seek(0) json_in = json.load(self._in) #load a sub-JSON start = index["geometry.coordinates"][0] end = index["geometry.coordinates"][1] self._out = open("test_out.json", "rb") sub_stream = SubStream(self._out, start, end) sub_json = json.load(sub_stream) self._out.close() self.assertCountEqual(json_in["geometry"]["coordinates"], sub_json)
def test_build_index_static(self): self.reset() idx = ["geometry.coordinates"] index = buildJSONindex(self._in, self._out, idx) self.assertDictEqual(index, {"geometry.coordinates": (216, 431)})