File: context.py

package info (click to toggle)
python-eventlet 0.40.1-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,200 kB
  • sloc: python: 25,101; sh: 78; makefile: 31
file content (95 lines) | stat: -rw-r--r-- 2,241 bytes parent folder | download | duplicates (3)
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
"""Test context switching performance of threading and eventlet"""

import threading
import time

import eventlet
import eventlet.hubs


CONTEXT_SWITCHES = 100000


def run(event, wait_event):
    counter = 0
    while counter <= CONTEXT_SWITCHES:
        wait_event.wait()
        wait_event.reset()
        counter += 1
        event.send()


def test_eventlet():
    event1 = eventlet.event.Event()
    event2 = eventlet.event.Event()
    event1.send()
    thread1 = eventlet.spawn(run, event1, event2)
    thread2 = eventlet.spawn(run, event2, event1)

    thread1.wait()
    thread2.wait()


class BenchThread(threading.Thread):
    def __init__(self, event, wait_event):
        threading.Thread.__init__(self)
        self.counter = 0
        self.event = event
        self.wait_event = wait_event

    def run(self):
        while self.counter <= CONTEXT_SWITCHES:
            self.wait_event.wait()
            self.wait_event.clear()
            self.counter += 1
            self.event.set()


def test_thread():
    event1 = threading.Event()
    event2 = threading.Event()
    event1.set()
    thread1 = BenchThread(event1, event2)
    thread2 = BenchThread(event2, event1)
    thread1.start()
    thread2.start()
    thread1.join()
    thread2.join()


print("Testing with %d context switches" % CONTEXT_SWITCHES)
start = time.time()
test_thread()
print("threading: %.02f seconds" % (time.time() - start))

try:
    eventlet.hubs.use_hub("eventlet.hubs.epolls")
    start = time.time()
    test_eventlet()
    print("epoll:     %.02f seconds" % (time.time() - start))
except:
    print("epoll hub unavailable")

try:
    eventlet.hubs.use_hub("eventlet.hubs.kqueue")
    start = time.time()
    test_eventlet()
    print("kqueue:    %.02f seconds" % (time.time() - start))
except:
    print("kqueue hub unavailable")

try:
    eventlet.hubs.use_hub("eventlet.hubs.poll")
    start = time.time()
    test_eventlet()
    print("poll:      %.02f seconds" % (time.time() - start))
except:
    print("poll hub unavailable")

try:
    eventlet.hubs.use_hub("eventlet.hubs.selects")
    start = time.time()
    test_eventlet()
    print("select:    %.02f seconds" % (time.time() - start))
except:
    print("select hub unavailable")