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

Hello

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

Hello

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