~exarkun/+junk/twisted-benchmarks

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
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


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),
    ]



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()
        for i in range(concurrency):
            self._request()
        return self._finished

    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)



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

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



def setup_driver(f, argv, reactor, reporter):
    return perform_benchmark(
        reactor,
        options['duration'], options['iterations'], options['warmup'],
        f, reporter)


def perform_benchmark(reactor, duration, iterations, warmup, f, reporter):
    jobs = [f] * iterations
    d = Deferred()
    def work(res, counter):
        try:
            f = jobs.pop()
        except IndexError:
            d.callback(None)
        else:
            try:
                next = f(reactor, duration)
            except:
                d.errback()
            else:
                if counter <= 0:
                    next.addCallback(reporter, duration, f.__module__)
                next.addCallbacks(work, d.errback, (counter - 1,))
    work(None, warmup)
    return d


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

    def driver(self, f, argv):
        from twisted.internet import reactor

        options = BenchmarkOptions()
        options.parseOptions(argv[1:])

        d = perform_benchmark(
            reactor,
            options['duration'], options['iterations'], options['warmup'],
            f, benchmark_report)
        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):
            return perform_benchmark(
                reactor, duration, iterations, warmup,
                job, self.benchmark_report)

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

        reactor.callWhenRunning(go)
        reactor.run()



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