最近一个项目需要用30个左右的jmeter客户端来做压力测试,需要做一些部署和配置工作,这时我喜欢的Fabric就可以发挥它的力量了。其中,我希望将jmeter施压段放到后台去运行,最开始将“nohup xxx &”放到fabric的run()中执行时,却没有执行;后来看了下文档,并做了些实验。
对于直接使用“&”放到后台执行的情况,Fabric推荐使用如下3种情况来替代(按鲁棒性从高到低排列):
1.使用已经存在daemon技术,如init、supervisord、upstart、systemd等,例如:/etc/init.d/tomcat restart 这样的命令,另外supervisord我也是很喜欢的。
2.使用screen、tmux、dtach等工具来从当前shell中detach进程。如:screen -d -m sleep 10 这样的命令。注:要设置pty=False,如 run(‘screen -d -m sleep 10’, pty=False)。
3.使用nohup,不过确实如官方文档所说只有部分用户能成功,我用nohup在run()中时就遇到了问题后该用screen了。
共享一个前几天写的jmeter相关的fabfile吧: https://github.com/smilejay/python/blob/master/py2015/unified_order_fabfile.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
|
#!/usr/bin/env python
# a fabfile to manange the performance test for unified order project.
# usage: fab -f unified_order_fabfile.py start_jmeter -P -z 30
# author: Jay
from
fabric
.
context_managers
import
cd
from
fabric
.
operations
import
run
,
put
from
fabric
.
api
import
task
,
env
env
.
hosts
=
[
'192.168.1.2'
,
'192.168.1.3'
,
'192.168.1.4'
]
env
.
port
=
22
env
.
user
=
'root'
env
.
password
=
'123456'
@
task
def
hostname
(
)
:
# show hostname # just for testing
with
cd
(
'/tmp'
)
:
run
(
'hostname'
)
@
task
def
copy_jmeter
(
)
:
# copy jmeter to other machines
with
cd
(
'/tmp'
)
:
run
(
'rm -rf jakarta-jmeter-2.3.4'
)
put
(
'jakarta-jmeter-2.3.4'
,
'/tmp/'
)
run
(
'cd jakarta-jmeter-2.3.4/bin; chmod a+x jmeter'
)
#run('ls /tmp/')
@
task
def
start_jmeter
(
)
:
# run jmeter in all test clients
#with cd('/tmp/'):
with
cd
(
'/tmp/jakarta-jmeter-2.3.4/bin/'
)
:
run
(
'screen -d -m ./jmeter -n -t my-order.jmx -l log.jtl &>abc.log'
)
#run('./jmeter -n -t unified-order.jmx -l log.jtl &>abc.log')
#run('screen -d -m sleep 10', pty=False)
#run('service tomcat start', pty=False)
@
task
def
kill_jmeter
(
)
:
# kill the jmeter processes for unified order project
with
cd
(
'/tmp/'
)
:
pids
=
run
(
"ps -ef | grep unified | grep -v 'grep' | awk '{print $2'}"
)
pid_list
=
pids
.
split
(
'\r\n'
)
for
i
in
pid_list
:
run
(
'kill -9 %s'
%
i
)
@
task
def
get_status
(
)
:
# get jmeter(java) running status
with
cd
(
'/tmp'
)
:
run
(
'ps -ef | grep unified | grep java | grep -v grep'
)
|
参考文档:http://fabric.readthedocs.org/en/1.6/faq.html