comparison contrib/python-zstandard/tests/test_data_structures.py @ 30924:c32454d69b85

zstd: vendor python-zstandard 0.7.0 Commit 3054ae3a66112970a091d3939fee32c2d0c1a23e from https://github.com/indygreg/python-zstandard is imported without modifications (other than removing unwanted files). The vendored zstd library within has been upgraded from 1.1.2 to 1.1.3. This version introduced new APIs for threads, thread pools, multi-threaded compression, and a new dictionary builder (COVER). These features are not yet used by python-zstandard (or Mercurial for that matter). However, that will likely change in the next python-zstandard release (and I think there are opportunities for Mercurial to take advantage of the multi-threaded APIs). Relevant to Mercurial, the CFFI bindings are now fully implemented. This means zstd should "just work" with PyPy (although I haven't tried). The python-zstandard test suite also runs all tests against both the C extension and CFFI bindings to ensure feature parity. There is also a "decompress_content_dict_chain()" API. This was derived from discussions with Yann Collet on list about alternate ways of encoding delta chains. The change most relevant to Mercurial is a performance enhancement in the simple decompression API to reuse a data structure across operations. This makes decompression of multiple inputs significantly faster. (This scenario occurs when reading revlog delta chains, for example.) Using python-zstandard's bench.py to measure the performance difference... On changelog chunks in the mozilla-unified repo: decompress discrete decompress() reuse zctx 1.262243 wall; 1.260000 CPU; 1.260000 user; 0.000000 sys 170.43 MB/s (best of 3) 0.949106 wall; 0.950000 CPU; 0.950000 user; 0.000000 sys 226.66 MB/s (best of 4) decompress discrete dict decompress() reuse zctx 0.692170 wall; 0.690000 CPU; 0.690000 user; 0.000000 sys 310.80 MB/s (best of 5) 0.437088 wall; 0.440000 CPU; 0.440000 user; 0.000000 sys 492.17 MB/s (best of 7) On manifest chunks in the mozilla-unified repo: decompress discrete decompress() reuse zctx 1.367284 wall; 1.370000 CPU; 1.370000 user; 0.000000 sys 274.01 MB/s (best of 3) 1.086831 wall; 1.080000 CPU; 1.080000 user; 0.000000 sys 344.72 MB/s (best of 3) decompress discrete dict decompress() reuse zctx 0.993272 wall; 0.990000 CPU; 0.990000 user; 0.000000 sys 377.19 MB/s (best of 3) 0.678651 wall; 0.680000 CPU; 0.680000 user; 0.000000 sys 552.06 MB/s (best of 5) That should make reads on zstd revlogs a bit faster ;) # no-check-commit
author Gregory Szorc <gregory.szorc@gmail.com>
date Tue, 07 Feb 2017 23:24:47 -0800
parents b86a448a2965
children e0dc40530c5a
comparison
equal deleted inserted replaced
30923:5b60464efbde 30924:c32454d69b85
11 except ImportError: 11 except ImportError:
12 hypothesis = None 12 hypothesis = None
13 13
14 import zstd 14 import zstd
15 15
16 from . common import (
17 make_cffi,
18 )
19
20
21 @make_cffi
16 class TestCompressionParameters(unittest.TestCase): 22 class TestCompressionParameters(unittest.TestCase):
17 def test_init_bad_arg_type(self): 23 def test_init_bad_arg_type(self):
18 with self.assertRaises(TypeError): 24 with self.assertRaises(TypeError):
19 zstd.CompressionParameters() 25 zstd.CompressionParameters()
20 26
40 46
41 def test_get_compression_parameters(self): 47 def test_get_compression_parameters(self):
42 p = zstd.get_compression_parameters(1) 48 p = zstd.get_compression_parameters(1)
43 self.assertIsInstance(p, zstd.CompressionParameters) 49 self.assertIsInstance(p, zstd.CompressionParameters)
44 50
45 self.assertEqual(p[0], 19) 51 self.assertEqual(p.window_log, 19)
52
53 def test_members(self):
54 p = zstd.CompressionParameters(10, 6, 7, 4, 5, 8, 1)
55 self.assertEqual(p.window_log, 10)
56 self.assertEqual(p.chain_log, 6)
57 self.assertEqual(p.hash_log, 7)
58 self.assertEqual(p.search_log, 4)
59 self.assertEqual(p.search_length, 5)
60 self.assertEqual(p.target_length, 8)
61 self.assertEqual(p.strategy, 1)
62
63
64 @make_cffi
65 class TestFrameParameters(unittest.TestCase):
66 def test_invalid_type(self):
67 with self.assertRaises(TypeError):
68 zstd.get_frame_parameters(None)
69
70 with self.assertRaises(TypeError):
71 zstd.get_frame_parameters(u'foobarbaz')
72
73 def test_invalid_input_sizes(self):
74 with self.assertRaisesRegexp(zstd.ZstdError, 'not enough data for frame'):
75 zstd.get_frame_parameters(b'')
76
77 with self.assertRaisesRegexp(zstd.ZstdError, 'not enough data for frame'):
78 zstd.get_frame_parameters(zstd.FRAME_HEADER)
79
80 def test_invalid_frame(self):
81 with self.assertRaisesRegexp(zstd.ZstdError, 'Unknown frame descriptor'):
82 zstd.get_frame_parameters(b'foobarbaz')
83
84 def test_attributes(self):
85 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x00\x00')
86 self.assertEqual(params.content_size, 0)
87 self.assertEqual(params.window_size, 1024)
88 self.assertEqual(params.dict_id, 0)
89 self.assertFalse(params.has_checksum)
90
91 # Lowest 2 bits indicate a dictionary and length. Here, the dict id is 1 byte.
92 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x01\x00\xff')
93 self.assertEqual(params.content_size, 0)
94 self.assertEqual(params.window_size, 1024)
95 self.assertEqual(params.dict_id, 255)
96 self.assertFalse(params.has_checksum)
97
98 # Lowest 3rd bit indicates if checksum is present.
99 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x04\x00')
100 self.assertEqual(params.content_size, 0)
101 self.assertEqual(params.window_size, 1024)
102 self.assertEqual(params.dict_id, 0)
103 self.assertTrue(params.has_checksum)
104
105 # Upper 2 bits indicate content size.
106 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x40\x00\xff\x00')
107 self.assertEqual(params.content_size, 511)
108 self.assertEqual(params.window_size, 1024)
109 self.assertEqual(params.dict_id, 0)
110 self.assertFalse(params.has_checksum)
111
112 # Window descriptor is 2nd byte after frame header.
113 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x00\x40')
114 self.assertEqual(params.content_size, 0)
115 self.assertEqual(params.window_size, 262144)
116 self.assertEqual(params.dict_id, 0)
117 self.assertFalse(params.has_checksum)
118
119 # Set multiple things.
120 params = zstd.get_frame_parameters(zstd.FRAME_HEADER + b'\x45\x40\x0f\x10\x00')
121 self.assertEqual(params.content_size, 272)
122 self.assertEqual(params.window_size, 262144)
123 self.assertEqual(params.dict_id, 15)
124 self.assertTrue(params.has_checksum)
125
46 126
47 if hypothesis: 127 if hypothesis:
48 s_windowlog = strategies.integers(min_value=zstd.WINDOWLOG_MIN, 128 s_windowlog = strategies.integers(min_value=zstd.WINDOWLOG_MIN,
49 max_value=zstd.WINDOWLOG_MAX) 129 max_value=zstd.WINDOWLOG_MAX)
50 s_chainlog = strategies.integers(min_value=zstd.CHAINLOG_MIN, 130 s_chainlog = strategies.integers(min_value=zstd.CHAINLOG_MIN,
63 zstd.STRATEGY_LAZY, 143 zstd.STRATEGY_LAZY,
64 zstd.STRATEGY_LAZY2, 144 zstd.STRATEGY_LAZY2,
65 zstd.STRATEGY_BTLAZY2, 145 zstd.STRATEGY_BTLAZY2,
66 zstd.STRATEGY_BTOPT)) 146 zstd.STRATEGY_BTOPT))
67 147
148
149 @make_cffi
68 class TestCompressionParametersHypothesis(unittest.TestCase): 150 class TestCompressionParametersHypothesis(unittest.TestCase):
69 @hypothesis.given(s_windowlog, s_chainlog, s_hashlog, s_searchlog, 151 @hypothesis.given(s_windowlog, s_chainlog, s_hashlog, s_searchlog,
70 s_searchlength, s_targetlength, s_strategy) 152 s_searchlength, s_targetlength, s_strategy)
71 def test_valid_init(self, windowlog, chainlog, hashlog, searchlog, 153 def test_valid_init(self, windowlog, chainlog, hashlog, searchlog,
72 searchlength, targetlength, strategy): 154 searchlength, targetlength, strategy):
73 p = zstd.CompressionParameters(windowlog, chainlog, hashlog, 155 p = zstd.CompressionParameters(windowlog, chainlog, hashlog,
74 searchlog, searchlength, 156 searchlog, searchlength,
75 targetlength, strategy) 157 targetlength, strategy)
76 self.assertEqual(tuple(p),
77 (windowlog, chainlog, hashlog, searchlog,
78 searchlength, targetlength, strategy))
79 158
80 # Verify we can instantiate a compressor with the supplied values. 159 # Verify we can instantiate a compressor with the supplied values.
81 # ZSTD_checkCParams moves the goal posts on us from what's advertised 160 # ZSTD_checkCParams moves the goal posts on us from what's advertised
82 # in the constants. So move along with them. 161 # in the constants. So move along with them.
83 if searchlength == zstd.SEARCHLENGTH_MIN and strategy in (zstd.STRATEGY_FAST, zstd.STRATEGY_GREEDY): 162 if searchlength == zstd.SEARCHLENGTH_MIN and strategy in (zstd.STRATEGY_FAST, zstd.STRATEGY_GREEDY):