~twisted-dev/twisted-benchmarks/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
from __future__ import division

import sys

from twisted.python import log
from twisted.internet.defer import Deferred
from twisted.internet.task import cooperate
from twisted.application.app import ReactorSelectionMixin
from twisted.python.usage import Options


PRINT_TEMPL = (
    '%(stats)s %(name)s/sec (%(count)s %(name)s in %(duration)s seconds)')

class BenchmarkOptions(Options, ReactorSelectionMixin):
    optParameters = [
        ('iterations', 'n', 1, 'number of iterations', int),
        ('duration', 'd', 5, 'duration of each iteration', float),
        ('warmup', 'w', 0, 'number of warmup iterations', int),
        ('format', 'f',
         PRINT_TEMPL,
         'format string to use to report the benchmark results '
         '(valid keys: name stats count duration)'),
    ]



class Client(object):
    def __init__(self, reactor):
        self._reactor = reactor
        self._requestCount = 0


    def run(self, concurrency, duration):
        self._reactor.callLater(duration, self._stop, None)
        self._finished = Deferred()
        self._finished.addBoth(self._cleanup)
        for i in range(concurrency):
            self._request()
        return self._finished


    def cleanup(self):
        pass


    def _continue(self, ignored):
        self._requestCount += 1
        if self._finished is not None:
            self._request()


    def _stop(self, reason):
        if self._finished is not None:
            finished = self._finished
            self._finished = None
            if reason is not None:
                finished.errback(reason)
            else:
                finished.callback(self._requestCount)


    def _cleanup(self, passthrough):
        self.cleanup()
        return passthrough




def benchmark_report(acceptCount, duration, name, format):
    print format % {
        'stats'    : acceptCount / duration,
        'name'     : name,
        'count'    : acceptCount,
        'duration' : duration
        }



def perform_benchmark(reactor, duration, iterations, warmup, f):
    warmups = cooperate(f(reactor, duration) for i in range(warmup)).whenDone()
    warmups.addCallback(lambda ignored: f(reactor, duration))
    yield warmups
    for i in range(iterations - 1):
        yield f(reactor, duration)



def report_benchmark(results, duration, f, reporter, format):
    reporting = cooperate(
        result.addCallback(reporter, duration, f.__module__, format)
        for result in results)
    return reporting.whenDone()



class Driver(object):
    benchmark_report = staticmethod(benchmark_report)

    def driver(self, f, argv):
        options = BenchmarkOptions()
        options.parseOptions(argv[1:])

        from twisted.internet import reactor

        duration = options['duration']
        iterations = options['iterations']
        warmup = options['warmup']
        format = options['format']
        d = report_benchmark(
            perform_benchmark(reactor, duration, iterations, warmup, f),
            duration, f, self.benchmark_report, format)
        d.addErrback(log.err)
        reactor.callWhenRunning(d.addBoth, lambda ign: reactor.stop())
        reactor.run()


    def multidriver(self, f):
        options = BenchmarkOptions()
        options.parseOptions(sys.argv[1:])
        self.run_jobs(
            f, options['duration'], options['iterations'], options['warmup'])


    def run_jobs(self, f, duration, iterations, warmup):
        from twisted.internet import reactor

        def work(job):
            d = perform_benchmark(
                reactor, duration, iterations, warmup,
                job, self.benchmark_report)
            d.addErrback(
                log.err, "Problem running benchmark %s" % (job.__module__,))
            return d

        def go():
            task = cooperate(work(job) for job in f)
            d = task.whenDone()
            d.addErrback(log.err, "Problem coordinating benchmarks")
            d.addCallback(lambda ignored: reactor.stop())

        reactor.callWhenRunning(go)
        reactor.run()



_driver = Driver()
driver = _driver.driver
multidriver = _driver.multidriver
del _driver