File: bench_threadpool.py

package info (click to toggle)
python-gevent 24.11.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 20,364 kB
  • sloc: python: 138,768; ansic: 87,807; sh: 12,548; makefile: 2,379; javascript: 108
file content (124 lines) | stat: -rw-r--r-- 2,554 bytes parent folder | download
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
# -*- coding: utf-8 -*-
"""
Benchmarks for thread pool.

"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import pyperf as perf

from gevent.threadpool import ThreadPool

try:
    xrange = xrange
except NameError:
    xrange = range

def noop():
    "Does nothing"

def identity(i):
    return i

PAR_COUNT = 5
N = 20

def bench_apply(loops):
    pool = ThreadPool(1)
    t0 = perf.perf_counter()

    for _ in xrange(loops):
        for _ in xrange(N):
            pool.apply(noop)

    pool.join()
    pool.kill()
    return perf.perf_counter() - t0

def bench_spawn_wait(loops):
    pool = ThreadPool(1)

    t0 = perf.perf_counter()

    for _ in xrange(loops):
        for _ in xrange(N):
            r = pool.spawn(noop)
            r.get()

    pool.join()
    pool.kill()
    return perf.perf_counter() - t0

def _map(pool, pool_func, loops):
    data = [1] * N
    t0 = perf.perf_counter()

    # Must collect for imap to finish
    for _ in xrange(loops):
        list(pool_func(identity, data))

    pool.join()
    pool.kill()
    return perf.perf_counter() - t0

def _ppool():
    pool = ThreadPool(PAR_COUNT)
    pool.size = PAR_COUNT
    return pool

def bench_map_seq(loops):
    pool = ThreadPool(1)
    return _map(pool, pool.map, loops)

def bench_map_par(loops):
    pool = _ppool()
    return _map(pool, pool.map, loops)

def bench_imap_seq(loops):
    pool = ThreadPool(1)
    return _map(pool, pool.imap, loops)

def bench_imap_par(loops):
    pool = _ppool()
    return _map(pool, pool.imap, loops)

def bench_imap_un_seq(loops):
    pool = ThreadPool(1)
    return _map(pool, pool.imap_unordered, loops)

def bench_imap_un_par(loops):
    pool = _ppool()
    return _map(pool, pool.imap_unordered, loops)

def main():
    runner = perf.Runner()

    runner.bench_time_func('imap_unordered_seq',
                           bench_imap_un_seq)

    runner.bench_time_func('imap_unordered_par',
                           bench_imap_un_par)

    runner.bench_time_func('imap_seq',
                           bench_imap_seq)

    runner.bench_time_func('imap_par',
                           bench_imap_par)

    runner.bench_time_func('map_seq',
                           bench_map_seq)

    runner.bench_time_func('map_par',
                           bench_map_par)

    runner.bench_time_func('apply',
                           bench_apply)

    runner.bench_time_func('spawn',
                           bench_spawn_wait)


if __name__ == '__main__':
    main()