forked from ikegami-yukino/mprpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbenchmark.py
71 lines (45 loc) · 1.48 KB
/
benchmark.py
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
# -*- coding: utf-8 -*-
from __future__ import print_function
import time
import multiprocessing
import sys
if sys.version_info < (3,):
range = xrange
else:
import builtins
range = builtins.range
NUM_CALLS = 10000
def run_sum_server():
from gevent.server import StreamServer
from mprpc import RPCServer
class SumServer(RPCServer):
def sum(self, x, y):
return x + y
server = StreamServer(('127.0.0.1', 6000), SumServer())
server.serve_forever()
def call():
from mprpc import RPCClient
client = RPCClient('127.0.0.1', 6000)
start = time.time()
[client.call('sum', 1, 2) for _ in range(NUM_CALLS)]
print('call: %d qps' % (NUM_CALLS / (time.time() - start)))
def call_using_connection_pool():
from mprpc import RPCPoolClient
import gevent.pool
import gsocketpool.pool
def _call(n):
with client_pool.connection() as client:
return client.call('sum', 1, 2)
options = dict(host='127.0.0.1', port=6000)
client_pool = gsocketpool.pool.Pool(RPCPoolClient, options, initial_connections=20)
glet_pool = gevent.pool.Pool(20)
start = time.time()
[None for _ in glet_pool.imap_unordered(_call, range(NUM_CALLS))]
print('call_using_connection_pool: %d qps' % (NUM_CALLS / (time.time() - start)))
if __name__ == '__main__':
p = multiprocessing.Process(target=run_sum_server)
p.start()
time.sleep(1)
call()
call_using_connection_pool()
p.terminate()