cmlenz@820: # -*- coding: utf-8 -*- cmlenz@820: # cmlenz@820: # Copyright (C) 2006-2008 Edgewall Software cmlenz@820: # All rights reserved. cmlenz@820: # cmlenz@820: # This software is licensed as described in the file COPYING, which cmlenz@820: # you should have received as part of this distribution. The terms cmlenz@820: # are also available at http://genshi.edgewall.org/wiki/License. cmlenz@820: # cmlenz@820: # This software consists of voluntary contributions made by many cmlenz@820: # individuals. For the exact contribution history, see the revision cmlenz@820: # history and logs, available at http://genshi.edgewall.org/log/. cmlenz@820: cmlenz@820: cmlenz@820: try: cmlenz@820: from os import times cmlenz@820: def time_func(): cmlenz@820: tup = times() cmlenz@820: #just user time cmlenz@820: return tup[0] # + tup[1] cmlenz@820: except ImportError: cmlenz@820: from time import time as time_func cmlenz@820: cmlenz@820: from genshi.core import START, END cmlenz@820: from genshi.path import Path cmlenz@820: from genshi.input import XML cmlenz@820: cmlenz@820: def benchmark(f, acurate_time=1): cmlenz@820: """Checks how much time does function f work. It runs it as cmlenz@820: many times as needed for avoiding inaccuracy""" cmlenz@820: cmlenz@820: runs = 1 cmlenz@820: while True: cmlenz@820: start_time = time_func() cmlenz@820: for _ in xrange(runs): cmlenz@820: f() cmlenz@820: dt = time_func() - start_time cmlenz@820: if dt >= acurate_time: cmlenz@820: break cmlenz@820: runs *= 2 cmlenz@820: return dt / runs cmlenz@820: cmlenz@820: def spell(t): cmlenz@820: """Returns spelled representation of time""" cmlenz@820: units = [(0.000001, 'microsecond', 'microseconds'), cmlenz@820: (0.001, 'milisecond', 'miliseconds'), cmlenz@820: (1, 'second', 'seconds'), cmlenz@820: (60, 'minute', 'minutes'), cmlenz@820: (60*60, 'hour', 'hours'), cmlenz@820: ] cmlenz@820: i = 0 cmlenz@820: at = abs(t) cmlenz@820: while i + 1 < len(units) and at >= units[i + 1][0]: cmlenz@820: i += 1 cmlenz@820: t /= units[i][0] cmlenz@820: if t >= 2: cmlenz@820: name = units[i][2] cmlenz@820: else: cmlenz@820: name = units[i][1] cmlenz@820: return "%f %s"%(t, name) cmlenz@820: cmlenz@820: def test_paths_in_streams(exprs, streams, test_strategies=False): cmlenz@820: for expr in exprs: cmlenz@820: print "Testing path %r" % expr cmlenz@820: for stream, sname in streams: cmlenz@820: print '\tRunning on "%s" example:' % sname cmlenz@820: cmlenz@820: path = Path(expr) cmlenz@820: def f(): cmlenz@820: for e in path.select(stream): cmlenz@820: pass cmlenz@820: t = spell(benchmark(f)) cmlenz@820: print "\t\tselect:\t\t%s" % t cmlenz@820: cmlenz@820: def f(): cmlenz@820: path = Path(expr) cmlenz@820: for e in path.select(stream): cmlenz@820: pass cmlenz@820: t = spell(benchmark(f)) cmlenz@820: print "\t\tinit + select:\t%s" % t cmlenz@820: cmlenz@820: if test_strategies and len(path.paths) == 1: cmlenz@820: from genshi.path import GenericStrategy, SingleStepStrategy, \ cmlenz@820: SimplePathStrategy cmlenz@820: from genshi.tests.path import FakePath cmlenz@820: strategies = (GenericStrategy, SingleStepStrategy, cmlenz@820: SimplePathStrategy) cmlenz@820: for strategy in strategies: cmlenz@820: if not strategy.supports(path.paths[0]): cmlenz@820: continue cmlenz@820: print "\t\t%s Strategy"%strategy.__name__ cmlenz@820: fp = FakePath(strategy(path.paths[0])) cmlenz@820: def f(): cmlenz@820: for e in fp.select(stream): cmlenz@820: pass cmlenz@820: t = spell(benchmark(f)) cmlenz@820: print "\t\t\tselect:\t\t%s"%t cmlenz@820: cmlenz@820: cmlenz@820: def test_documents(test_strategies=False): cmlenz@820: streams = [] cmlenz@820: cmlenz@820: s = XML("""\ cmlenz@820: cmlenz@820: cmlenz@820: cmlenz@820: Foo cmlenz@820: cmlenz@820: cmlenz@820:

Hello

cmlenz@820: cmlenz@820: cmlenz@820: """) cmlenz@820: streams.append((s, "small document")) cmlenz@820: cmlenz@820: s = XML("""\ cmlenz@820: cmlenz@820: cmlenz@820: cmlenz@820: Foo cmlenz@820: cmlenz@820: cmlenz@820:

Hello

cmlenz@820:
cmlenz@820: cmlenz@820:
cmlenz@820: cmlenz@820: Trac Web-based lightweight project management cmlenz@820: system cmlenz@820: cmlenz@820:
cmlenz@820:
cmlenz@820: cmlenz@820: cmlenz@820: """) cmlenz@820: streams.append((s, "big document")) cmlenz@820: cmlenz@820: paths = [ cmlenz@820: '.', cmlenz@820: '*|text()', cmlenz@820: 'html', cmlenz@820: 'html[@lang="en"]', cmlenz@820: 'html/body/h1/text()', cmlenz@820: 'html/body/div/a/@href', cmlenz@820: 'html/body/div[@id="splash"]/a[@class="b4"]/strong/text()', cmlenz@820: 'descendant-or-self::text()', cmlenz@820: 'descendant-or-self::h1/text()', cmlenz@820: ] cmlenz@820: test_paths_in_streams(paths, streams, test_strategies) cmlenz@820: cmlenz@820: if __name__ == '__main__': cmlenz@820: from sys import argv cmlenz@820: if "--strategies" in argv: cmlenz@820: test_strategies = True cmlenz@820: else: cmlenz@820: test_strategies = False cmlenz@820: test_documents(test_strategies)