1
# Copyright (C) 2006 by 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 version 2 as published by
5
# the Free Software Foundation.
7
# This program is distributed in the hope that it will be useful,
8
# but WITHOUT ANY WARRANTY; without even the implied warranty of
9
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
# GNU General Public License for more details.
12
# You should have received a copy of the GNU General Public License
13
# along with this program; if not, write to the Free Software
14
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16
"""Tests for encoding performance."""
23
from bzrlib.benchmarks import Benchmark
26
_normal_revision_id = (u'john@arbash-meinel.com-20060801200018'
28
_unicode_revision_id = (u'\u062c\u0648\u062c\u0648@\xe5rbash-meinel.com-'
29
u'\xb5\xb5\xb5-20060801200018-cafa6272d9b8cac4')
31
_normal_revision_id_utf8 = _normal_revision_id.encode('utf-8')
32
_unicode_revision_id_utf8 = _unicode_revision_id.encode('utf-8')
35
class EncodingBenchmark(Benchmark):
38
super(EncodingBenchmark, self).setUp()
39
# Make sure we start and end with a clean cache
40
cache_utf8.clear_encoding_cache()
41
self.addCleanup(cache_utf8.clear_encoding_cache)
43
def encode_1M(self, revision_id):
44
"""Encode the given revision id 1 million times"""
45
# In a real kernel tree there are 7.7M lines of code
46
# so the initial import actually has to encode a revision
47
# id to store annotated lines one time for every line.
48
for i in xrange(1000000):
49
revision_id.encode('utf8')
51
def encode_cached_1M(self, revision_id):
52
"""Encode the given revision id 1 million times using the cache"""
53
encode = cache_utf8.encode
54
for i in xrange(1000000):
57
def encode_multi(self, revision_list, count):
58
"""Encode each entry in the list count times"""
59
for i in xrange(count):
60
for revision_id in revision_list:
61
revision_id.encode('utf-8')
63
def encode_cached_multi(self, revision_list, count):
64
"""Encode each entry in the list count times"""
65
encode = cache_utf8.encode
66
for i in xrange(count):
67
for revision_id in revision_list:
70
def test_encode_1_by_1M_ascii(self):
71
"""Test encoding a single revision id 1 million times."""
72
self.time(self.encode_1M, _normal_revision_id)
74
def test_encode_1_by_1M_ascii_cached(self):
75
"""Test encoding a single revision id 1 million times."""
76
self.time(self.encode_cached_1M, _normal_revision_id)
78
def test_encode_1_by_1M_ascii_str(self):
79
# We have places that think they have a unicode revision id
80
# but actually, they have a plain string. So .encode(utf8)
81
# actually has to decode from ascii, and then encode into utf8
82
self.time(self.encode_1M, str(_normal_revision_id))
84
def test_encode_1_by_1M_ascii_str_cached(self):
85
self.time(self.encode_cached_1M, str(_normal_revision_id))
87
def test_encode_1_by_1M_unicode(self):
88
"""Test encoding a single revision id 1 million times."""
89
self.time(self.encode_1M, _unicode_revision_id)
91
def test_encode_1_by_1M_unicode_cached(self):
92
"""Test encoding a single revision id 1 million times."""
93
self.time(self.encode_cached_1M, _unicode_revision_id)
95
def test_encode_1k_by_1k_ascii(self):
96
"""Test encoding 5 revisions 100k times"""
97
revisions = [unicode(osutils.rand_chars(60)) for x in xrange(1000)]
98
self.time(self.encode_multi, revisions, 1000)
100
def test_encode_1k_by_1k_ascii_cached(self):
101
"""Test encoding 5 revisions 100k times"""
102
revisions = [unicode(osutils.rand_chars(60)) for x in xrange(1000)]
103
self.time(self.encode_cached_multi, revisions, 1000)
105
def test_encode_1k_by_1k_unicode(self):
106
"""Test encoding 5 revisions 100k times"""
107
revisions = ['\u062c\u0648\u062c\u0648' +
108
unicode(osutils.rand_chars(60)) for x in xrange(1000)]
109
self.time(self.encode_multi, revisions, 1000)
111
def test_encode_1k_by_1k_unicode_cached(self):
112
"""Test encoding 5 revisions 100k times"""
113
revisions = ['\u062c\u0648\u062c\u0648' +
114
unicode(osutils.rand_chars(60)) for x in xrange(1000)]
115
self.time(self.encode_cached_multi, revisions, 1000)
118
class DecodingBenchmarks(Benchmark):
121
super(DecodingBenchmarks, self).setUp()
122
# Make sure we start and end with a clean cache
123
cache_utf8.clear_encoding_cache()
124
self.addCleanup(cache_utf8.clear_encoding_cache)
126
def decode_1M(self, revision_id):
127
for i in xrange(1000000):
128
revision_id.decode('utf8')
130
def decode_cached_1M(self, revision_id):
131
decode = cache_utf8.decode
132
for i in xrange(1000000):
135
def decode_multi(self, revision_list, count):
136
for i in xrange(count):
137
for revision_id in revision_list:
138
revision_id.decode('utf-8')
140
def decode_cached_multi(self, revision_list, count):
141
decode = cache_utf8.decode
142
for i in xrange(count):
143
for revision_id in revision_list:
146
def test_decode_1_by_1M_ascii(self):
147
"""Test decoding a single revision id 1 million times."""
148
self.time(self.decode_1M, _normal_revision_id_utf8)
150
def test_decode_1_by_1M_ascii_cached(self):
151
"""Test decoding a single revision id 1 million times."""
152
self.time(self.decode_cached_1M, _normal_revision_id_utf8)
154
def test_decode_1_by_1M_unicode(self):
155
"""Test decoding a single revision id 1 million times."""
156
self.time(self.decode_1M, _unicode_revision_id_utf8)
158
def test_decode_1_by_1M_unicode_cached(self):
159
"""Test decoding a single revision id 1 million times."""
160
self.time(self.decode_cached_1M, _unicode_revision_id_utf8)
162
def test_decode_1k_by_1k_ascii(self):
163
"""Test decoding 5 revisions 100k times"""
164
revisions = [osutils.rand_chars(60) for x in xrange(1000)]
165
self.time(self.decode_multi, revisions, 1000)
167
def test_decode_1k_by_1k_ascii_cached(self):
168
"""Test decoding 5 revisions 100k times"""
169
revisions = [osutils.rand_chars(60) for x in xrange(1000)]
170
self.time(self.decode_cached_multi, revisions, 1000)
172
def test_decode_1k_by_1k_unicode(self):
173
"""Test decoding 5 revisions 100k times"""
174
revisions = [('\u062c\u0648\u062c\u0648' +
175
unicode(osutils.rand_chars(60))).encode('utf8')
176
for x in xrange(1000)]
177
self.time(self.decode_multi, revisions, 1000)
179
def test_decode_1k_by_1k_unicode_cached(self):
180
"""Test decoding 5 revisions 100k times"""
181
revisions = [('\u062c\u0648\u062c\u0648' +
182
unicode(osutils.rand_chars(60))).encode('utf8')
183
for x in xrange(1000)]
184
self.time(self.decode_cached_multi, revisions, 1000)