forked from donspaulding/gevent-redis
-
Notifications
You must be signed in to change notification settings - Fork 1
/
benchmark.py
57 lines (51 loc) · 1.6 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
#!/usr/bin/env python
import time
CONNECTIONS = 10
INCR_NUMBER = 100
def test_redispy():
import threading, redis
import gevent.monkey
gevent.monkey.patch_all()
redis_client = redis.Redis()
redis_client.delete('test')
del redis_client
time_begin = time.time()
print 'test_redispy begin', time_begin
def worker():
redis_client = redis.Redis()
for i in xrange(INCR_NUMBER):
redis_client.lpush('test', i)
redis_client.lrange('test', 0, -1)
jobs = [threading.Thread(target=worker) for i in xrange(CONNECTIONS)]
for job in jobs:
job.start()
for job in jobs:
job.join()
time_end = time.time()
print 'test_redispy end', time_end
print 'test_redispy total', time_end - time_begin
def test_geventredis():
import gevent, geventredis
redis_client = geventredis.connect()
redis_client.delete('test')
del redis_client
time_begin = time.time()
print 'test_geventredis begin', time_begin
def worker():
redis_client = geventredis.connect()
for i in xrange(INCR_NUMBER):
redis_client.lpush('test', i)
redis_client.lrange('test', 0, -1)
jobs = [gevent.spawn(worker) for i in xrange(CONNECTIONS)]
gevent.joinall(jobs)
time_end = time.time()
print 'test_geventredis end', time_end
print 'test_geventredis total', time_end - time_begin
def test():
print '-----------------------------'
test_geventredis()
print '-----------------------------'
test_redispy()
print '-----------------------------'
if __name__ == '__main__':
test()