Show More
@@ -1,268 +1,268 | |||
|
1 | 1 | # perf.py - performance test routines |
|
2 | 2 | '''helper extension to measure performance''' |
|
3 | 3 | |
|
4 | 4 | from mercurial import cmdutil, scmutil, util, match, commands |
|
5 | 5 | import time, os, sys |
|
6 | 6 | |
|
7 | cmdtable = {} | |
|
8 | command = cmdutil.command(cmdtable) | |
|
9 | ||
|
7 | 10 | def timer(func, title=None): |
|
8 | 11 | results = [] |
|
9 | 12 | begin = time.time() |
|
10 | 13 | count = 0 |
|
11 | 14 | while True: |
|
12 | 15 | ostart = os.times() |
|
13 | 16 | cstart = time.time() |
|
14 | 17 | r = func() |
|
15 | 18 | cstop = time.time() |
|
16 | 19 | ostop = os.times() |
|
17 | 20 | count += 1 |
|
18 | 21 | a, b = ostart, ostop |
|
19 | 22 | results.append((cstop - cstart, b[0] - a[0], b[1]-a[1])) |
|
20 | 23 | if cstop - begin > 3 and count >= 100: |
|
21 | 24 | break |
|
22 | 25 | if cstop - begin > 10 and count >= 3: |
|
23 | 26 | break |
|
24 | 27 | if title: |
|
25 | 28 | sys.stderr.write("! %s\n" % title) |
|
26 | 29 | if r: |
|
27 | 30 | sys.stderr.write("! result: %s\n" % r) |
|
28 | 31 | m = min(results) |
|
29 | 32 | sys.stderr.write("! wall %f comb %f user %f sys %f (best of %d)\n" |
|
30 | 33 | % (m[0], m[1] + m[2], m[1], m[2], count)) |
|
31 | 34 | |
|
35 | @command('perfwalk') | |
|
32 | 36 | def perfwalk(ui, repo, *pats): |
|
33 | 37 | try: |
|
34 | 38 | m = scmutil.match(repo[None], pats, {}) |
|
35 | 39 | timer(lambda: len(list(repo.dirstate.walk(m, [], True, False)))) |
|
36 | 40 | except Exception: |
|
37 | 41 | try: |
|
38 | 42 | m = scmutil.match(repo[None], pats, {}) |
|
39 | 43 | timer(lambda: len([b for a, b, c in repo.dirstate.statwalk([], m)])) |
|
40 | 44 | except Exception: |
|
41 | 45 | timer(lambda: len(list(cmdutil.walk(repo, pats, {})))) |
|
42 | 46 | |
|
47 | @command('perfstatus', | |
|
48 | [('u', 'unknown', False, | |
|
49 | 'ask status to look for unknown files')]) | |
|
43 | 50 | def perfstatus(ui, repo, **opts): |
|
44 | 51 | #m = match.always(repo.root, repo.getcwd()) |
|
45 | 52 | #timer(lambda: sum(map(len, repo.dirstate.status(m, [], False, False, |
|
46 | 53 | # False)))) |
|
47 | 54 | timer(lambda: sum(map(len, repo.status(**opts)))) |
|
48 | 55 | |
|
49 | 56 | def clearcaches(cl): |
|
50 | 57 | # behave somewhat consistently across internal API changes |
|
51 | 58 | if util.safehasattr(cl, 'clearcaches'): |
|
52 | 59 | cl.clearcaches() |
|
53 | 60 | elif util.safehasattr(cl, '_nodecache'): |
|
54 | 61 | from mercurial.node import nullid, nullrev |
|
55 | 62 | cl._nodecache = {nullid: nullrev} |
|
56 | 63 | cl._nodepos = None |
|
57 | 64 | |
|
65 | @command('perfheads') | |
|
58 | 66 | def perfheads(ui, repo): |
|
59 | 67 | cl = repo.changelog |
|
60 | 68 | def d(): |
|
61 | 69 | len(cl.headrevs()) |
|
62 | 70 | clearcaches(cl) |
|
63 | 71 | timer(d) |
|
64 | 72 | |
|
73 | @command('perftags') | |
|
65 | 74 | def perftags(ui, repo): |
|
66 | 75 | import mercurial.changelog, mercurial.manifest |
|
67 | 76 | def t(): |
|
68 | 77 | repo.changelog = mercurial.changelog.changelog(repo.sopener) |
|
69 | 78 | repo.manifest = mercurial.manifest.manifest(repo.sopener) |
|
70 | 79 | repo._tags = None |
|
71 | 80 | return len(repo.tags()) |
|
72 | 81 | timer(t) |
|
73 | 82 | |
|
83 | @command('perfancestors') | |
|
74 | 84 | def perfancestors(ui, repo): |
|
75 | 85 | heads = repo.changelog.headrevs() |
|
76 | 86 | def d(): |
|
77 | 87 | for a in repo.changelog.ancestors(heads): |
|
78 | 88 | pass |
|
79 | 89 | timer(d) |
|
80 | 90 | |
|
91 | @command('perfancestorset') | |
|
81 | 92 | def perfancestorset(ui, repo, revset): |
|
82 | 93 | revs = repo.revs(revset) |
|
83 | 94 | heads = repo.changelog.headrevs() |
|
84 | 95 | def d(): |
|
85 | 96 | s = repo.changelog.ancestors(heads) |
|
86 | 97 | for rev in revs: |
|
87 | 98 | rev in s |
|
88 | 99 | timer(d) |
|
89 | 100 | |
|
101 | @command('perfdirstate') | |
|
90 | 102 | def perfdirstate(ui, repo): |
|
91 | 103 | "a" in repo.dirstate |
|
92 | 104 | def d(): |
|
93 | 105 | repo.dirstate.invalidate() |
|
94 | 106 | "a" in repo.dirstate |
|
95 | 107 | timer(d) |
|
96 | 108 | |
|
109 | @command('perfdirstatedirs') | |
|
97 | 110 | def perfdirstatedirs(ui, repo): |
|
98 | 111 | "a" in repo.dirstate |
|
99 | 112 | def d(): |
|
100 | 113 | "a" in repo.dirstate._dirs |
|
101 | 114 | del repo.dirstate._dirs |
|
102 | 115 | timer(d) |
|
103 | 116 | |
|
117 | @command('perfdirstatewrite') | |
|
104 | 118 | def perfdirstatewrite(ui, repo): |
|
105 | 119 | ds = repo.dirstate |
|
106 | 120 | "a" in ds |
|
107 | 121 | def d(): |
|
108 | 122 | ds._dirty = True |
|
109 | 123 | ds.write() |
|
110 | 124 | timer(d) |
|
111 | 125 | |
|
126 | @command('perfmanifest') | |
|
112 | 127 | def perfmanifest(ui, repo): |
|
113 | 128 | def d(): |
|
114 | 129 | t = repo.manifest.tip() |
|
115 | 130 | m = repo.manifest.read(t) |
|
116 | 131 | repo.manifest.mapcache = None |
|
117 | 132 | repo.manifest._cache = None |
|
118 | 133 | timer(d) |
|
119 | 134 | |
|
135 | @command('perfchangeset') | |
|
120 | 136 | def perfchangeset(ui, repo, rev): |
|
121 | 137 | n = repo[rev].node() |
|
122 | 138 | def d(): |
|
123 | 139 | c = repo.changelog.read(n) |
|
124 | 140 | #repo.changelog._cache = None |
|
125 | 141 | timer(d) |
|
126 | 142 | |
|
143 | @command('perfindex') | |
|
127 | 144 | def perfindex(ui, repo): |
|
128 | 145 | import mercurial.revlog |
|
129 | 146 | mercurial.revlog._prereadsize = 2**24 # disable lazy parser in old hg |
|
130 | 147 | n = repo["tip"].node() |
|
131 | 148 | def d(): |
|
132 | 149 | cl = mercurial.revlog.revlog(repo.sopener, "00changelog.i") |
|
133 | 150 | cl.rev(n) |
|
134 | 151 | timer(d) |
|
135 | 152 | |
|
153 | @command('perfstartup') | |
|
136 | 154 | def perfstartup(ui, repo): |
|
137 | 155 | cmd = sys.argv[0] |
|
138 | 156 | def d(): |
|
139 | 157 | os.system("HGRCPATH= %s version -q > /dev/null" % cmd) |
|
140 | 158 | timer(d) |
|
141 | 159 | |
|
160 | @command('perfparents') | |
|
142 | 161 | def perfparents(ui, repo): |
|
143 | 162 | nl = [repo.changelog.node(i) for i in xrange(1000)] |
|
144 | 163 | def d(): |
|
145 | 164 | for n in nl: |
|
146 | 165 | repo.changelog.parents(n) |
|
147 | 166 | timer(d) |
|
148 | 167 | |
|
168 | @command('perflookup') | |
|
149 | 169 | def perflookup(ui, repo, rev): |
|
150 | 170 | timer(lambda: len(repo.lookup(rev))) |
|
151 | 171 | |
|
172 | @command('perfrevrange') | |
|
152 | 173 | def perfrevrange(ui, repo, *specs): |
|
153 | 174 | revrange = scmutil.revrange |
|
154 | 175 | timer(lambda: len(revrange(repo, specs))) |
|
155 | 176 | |
|
177 | @command('perfnodelookup') | |
|
156 | 178 | def perfnodelookup(ui, repo, rev): |
|
157 | 179 | import mercurial.revlog |
|
158 | 180 | mercurial.revlog._prereadsize = 2**24 # disable lazy parser in old hg |
|
159 | 181 | n = repo[rev].node() |
|
160 | 182 | cl = mercurial.revlog.revlog(repo.sopener, "00changelog.i") |
|
161 | 183 | def d(): |
|
162 | 184 | cl.rev(n) |
|
163 | 185 | clearcaches(cl) |
|
164 | 186 | timer(d) |
|
165 | 187 | |
|
188 | @command('perflog', | |
|
189 | [('', 'rename', False, 'ask log to follow renames')]) | |
|
166 | 190 | def perflog(ui, repo, **opts): |
|
167 | 191 | ui.pushbuffer() |
|
168 | 192 | timer(lambda: commands.log(ui, repo, rev=[], date='', user='', |
|
169 | 193 | copies=opts.get('rename'))) |
|
170 | 194 | ui.popbuffer() |
|
171 | 195 | |
|
196 | @command('perftemplating') | |
|
172 | 197 | def perftemplating(ui, repo): |
|
173 | 198 | ui.pushbuffer() |
|
174 | 199 | timer(lambda: commands.log(ui, repo, rev=[], date='', user='', |
|
175 | 200 | template='{date|shortdate} [{rev}:{node|short}]' |
|
176 | 201 | ' {author|person}: {desc|firstline}\n')) |
|
177 | 202 | ui.popbuffer() |
|
178 | 203 | |
|
204 | @command('perfcca') | |
|
179 | 205 | def perfcca(ui, repo): |
|
180 | 206 | timer(lambda: scmutil.casecollisionauditor(ui, False, repo.dirstate)) |
|
181 | 207 | |
|
208 | @command('perffncacheload') | |
|
182 | 209 | def perffncacheload(ui, repo): |
|
183 | 210 | s = repo.store |
|
184 | 211 | def d(): |
|
185 | 212 | s.fncache._load() |
|
186 | 213 | timer(d) |
|
187 | 214 | |
|
215 | @command('perffncachewrite') | |
|
188 | 216 | def perffncachewrite(ui, repo): |
|
189 | 217 | s = repo.store |
|
190 | 218 | s.fncache._load() |
|
191 | 219 | def d(): |
|
192 | 220 | s.fncache._dirty = True |
|
193 | 221 | s.fncache.write() |
|
194 | 222 | timer(d) |
|
195 | 223 | |
|
224 | @command('perffncacheencode') | |
|
196 | 225 | def perffncacheencode(ui, repo): |
|
197 | 226 | s = repo.store |
|
198 | 227 | s.fncache._load() |
|
199 | 228 | def d(): |
|
200 | 229 | for p in s.fncache.entries: |
|
201 | 230 | s.encode(p) |
|
202 | 231 | timer(d) |
|
203 | 232 | |
|
233 | @command('perfdiffwd') | |
|
204 | 234 | def perfdiffwd(ui, repo): |
|
205 | 235 | """Profile diff of working directory changes""" |
|
206 | 236 | options = { |
|
207 | 237 | 'w': 'ignore_all_space', |
|
208 | 238 | 'b': 'ignore_space_change', |
|
209 | 239 | 'B': 'ignore_blank_lines', |
|
210 | 240 | } |
|
211 | 241 | |
|
212 | 242 | for diffopt in ('', 'w', 'b', 'B', 'wB'): |
|
213 | 243 | opts = dict((options[c], '1') for c in diffopt) |
|
214 | 244 | def d(): |
|
215 | 245 | ui.pushbuffer() |
|
216 | 246 | commands.diff(ui, repo, **opts) |
|
217 | 247 | ui.popbuffer() |
|
218 | 248 | title = 'diffopts: %s' % (diffopt and ('-' + diffopt) or 'none') |
|
219 | 249 | timer(d, title) |
|
220 | 250 | |
|
251 | @command('perfrevlog', | |
|
252 | [('d', 'dist', 100, 'distance between the revisions')], | |
|
253 | "[INDEXFILE]") | |
|
221 | 254 | def perfrevlog(ui, repo, file_, **opts): |
|
222 | 255 | from mercurial import revlog |
|
223 | 256 | dist = opts['dist'] |
|
224 | 257 | def d(): |
|
225 | 258 | r = revlog.revlog(lambda fn: open(fn, 'rb'), file_) |
|
226 | 259 | for x in xrange(0, len(r), dist): |
|
227 | 260 | r.revision(r.node(x)) |
|
228 | 261 | |
|
229 | 262 | timer(d) |
|
230 | 263 | |
|
264 | @command('perfrevset') | |
|
231 | 265 | def perfrevset(ui, repo, expr): |
|
232 | 266 | def d(): |
|
233 | 267 | repo.revs(expr) |
|
234 | 268 | timer(d) |
|
235 | ||
|
236 | cmdtable = { | |
|
237 | 'perfcca': (perfcca, []), | |
|
238 | 'perffncacheload': (perffncacheload, []), | |
|
239 | 'perffncachewrite': (perffncachewrite, []), | |
|
240 | 'perffncacheencode': (perffncacheencode, []), | |
|
241 | 'perflookup': (perflookup, []), | |
|
242 | 'perfrevrange': (perfrevrange, []), | |
|
243 | 'perfnodelookup': (perfnodelookup, []), | |
|
244 | 'perfparents': (perfparents, []), | |
|
245 | 'perfstartup': (perfstartup, []), | |
|
246 | 'perfstatus': (perfstatus, | |
|
247 | [('u', 'unknown', False, | |
|
248 | 'ask status to look for unknown files')]), | |
|
249 | 'perfwalk': (perfwalk, []), | |
|
250 | 'perfmanifest': (perfmanifest, []), | |
|
251 | 'perfchangeset': (perfchangeset, []), | |
|
252 | 'perfindex': (perfindex, []), | |
|
253 | 'perfheads': (perfheads, []), | |
|
254 | 'perftags': (perftags, []), | |
|
255 | 'perfancestors': (perfancestors, []), | |
|
256 | 'perfancestorset': (perfancestorset, [], "REVSET"), | |
|
257 | 'perfdirstate': (perfdirstate, []), | |
|
258 | 'perfdirstatedirs': (perfdirstate, []), | |
|
259 | 'perfdirstatewrite': (perfdirstatewrite, []), | |
|
260 | 'perflog': (perflog, | |
|
261 | [('', 'rename', False, 'ask log to follow renames')]), | |
|
262 | 'perftemplating': (perftemplating, []), | |
|
263 | 'perfdiffwd': (perfdiffwd, []), | |
|
264 | 'perfrevlog': (perfrevlog, | |
|
265 | [('d', 'dist', 100, 'distance between the revisions')], | |
|
266 | "[INDEXFILE]"), | |
|
267 | 'perfrevset': (perfrevset, [], "REVSET") | |
|
268 | } |
General Comments 0
You need to be logged in to leave comments.
Login now