##// END OF EJS Templates
perf: call clearcaches() in perfmanifest...
Gregory Szorc -
r27467:fbe292b5 default
parent child Browse files
Show More
@@ -1,685 +1,684 b''
1 1 # perf.py - performance test routines
2 2 '''helper extension to measure performance'''
3 3
4 4 from mercurial import cmdutil, scmutil, util, commands, obsolete
5 5 from mercurial import repoview, branchmap, merge, copies, error
6 6 import time, os, sys
7 7 import random
8 8 import functools
9 9
10 10 formatteropts = commands.formatteropts
11 11
12 12 cmdtable = {}
13 13 command = cmdutil.command(cmdtable)
14 14
15 15 def getlen(ui):
16 16 if ui.configbool("perf", "stub"):
17 17 return lambda x: 1
18 18 return len
19 19
20 20 def gettimer(ui, opts=None):
21 21 """return a timer function and formatter: (timer, formatter)
22 22
23 23 This function exists to gather the creation of formatter in a single
24 24 place instead of duplicating it in all performance commands."""
25 25
26 26 # enforce an idle period before execution to counteract power management
27 27 # experimental config: perf.presleep
28 28 time.sleep(ui.configint("perf", "presleep", 1))
29 29
30 30 if opts is None:
31 31 opts = {}
32 32 # redirect all to stderr
33 33 ui = ui.copy()
34 34 ui.fout = ui.ferr
35 35 # get a formatter
36 36 fm = ui.formatter('perf', opts)
37 37 # stub function, runs code only once instead of in a loop
38 38 # experimental config: perf.stub
39 39 if ui.configbool("perf", "stub"):
40 40 return functools.partial(stub_timer, fm), fm
41 41 return functools.partial(_timer, fm), fm
42 42
43 43 def stub_timer(fm, func, title=None):
44 44 func()
45 45
46 46 def _timer(fm, func, title=None):
47 47 results = []
48 48 begin = time.time()
49 49 count = 0
50 50 while True:
51 51 ostart = os.times()
52 52 cstart = time.time()
53 53 r = func()
54 54 cstop = time.time()
55 55 ostop = os.times()
56 56 count += 1
57 57 a, b = ostart, ostop
58 58 results.append((cstop - cstart, b[0] - a[0], b[1]-a[1]))
59 59 if cstop - begin > 3 and count >= 100:
60 60 break
61 61 if cstop - begin > 10 and count >= 3:
62 62 break
63 63
64 64 fm.startitem()
65 65
66 66 if title:
67 67 fm.write('title', '! %s\n', title)
68 68 if r:
69 69 fm.write('result', '! result: %s\n', r)
70 70 m = min(results)
71 71 fm.plain('!')
72 72 fm.write('wall', ' wall %f', m[0])
73 73 fm.write('comb', ' comb %f', m[1] + m[2])
74 74 fm.write('user', ' user %f', m[1])
75 75 fm.write('sys', ' sys %f', m[2])
76 76 fm.write('count', ' (best of %d)', count)
77 77 fm.plain('\n')
78 78
79 79 @command('perfwalk', formatteropts)
80 80 def perfwalk(ui, repo, *pats, **opts):
81 81 timer, fm = gettimer(ui, opts)
82 82 try:
83 83 m = scmutil.match(repo[None], pats, {})
84 84 timer(lambda: len(list(repo.dirstate.walk(m, [], True, False))))
85 85 except Exception:
86 86 try:
87 87 m = scmutil.match(repo[None], pats, {})
88 88 timer(lambda: len([b for a, b, c in repo.dirstate.statwalk([], m)]))
89 89 except Exception:
90 90 timer(lambda: len(list(cmdutil.walk(repo, pats, {}))))
91 91 fm.end()
92 92
93 93 @command('perfannotate', formatteropts)
94 94 def perfannotate(ui, repo, f, **opts):
95 95 timer, fm = gettimer(ui, opts)
96 96 fc = repo['.'][f]
97 97 timer(lambda: len(fc.annotate(True)))
98 98 fm.end()
99 99
100 100 @command('perfstatus',
101 101 [('u', 'unknown', False,
102 102 'ask status to look for unknown files')] + formatteropts)
103 103 def perfstatus(ui, repo, **opts):
104 104 #m = match.always(repo.root, repo.getcwd())
105 105 #timer(lambda: sum(map(len, repo.dirstate.status(m, [], False, False,
106 106 # False))))
107 107 timer, fm = gettimer(ui, opts)
108 108 timer(lambda: sum(map(len, repo.status(unknown=opts['unknown']))))
109 109 fm.end()
110 110
111 111 @command('perfaddremove', formatteropts)
112 112 def perfaddremove(ui, repo, **opts):
113 113 timer, fm = gettimer(ui, opts)
114 114 try:
115 115 oldquiet = repo.ui.quiet
116 116 repo.ui.quiet = True
117 117 matcher = scmutil.match(repo[None])
118 118 timer(lambda: scmutil.addremove(repo, matcher, "", dry_run=True))
119 119 finally:
120 120 repo.ui.quiet = oldquiet
121 121 fm.end()
122 122
123 123 def clearcaches(cl):
124 124 # behave somewhat consistently across internal API changes
125 125 if util.safehasattr(cl, 'clearcaches'):
126 126 cl.clearcaches()
127 127 elif util.safehasattr(cl, '_nodecache'):
128 128 from mercurial.node import nullid, nullrev
129 129 cl._nodecache = {nullid: nullrev}
130 130 cl._nodepos = None
131 131
132 132 @command('perfheads', formatteropts)
133 133 def perfheads(ui, repo, **opts):
134 134 timer, fm = gettimer(ui, opts)
135 135 cl = repo.changelog
136 136 def d():
137 137 len(cl.headrevs())
138 138 clearcaches(cl)
139 139 timer(d)
140 140 fm.end()
141 141
142 142 @command('perftags', formatteropts)
143 143 def perftags(ui, repo, **opts):
144 144 import mercurial.changelog
145 145 import mercurial.manifest
146 146 timer, fm = gettimer(ui, opts)
147 147 def t():
148 148 repo.changelog = mercurial.changelog.changelog(repo.svfs)
149 149 repo.manifest = mercurial.manifest.manifest(repo.svfs)
150 150 repo._tags = None
151 151 return len(repo.tags())
152 152 timer(t)
153 153 fm.end()
154 154
155 155 @command('perfancestors', formatteropts)
156 156 def perfancestors(ui, repo, **opts):
157 157 timer, fm = gettimer(ui, opts)
158 158 heads = repo.changelog.headrevs()
159 159 def d():
160 160 for a in repo.changelog.ancestors(heads):
161 161 pass
162 162 timer(d)
163 163 fm.end()
164 164
165 165 @command('perfancestorset', formatteropts)
166 166 def perfancestorset(ui, repo, revset, **opts):
167 167 timer, fm = gettimer(ui, opts)
168 168 revs = repo.revs(revset)
169 169 heads = repo.changelog.headrevs()
170 170 def d():
171 171 s = repo.changelog.ancestors(heads)
172 172 for rev in revs:
173 173 rev in s
174 174 timer(d)
175 175 fm.end()
176 176
177 177 @command('perfdirs', formatteropts)
178 178 def perfdirs(ui, repo, **opts):
179 179 timer, fm = gettimer(ui, opts)
180 180 dirstate = repo.dirstate
181 181 'a' in dirstate
182 182 def d():
183 183 dirstate.dirs()
184 184 del dirstate._dirs
185 185 timer(d)
186 186 fm.end()
187 187
188 188 @command('perfdirstate', formatteropts)
189 189 def perfdirstate(ui, repo, **opts):
190 190 timer, fm = gettimer(ui, opts)
191 191 "a" in repo.dirstate
192 192 def d():
193 193 repo.dirstate.invalidate()
194 194 "a" in repo.dirstate
195 195 timer(d)
196 196 fm.end()
197 197
198 198 @command('perfdirstatedirs', formatteropts)
199 199 def perfdirstatedirs(ui, repo, **opts):
200 200 timer, fm = gettimer(ui, opts)
201 201 "a" in repo.dirstate
202 202 def d():
203 203 "a" in repo.dirstate._dirs
204 204 del repo.dirstate._dirs
205 205 timer(d)
206 206 fm.end()
207 207
208 208 @command('perfdirstatefoldmap', formatteropts)
209 209 def perfdirstatefoldmap(ui, repo, **opts):
210 210 timer, fm = gettimer(ui, opts)
211 211 dirstate = repo.dirstate
212 212 'a' in dirstate
213 213 def d():
214 214 dirstate._filefoldmap.get('a')
215 215 del dirstate._filefoldmap
216 216 timer(d)
217 217 fm.end()
218 218
219 219 @command('perfdirfoldmap', formatteropts)
220 220 def perfdirfoldmap(ui, repo, **opts):
221 221 timer, fm = gettimer(ui, opts)
222 222 dirstate = repo.dirstate
223 223 'a' in dirstate
224 224 def d():
225 225 dirstate._dirfoldmap.get('a')
226 226 del dirstate._dirfoldmap
227 227 del dirstate._dirs
228 228 timer(d)
229 229 fm.end()
230 230
231 231 @command('perfdirstatewrite', formatteropts)
232 232 def perfdirstatewrite(ui, repo, **opts):
233 233 timer, fm = gettimer(ui, opts)
234 234 ds = repo.dirstate
235 235 "a" in ds
236 236 def d():
237 237 ds._dirty = True
238 238 ds.write(repo.currenttransaction())
239 239 timer(d)
240 240 fm.end()
241 241
242 242 @command('perfmergecalculate',
243 243 [('r', 'rev', '.', 'rev to merge against')] + formatteropts)
244 244 def perfmergecalculate(ui, repo, rev, **opts):
245 245 timer, fm = gettimer(ui, opts)
246 246 wctx = repo[None]
247 247 rctx = scmutil.revsingle(repo, rev, rev)
248 248 ancestor = wctx.ancestor(rctx)
249 249 # we don't want working dir files to be stat'd in the benchmark, so prime
250 250 # that cache
251 251 wctx.dirty()
252 252 def d():
253 253 # acceptremote is True because we don't want prompts in the middle of
254 254 # our benchmark
255 255 merge.calculateupdates(repo, wctx, rctx, [ancestor], False, False,
256 256 acceptremote=True, followcopies=True)
257 257 timer(d)
258 258 fm.end()
259 259
260 260 @command('perfpathcopies', [], "REV REV")
261 261 def perfpathcopies(ui, repo, rev1, rev2, **opts):
262 262 timer, fm = gettimer(ui, opts)
263 263 ctx1 = scmutil.revsingle(repo, rev1, rev1)
264 264 ctx2 = scmutil.revsingle(repo, rev2, rev2)
265 265 def d():
266 266 copies.pathcopies(ctx1, ctx2)
267 267 timer(d)
268 268 fm.end()
269 269
270 270 @command('perfmanifest', [], 'REV')
271 271 def perfmanifest(ui, repo, rev, **opts):
272 272 timer, fm = gettimer(ui, opts)
273 273 ctx = scmutil.revsingle(repo, rev, rev)
274 274 t = ctx.manifestnode()
275 275 def d():
276 repo.manifest._mancache.clear()
277 repo.manifest._cache = None
276 repo.manifest.clearcaches()
278 277 repo.manifest.read(t)
279 278 timer(d)
280 279 fm.end()
281 280
282 281 @command('perfchangeset', formatteropts)
283 282 def perfchangeset(ui, repo, rev, **opts):
284 283 timer, fm = gettimer(ui, opts)
285 284 n = repo[rev].node()
286 285 def d():
287 286 repo.changelog.read(n)
288 287 #repo.changelog._cache = None
289 288 timer(d)
290 289 fm.end()
291 290
292 291 @command('perfindex', formatteropts)
293 292 def perfindex(ui, repo, **opts):
294 293 import mercurial.revlog
295 294 timer, fm = gettimer(ui, opts)
296 295 mercurial.revlog._prereadsize = 2**24 # disable lazy parser in old hg
297 296 n = repo["tip"].node()
298 297 def d():
299 298 cl = mercurial.revlog.revlog(repo.svfs, "00changelog.i")
300 299 cl.rev(n)
301 300 timer(d)
302 301 fm.end()
303 302
304 303 @command('perfstartup', formatteropts)
305 304 def perfstartup(ui, repo, **opts):
306 305 timer, fm = gettimer(ui, opts)
307 306 cmd = sys.argv[0]
308 307 def d():
309 308 if os.name != 'nt':
310 309 os.system("HGRCPATH= %s version -q > /dev/null" % cmd)
311 310 else:
312 311 os.environ['HGRCPATH'] = ''
313 312 os.system("%s version -q > NUL" % cmd)
314 313 timer(d)
315 314 fm.end()
316 315
317 316 @command('perfparents', formatteropts)
318 317 def perfparents(ui, repo, **opts):
319 318 timer, fm = gettimer(ui, opts)
320 319 # control the number of commits perfparents iterates over
321 320 # experimental config: perf.parentscount
322 321 count = ui.configint("perf", "parentscount", 1000)
323 322 if len(repo.changelog) < count:
324 323 raise error.Abort("repo needs %d commits for this test" % count)
325 324 repo = repo.unfiltered()
326 325 nl = [repo.changelog.node(i) for i in xrange(count)]
327 326 def d():
328 327 for n in nl:
329 328 repo.changelog.parents(n)
330 329 timer(d)
331 330 fm.end()
332 331
333 332 @command('perfctxfiles', formatteropts)
334 333 def perfctxfiles(ui, repo, x, **opts):
335 334 x = int(x)
336 335 timer, fm = gettimer(ui, opts)
337 336 def d():
338 337 len(repo[x].files())
339 338 timer(d)
340 339 fm.end()
341 340
342 341 @command('perfrawfiles', formatteropts)
343 342 def perfrawfiles(ui, repo, x, **opts):
344 343 x = int(x)
345 344 timer, fm = gettimer(ui, opts)
346 345 cl = repo.changelog
347 346 def d():
348 347 len(cl.read(x)[3])
349 348 timer(d)
350 349 fm.end()
351 350
352 351 @command('perflookup', formatteropts)
353 352 def perflookup(ui, repo, rev, **opts):
354 353 timer, fm = gettimer(ui, opts)
355 354 timer(lambda: len(repo.lookup(rev)))
356 355 fm.end()
357 356
358 357 @command('perfrevrange', formatteropts)
359 358 def perfrevrange(ui, repo, *specs, **opts):
360 359 timer, fm = gettimer(ui, opts)
361 360 revrange = scmutil.revrange
362 361 timer(lambda: len(revrange(repo, specs)))
363 362 fm.end()
364 363
365 364 @command('perfnodelookup', formatteropts)
366 365 def perfnodelookup(ui, repo, rev, **opts):
367 366 timer, fm = gettimer(ui, opts)
368 367 import mercurial.revlog
369 368 mercurial.revlog._prereadsize = 2**24 # disable lazy parser in old hg
370 369 n = repo[rev].node()
371 370 cl = mercurial.revlog.revlog(repo.svfs, "00changelog.i")
372 371 def d():
373 372 cl.rev(n)
374 373 clearcaches(cl)
375 374 timer(d)
376 375 fm.end()
377 376
378 377 @command('perflog',
379 378 [('', 'rename', False, 'ask log to follow renames')] + formatteropts)
380 379 def perflog(ui, repo, rev=None, **opts):
381 380 if rev is None:
382 381 rev=[]
383 382 timer, fm = gettimer(ui, opts)
384 383 ui.pushbuffer()
385 384 timer(lambda: commands.log(ui, repo, rev=rev, date='', user='',
386 385 copies=opts.get('rename')))
387 386 ui.popbuffer()
388 387 fm.end()
389 388
390 389 @command('perfmoonwalk', formatteropts)
391 390 def perfmoonwalk(ui, repo, **opts):
392 391 """benchmark walking the changelog backwards
393 392
394 393 This also loads the changelog data for each revision in the changelog.
395 394 """
396 395 timer, fm = gettimer(ui, opts)
397 396 def moonwalk():
398 397 for i in xrange(len(repo), -1, -1):
399 398 ctx = repo[i]
400 399 ctx.branch() # read changelog data (in addition to the index)
401 400 timer(moonwalk)
402 401 fm.end()
403 402
404 403 @command('perftemplating', formatteropts)
405 404 def perftemplating(ui, repo, rev=None, **opts):
406 405 if rev is None:
407 406 rev=[]
408 407 timer, fm = gettimer(ui, opts)
409 408 ui.pushbuffer()
410 409 timer(lambda: commands.log(ui, repo, rev=rev, date='', user='',
411 410 template='{date|shortdate} [{rev}:{node|short}]'
412 411 ' {author|person}: {desc|firstline}\n'))
413 412 ui.popbuffer()
414 413 fm.end()
415 414
416 415 @command('perfcca', formatteropts)
417 416 def perfcca(ui, repo, **opts):
418 417 timer, fm = gettimer(ui, opts)
419 418 timer(lambda: scmutil.casecollisionauditor(ui, False, repo.dirstate))
420 419 fm.end()
421 420
422 421 @command('perffncacheload', formatteropts)
423 422 def perffncacheload(ui, repo, **opts):
424 423 timer, fm = gettimer(ui, opts)
425 424 s = repo.store
426 425 def d():
427 426 s.fncache._load()
428 427 timer(d)
429 428 fm.end()
430 429
431 430 @command('perffncachewrite', formatteropts)
432 431 def perffncachewrite(ui, repo, **opts):
433 432 timer, fm = gettimer(ui, opts)
434 433 s = repo.store
435 434 s.fncache._load()
436 435 lock = repo.lock()
437 436 tr = repo.transaction('perffncachewrite')
438 437 def d():
439 438 s.fncache._dirty = True
440 439 s.fncache.write(tr)
441 440 timer(d)
442 441 lock.release()
443 442 fm.end()
444 443
445 444 @command('perffncacheencode', formatteropts)
446 445 def perffncacheencode(ui, repo, **opts):
447 446 timer, fm = gettimer(ui, opts)
448 447 s = repo.store
449 448 s.fncache._load()
450 449 def d():
451 450 for p in s.fncache.entries:
452 451 s.encode(p)
453 452 timer(d)
454 453 fm.end()
455 454
456 455 @command('perfdiffwd', formatteropts)
457 456 def perfdiffwd(ui, repo, **opts):
458 457 """Profile diff of working directory changes"""
459 458 timer, fm = gettimer(ui, opts)
460 459 options = {
461 460 'w': 'ignore_all_space',
462 461 'b': 'ignore_space_change',
463 462 'B': 'ignore_blank_lines',
464 463 }
465 464
466 465 for diffopt in ('', 'w', 'b', 'B', 'wB'):
467 466 opts = dict((options[c], '1') for c in diffopt)
468 467 def d():
469 468 ui.pushbuffer()
470 469 commands.diff(ui, repo, **opts)
471 470 ui.popbuffer()
472 471 title = 'diffopts: %s' % (diffopt and ('-' + diffopt) or 'none')
473 472 timer(d, title)
474 473 fm.end()
475 474
476 475 @command('perfrevlog',
477 476 [('d', 'dist', 100, 'distance between the revisions')] + formatteropts,
478 477 "[INDEXFILE]")
479 478 def perfrevlog(ui, repo, file_, **opts):
480 479 timer, fm = gettimer(ui, opts)
481 480 from mercurial import revlog
482 481 dist = opts['dist']
483 482 _len = getlen(ui)
484 483 def d():
485 484 r = revlog.revlog(lambda fn: open(fn, 'rb'), file_)
486 485 for x in xrange(0, _len(r), dist):
487 486 r.revision(r.node(x))
488 487
489 488 timer(d)
490 489 fm.end()
491 490
492 491 @command('perfrevset',
493 492 [('C', 'clear', False, 'clear volatile cache between each call.'),
494 493 ('', 'contexts', False, 'obtain changectx for each revision')]
495 494 + formatteropts, "REVSET")
496 495 def perfrevset(ui, repo, expr, clear=False, contexts=False, **opts):
497 496 """benchmark the execution time of a revset
498 497
499 498 Use the --clean option if need to evaluate the impact of build volatile
500 499 revisions set cache on the revset execution. Volatile cache hold filtered
501 500 and obsolete related cache."""
502 501 timer, fm = gettimer(ui, opts)
503 502 def d():
504 503 if clear:
505 504 repo.invalidatevolatilesets()
506 505 if contexts:
507 506 for ctx in repo.set(expr): pass
508 507 else:
509 508 for r in repo.revs(expr): pass
510 509 timer(d)
511 510 fm.end()
512 511
513 512 @command('perfvolatilesets', formatteropts)
514 513 def perfvolatilesets(ui, repo, *names, **opts):
515 514 """benchmark the computation of various volatile set
516 515
517 516 Volatile set computes element related to filtering and obsolescence."""
518 517 timer, fm = gettimer(ui, opts)
519 518 repo = repo.unfiltered()
520 519
521 520 def getobs(name):
522 521 def d():
523 522 repo.invalidatevolatilesets()
524 523 obsolete.getrevs(repo, name)
525 524 return d
526 525
527 526 allobs = sorted(obsolete.cachefuncs)
528 527 if names:
529 528 allobs = [n for n in allobs if n in names]
530 529
531 530 for name in allobs:
532 531 timer(getobs(name), title=name)
533 532
534 533 def getfiltered(name):
535 534 def d():
536 535 repo.invalidatevolatilesets()
537 536 repoview.filterrevs(repo, name)
538 537 return d
539 538
540 539 allfilter = sorted(repoview.filtertable)
541 540 if names:
542 541 allfilter = [n for n in allfilter if n in names]
543 542
544 543 for name in allfilter:
545 544 timer(getfiltered(name), title=name)
546 545 fm.end()
547 546
548 547 @command('perfbranchmap',
549 548 [('f', 'full', False,
550 549 'Includes build time of subset'),
551 550 ] + formatteropts)
552 551 def perfbranchmap(ui, repo, full=False, **opts):
553 552 """benchmark the update of a branchmap
554 553
555 554 This benchmarks the full repo.branchmap() call with read and write disabled
556 555 """
557 556 timer, fm = gettimer(ui, opts)
558 557 def getbranchmap(filtername):
559 558 """generate a benchmark function for the filtername"""
560 559 if filtername is None:
561 560 view = repo
562 561 else:
563 562 view = repo.filtered(filtername)
564 563 def d():
565 564 if full:
566 565 view._branchcaches.clear()
567 566 else:
568 567 view._branchcaches.pop(filtername, None)
569 568 view.branchmap()
570 569 return d
571 570 # add filter in smaller subset to bigger subset
572 571 possiblefilters = set(repoview.filtertable)
573 572 allfilters = []
574 573 while possiblefilters:
575 574 for name in possiblefilters:
576 575 subset = branchmap.subsettable.get(name)
577 576 if subset not in possiblefilters:
578 577 break
579 578 else:
580 579 assert False, 'subset cycle %s!' % possiblefilters
581 580 allfilters.append(name)
582 581 possiblefilters.remove(name)
583 582
584 583 # warm the cache
585 584 if not full:
586 585 for name in allfilters:
587 586 repo.filtered(name).branchmap()
588 587 # add unfiltered
589 588 allfilters.append(None)
590 589 oldread = branchmap.read
591 590 oldwrite = branchmap.branchcache.write
592 591 try:
593 592 branchmap.read = lambda repo: None
594 593 branchmap.write = lambda repo: None
595 594 for name in allfilters:
596 595 timer(getbranchmap(name), title=str(name))
597 596 finally:
598 597 branchmap.read = oldread
599 598 branchmap.branchcache.write = oldwrite
600 599 fm.end()
601 600
602 601 @command('perfloadmarkers')
603 602 def perfloadmarkers(ui, repo):
604 603 """benchmark the time to parse the on-disk markers for a repo
605 604
606 605 Result is the number of markers in the repo."""
607 606 timer, fm = gettimer(ui)
608 607 timer(lambda: len(obsolete.obsstore(repo.svfs)))
609 608 fm.end()
610 609
611 610 @command('perflrucachedict', formatteropts +
612 611 [('', 'size', 4, 'size of cache'),
613 612 ('', 'gets', 10000, 'number of key lookups'),
614 613 ('', 'sets', 10000, 'number of key sets'),
615 614 ('', 'mixed', 10000, 'number of mixed mode operations'),
616 615 ('', 'mixedgetfreq', 50, 'frequency of get vs set ops in mixed mode')],
617 616 norepo=True)
618 617 def perflrucache(ui, size=4, gets=10000, sets=10000, mixed=10000,
619 618 mixedgetfreq=50, **opts):
620 619 def doinit():
621 620 for i in xrange(10000):
622 621 util.lrucachedict(size)
623 622
624 623 values = []
625 624 for i in xrange(size):
626 625 values.append(random.randint(0, sys.maxint))
627 626
628 627 # Get mode fills the cache and tests raw lookup performance with no
629 628 # eviction.
630 629 getseq = []
631 630 for i in xrange(gets):
632 631 getseq.append(random.choice(values))
633 632
634 633 def dogets():
635 634 d = util.lrucachedict(size)
636 635 for v in values:
637 636 d[v] = v
638 637 for key in getseq:
639 638 value = d[key]
640 639 value # silence pyflakes warning
641 640
642 641 # Set mode tests insertion speed with cache eviction.
643 642 setseq = []
644 643 for i in xrange(sets):
645 644 setseq.append(random.randint(0, sys.maxint))
646 645
647 646 def dosets():
648 647 d = util.lrucachedict(size)
649 648 for v in setseq:
650 649 d[v] = v
651 650
652 651 # Mixed mode randomly performs gets and sets with eviction.
653 652 mixedops = []
654 653 for i in xrange(mixed):
655 654 r = random.randint(0, 100)
656 655 if r < mixedgetfreq:
657 656 op = 0
658 657 else:
659 658 op = 1
660 659
661 660 mixedops.append((op, random.randint(0, size * 2)))
662 661
663 662 def domixed():
664 663 d = util.lrucachedict(size)
665 664
666 665 for op, v in mixedops:
667 666 if op == 0:
668 667 try:
669 668 d[v]
670 669 except KeyError:
671 670 pass
672 671 else:
673 672 d[v] = v
674 673
675 674 benches = [
676 675 (doinit, 'init'),
677 676 (dogets, 'gets'),
678 677 (dosets, 'sets'),
679 678 (domixed, 'mixed')
680 679 ]
681 680
682 681 for fn, title in benches:
683 682 timer, fm = gettimer(ui, opts)
684 683 timer(fn, title=title)
685 684 fm.end()
General Comments 0
You need to be logged in to leave comments. Login now