-
Notifications
You must be signed in to change notification settings - Fork 303
/
performance_testing.py
80 lines (72 loc) · 2.35 KB
/
performance_testing.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
72
73
74
75
76
77
78
79
80
# encoding: utf-8
__author__ = 'zhanghe'
import urllib2
from multiprocessing.dummy import Pool as ThreadPool
from tools import time_log
urls = [
'http://www.python.org',
'http://www.python.org/about/',
'http://www.onlamp.com/pub/a/python/2003/04/17/metaclasses.html',
'http://www.python.org/doc/',
'http://www.python.org/download/',
'http://www.python.org/getit/',
'http://www.python.org/community/',
'https://wiki.python.org/moin/',
'http://planet.python.org/',
'https://wiki.python.org/moin/LocalUserGroups',
'http://www.python.org/psf/',
'http://docs.python.org/devguide/',
'http://www.python.org/community/awards/'
# etc..
]
@time_log.time_log
def work(num=2):
# Make the Pool of workers
pool = ThreadPool(num)
# Open the urls in their own threads
# and return the results
results = pool.map(urllib2.urlopen, urls)
# close the pool and wait for the work to finish
pool.close()
pool.join()
@time_log.time_log
def work2(num=2):
p = ThreadPool(num)
for i in urls:
p.apply_async(urllib2.urlopen, args=(i,))
p.close()
p.join()
if __name__ == '__main__':
work(1)
work(2)
work(4)
work(8)
work2(1)
work2(2)
work2(4)
work2(8)
# 测试结果:
# 方法work开始时间:Thu Apr 30 00:26:21 2015
# 方法work结束时间:Thu Apr 30 00:26:46 2015
# 方法work运行时间:25.28S
# 方法work开始时间:Thu Apr 30 00:26:46 2015
# 方法work结束时间:Thu Apr 30 00:27:04 2015
# 方法work运行时间:18.25S
# 方法work开始时间:Thu Apr 30 00:27:04 2015
# 方法work结束时间:Thu Apr 30 00:27:10 2015
# 方法work运行时间:5.47S
# 方法work开始时间:Thu Apr 30 00:27:10 2015
# 方法work结束时间:Thu Apr 30 00:27:15 2015
# 方法work运行时间:5.16S
# 方法work2开始时间:Thu Apr 30 00:33:56 2015
# 方法work2结束时间:Thu Apr 30 00:34:24 2015
# 方法work2运行时间:27.42S
# 方法work2开始时间:Thu Apr 30 00:34:24 2015
# 方法work2结束时间:Thu Apr 30 00:34:35 2015
# 方法work2运行时间:11.62S
# 方法work2开始时间:Thu Apr 30 00:34:35 2015
# 方法work2结束时间:Thu Apr 30 00:34:42 2015
# 方法work2运行时间:6.87S
# 方法work2开始时间:Thu Apr 30 00:34:42 2015
# 方法work2结束时间:Thu Apr 30 00:34:53 2015
# 方法work2运行时间:11.10S