1
# Copyright (C) 2006 Canonical Ltd
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
# GNU General Public License for more details.
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
"""Tests for encoding performance."""
25
from bzrlib.benchmarks import Benchmark
28
_normal_revision_id = (u'john@arbash-meinel.com-20060801200018'
30
_unicode_revision_id = (u'\u062c\u0648\u062c\u0648@\xe5rbash-meinel.com-'
31
u'\xb5\xb5\xb5-20060801200018-cafa6272d9b8cac4')
33
_normal_revision_id_utf8 = _normal_revision_id.encode('utf-8')
34
_unicode_revision_id_utf8 = _unicode_revision_id.encode('utf-8')
37
class EncodingBenchmark(Benchmark):
40
super(EncodingBenchmark, self).setUp()
41
# Make sure we start and end with a clean cache
42
cache_utf8.clear_encoding_cache()
43
self.addCleanup(cache_utf8.clear_encoding_cache)
45
def encode_1M(self, revision_id):
46
"""Encode the given revision id 1 million times"""
47
# In a real kernel tree there are 7.7M lines of code
48
# so the initial import actually has to encode a revision
49
# id to store annotated lines one time for every line.
50
for i in xrange(1000000):
51
revision_id.encode('utf8')
53
def encode_cached_1M(self, revision_id):
54
"""Encode the given revision id 1 million times using the cache"""
55
encode = cache_utf8.encode
56
for i in xrange(1000000):
59
def encode_multi(self, revision_list, count):
60
"""Encode each entry in the list count times"""
61
for i in xrange(count):
62
for revision_id in revision_list:
63
revision_id.encode('utf-8')
65
def encode_cached_multi(self, revision_list, count):
66
"""Encode each entry in the list count times"""
67
encode = cache_utf8.encode
68
for i in xrange(count):
69
for revision_id in revision_list:
72
def test_encode_1_by_1M_ascii(self):
73
"""Test encoding a single revision id 1 million times."""
74
self.time(self.encode_1M, _normal_revision_id)
76
def test_encode_1_by_1M_ascii_cached(self):
77
"""Test encoding a single revision id 1 million times."""
78
self.time(self.encode_cached_1M, _normal_revision_id)
80
def test_encode_1_by_1M_ascii_str(self):
81
# We have places that think they have a unicode revision id
82
# but actually, they have a plain string. So .encode(utf8)
83
# actually has to decode from ascii, and then encode into utf8
84
self.time(self.encode_1M, str(_normal_revision_id))
86
def test_encode_1_by_1M_ascii_str_cached(self):
87
self.time(self.encode_cached_1M, str(_normal_revision_id))
89
def test_encode_1_by_1M_unicode(self):
90
"""Test encoding a single revision id 1 million times."""
91
self.time(self.encode_1M, _unicode_revision_id)
93
def test_encode_1_by_1M_unicode_cached(self):
94
"""Test encoding a single revision id 1 million times."""
95
self.time(self.encode_cached_1M, _unicode_revision_id)
97
def test_encode_1k_by_1k_ascii(self):
98
"""Test encoding 5 revisions 100k times"""
99
revisions = [unicode(osutils.rand_chars(60)) for x in xrange(1000)]
100
self.time(self.encode_multi, revisions, 1000)
102
def test_encode_1k_by_1k_ascii_cached(self):
103
"""Test encoding 5 revisions 100k times"""
104
revisions = [unicode(osutils.rand_chars(60)) for x in xrange(1000)]
105
self.time(self.encode_cached_multi, revisions, 1000)
107
def test_encode_1k_by_1k_unicode(self):
108
"""Test encoding 5 revisions 100k times"""
109
revisions = [u'\u062c\u0648\u062c\u0648' +
110
unicode(osutils.rand_chars(60)) for x in xrange(1000)]
111
self.time(self.encode_multi, revisions, 1000)
113
def test_encode_1k_by_1k_unicode_cached(self):
114
"""Test encoding 5 revisions 100k times"""
115
revisions = [u'\u062c\u0648\u062c\u0648' +
116
unicode(osutils.rand_chars(60)) for x in xrange(1000)]
117
self.time(self.encode_cached_multi, revisions, 1000)
119
def test_encode_500K_by_1_ascii(self):
120
revisions = [unicode("test%07d" % x) for x in xrange(500000)]
121
self.time(self.encode_multi, revisions, 1)
123
def test_encode_500K_by_1_ascii_cached(self):
124
revisions = [unicode("test%07d" % x) for x in xrange(500000)]
125
self.time(self.encode_cached_multi, revisions, 1)
127
def test_encode_500K_by_1_unicode(self):
128
revisions = [u'\u062c\u0648\u062c\u0648' +
129
unicode("%07d" % x) for x in xrange(500000)]
130
self.time(self.encode_multi, revisions, 1)
132
def test_encode_500K_by_1_unicode_cached(self):
133
revisions = [u'\u062c\u0648\u062c\u0648' +
134
unicode("%07d" % x) for x in xrange(500000)]
135
self.time(self.encode_cached_multi, revisions, 1)
137
class DecodingBenchmarks(Benchmark):
140
super(DecodingBenchmarks, self).setUp()
141
# Make sure we start and end with a clean cache
142
cache_utf8.clear_encoding_cache()
143
self.addCleanup(cache_utf8.clear_encoding_cache)
145
def decode_1M(self, revision_id):
146
for i in xrange(1000000):
147
revision_id.decode('utf8')
149
def decode_cached_1M(self, revision_id):
150
decode = cache_utf8.decode
151
for i in xrange(1000000):
154
def decode_multi(self, revision_list, count):
155
for i in xrange(count):
156
for revision_id in revision_list:
157
revision_id.decode('utf-8')
159
def decode_cached_multi(self, revision_list, count):
160
decode = cache_utf8.decode
161
for i in xrange(count):
162
for revision_id in revision_list:
165
def test_decode_1_by_1M_ascii(self):
166
"""Test decoding a single revision id 1 million times."""
167
self.time(self.decode_1M, _normal_revision_id_utf8)
169
def test_decode_1_by_1M_ascii_cached(self):
170
"""Test decoding a single revision id 1 million times."""
171
self.time(self.decode_cached_1M, _normal_revision_id_utf8)
173
def test_decode_1_by_1M_unicode(self):
174
"""Test decoding a single revision id 1 million times."""
175
self.time(self.decode_1M, _unicode_revision_id_utf8)
177
def test_decode_1_by_1M_unicode_cached(self):
178
"""Test decoding a single revision id 1 million times."""
179
self.time(self.decode_cached_1M, _unicode_revision_id_utf8)
181
def test_decode_1k_by_1k_ascii(self):
182
"""Test decoding 5 revisions 100k times"""
183
revisions = [osutils.rand_chars(60) for x in xrange(1000)]
184
self.time(self.decode_multi, revisions, 1000)
186
def test_decode_1k_by_1k_ascii_cached(self):
187
"""Test decoding 5 revisions 100k times"""
188
revisions = [osutils.rand_chars(60) for x in xrange(1000)]
189
self.time(self.decode_cached_multi, revisions, 1000)
191
def test_decode_1k_by_1k_unicode(self):
192
"""Test decoding 5 revisions 100k times"""
193
revisions = [(u'\u062c\u0648\u062c\u0648' +
194
unicode(osutils.rand_chars(60))).encode('utf8')
195
for x in xrange(1000)]
196
self.time(self.decode_multi, revisions, 1000)
198
def test_decode_1k_by_1k_unicode_cached(self):
199
"""Test decoding 5 revisions 100k times"""
200
revisions = [(u'\u062c\u0648\u062c\u0648' +
201
unicode(osutils.rand_chars(60))).encode('utf8')
202
for x in xrange(1000)]
203
self.time(self.decode_cached_multi, revisions, 1000)
205
def test_decode_500K_by_1_ascii(self):
206
revisions = [("test%07d" % x) for x in xrange(500000)]
207
self.time(self.decode_multi, revisions, 1)
209
def test_decode_500K_by_1_ascii_cached(self):
210
revisions = [("test%07d" % x) for x in xrange(500000)]
211
self.time(self.decode_cached_multi, revisions, 1)
213
def test_decode_500K_by_1_unicode(self):
214
revisions = [(u'\u062c\u0648\u062c\u0648' +
215
unicode("%07d" % x)).encode('utf-8')
216
for x in xrange(500000)]
217
self.time(self.decode_multi, revisions, 1)
219
def test_decode_500K_by_1_unicode_cached(self):
220
revisions = [(u'\u062c\u0648\u062c\u0648' +
221
unicode("%07d" % x)).encode('utf-8')
222
for x in xrange(500000)]
223
self.time(self.decode_cached_multi, revisions, 1)