1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| import zlib import pytest import zstandard as zstd
@pytest.fixture def example_data(): s = '{"PMCH31_02":6.7,"PMCH31_01":9.6,"PMCH31_04":3.4,"PMCH31_03":4.6,"PMCH31_06":3.4,"PMCH31_05":3.5,"PMCH31_08":3.5,"PMCH31_07":3.7,"pm100":103.80000000000001,"PMCH31_09":3.5,"PMCH31_11":4.5,"ch16_20":20.5,"lat":0,"PMCH31_10":4.3,"sat_num_pos":0,"pm4_f":84.2,"lng":0,"pm25":68,"PMCH31_24":1.7,"PMCH31_23":3.1,"PMCH31_26":0,"PMCH31_25":0,"PMCH31_28":0,"PMCH31_27":0,"gps_status":1,"PMCH31_29":0,"speed":0,"SN":"B003-02BD","sn":"B003-02BD","PMCH31_31":0,"PMCH31_30":0,"direction":0,"PMCH31_13":6,"PMCH31_12":5,"PMCH31_15":6.1,"PMCH31_14":6.3,"PMCH31_17":5.4,"PMCH31_16":4.7,"PMCH31_19":3.2,"PMCH31_18":4.5,"pm1_f":41.9,"pm10":99.00000000000001,"uploadTime":["2022-11-11 10:54:43.278"],"sensors":[{"subno":1,"DC":555},{"subno":2,"DC":674},{"subno":3,"DC":516},{"subno":4,"DC":610}],"pm03":9.6,"tm":1668135283278,"time":"2022-11-11 10:54:43.278","PMCH31_20":2.7,"PMCH31_22":1.7,"PMCH31_21":2.7,"DC":0}' s = s.encode() return s
@pytest.mark.benchmark(group="compress") def test_compress_zlib(benchmark, example_data): result = benchmark(zlib.compress, example_data) print( f"zlib compress len: {len(result)} compress rate: {len(result) / len(example_data) * 100:.2f}" )
@pytest.mark.benchmark(group="compress") def test_compress_zstd(benchmark, example_data): result = benchmark(zstd.compress, example_data) print( f"zstd compress len: {len(result)} compress rate: {len(result) / len(example_data) * 100:.2f}" )
@pytest.mark.benchmark(group="decompress") def test_decompress_zlib(benchmark, example_data): compressed_data = zlib.compress(example_data) result = benchmark(zlib.decompress, compressed_data) assert result == example_data
@pytest.mark.benchmark(group="decompress") def test_decompress_zstd(benchmark, example_data): compressed_data = zstd.compress(example_data) result = benchmark(zstd.decompress, compressed_data) assert result == example_data
|