~thomasvs/paisley/object-views

« back to all changes in this revision

Viewing changes to paisley_bench.py

  • Committer: David Reid
  • Date: 2008-03-22 06:51:33 UTC
  • Revision ID: dreid@dreid.org-20080322065133-bwvd6lcwy5f3so31
A simple framework for performing benchmarks on paisley+couchdb.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import time
 
2
import sys
 
3
import numpy
 
4
 
 
5
import paisley
 
6
 
 
7
from twisted.internet import reactor
 
8
from twisted.internet.defer import inlineCallbacks, waitForDeferred
 
9
 
 
10
def benchmark(times, timer=time.time, timeStore=None, progressDest=sys.stdout):
 
11
    def _decorator(f):
 
12
        def _decorated(*args, **kwargs):
 
13
            for x in xrange(times):
 
14
                startTime=timer()
 
15
                result = yield f(*args, **kwargs)
 
16
                timeStore.setdefault(f.__name__, []).append(timer()-startTime)
 
17
 
 
18
                if x%(times*.10) == 0.0:
 
19
                    progressDest.write('.')
 
20
                    progressDest.flush()
 
21
            progressDest.write('\n')
 
22
 
 
23
        _decorated.__name__ = f.__name__
 
24
 
 
25
        return inlineCallbacks(_decorated)
 
26
 
 
27
    return _decorator
 
28
 
 
29
RUN_TIMES = 1000
 
30
TIMES = {}
 
31
 
 
32
benchmarkDecorator = benchmark(RUN_TIMES, timeStore=TIMES)
 
33
 
 
34
 
 
35
@benchmarkDecorator
 
36
def bench_saveDoc(server):
 
37
    d = server.saveDoc('benchmarks', """
 
38
        {
 
39
            "Subject":"I like Planktion",
 
40
            "Author":"Rusty",
 
41
            "PostedDate":"2006-08-15T17:30:12-04:00",
 
42
            "Tags":["plankton", "baseball", "decisions"],
 
43
            "Body":"I decided today that I don't like baseball. I like plankton."
 
44
        }
 
45
""")
 
46
    return d
 
47
 
 
48
 
 
49
@inlineCallbacks
 
50
def run_tests(server):
 
51
    for bench in [bench_saveDoc]:
 
52
        print "benchmarking %s" % (bench.__name__,)
 
53
        result = yield bench(server).addCallback(_printCb)
 
54
        print "    avg: %r" % (
 
55
            sum(TIMES[bench.__name__])/len(TIMES[bench.__name__]),)
 
56
        print "    std: %r" % (
 
57
            numpy.std(TIMES[bench.__name__]),)
 
58
        print "    min: %r" % (
 
59
            min(TIMES[bench.__name__]),)
 
60
        print "    max: %r" % (
 
61
            max(TIMES[bench.__name__]),)
 
62
        print "  total: %r" % (
 
63
            sum(TIMES[bench.__name__]),)
 
64
 
 
65
 
 
66
def run():
 
67
    s = paisley.CouchDB('localhost')
 
68
    d = s.createDB('benchmarks')
 
69
    d.addBoth(_printCb)
 
70
    d.addCallback(lambda _: run_tests(s))
 
71
 
 
72
    return d
 
73
 
 
74
 
 
75
def _printCb(msg):
 
76
    if msg is not None:
 
77
        print msg
 
78
 
 
79
 
 
80
if __name__ == '__main__':
 
81
    def _run():
 
82
        d = run()
 
83
        d.addBoth(_printCb)
 
84
        d.addBoth(lambda _: reactor.stop())
 
85
 
 
86
    reactor.callWhenRunning(_run)
 
87
    reactor.run()