locus分布式压测docker环境搭建

单机版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
version: '2'

services:
master:
image: locustio/locust
ports:
- "8089:8089"
volumes:
- ./:/mnt/locust
#command: -f /mnt/locust/locustfile_new.py --master -H http://0.0.0.0:8089
command: -f /mnt/locust/locustfile_epp.py --master --web-host=0.0.0.0 --web-port=8089
worker:
image: locustio/locust
volumes:
- ./:/mnt/locust
command: -f /mnt/locust/locustfile_epp.py --worker --master-host master

多机版

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
version: '3.7'

services:
master:
image: locustio/locust
restart: always
networks:
overlay:
ports:
- "8089:8089"
volumes:
- /home/jinri/locust:/mnt/locust
deploy:
mode: replicated
replicas: 1
placement:
constraints: [node.hostname == swarm_17 ]
command: -f /mnt/locust/locustfile_epp.py --master --web-host=0.0.0.0 --web-port=8089
tty: true


worker:
image: locustio/locust
restart: always
networks:
overlay:
volumes:
- /home/jinri/locust:/mnt/locust
deploy:
mode: replicated
replicas: 40
#mode: global
command: -f /mnt/locust/locustfile_epp.py --worker --master-host master
tty: true

networks:
overlay:
driver: overlay

压测脚本

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
# locust v1.0+版本依赖python3.6
from locust import HttpUser, task, constant
import queue
# from multiprocessing import Queue

def get_txt_data():
with open("/mnt/locust/md5sha1.txt", "r") as f:
#data = f.read()
data = f.readlines()
return data

def queue_data():
queue_data = queue.Queue() # 默认为先进先出 该队列为task_set共享
# queue.LifoQueue(),后进先出
lines = get_txt_data()
for i in lines:
queue_data.put_nowait(i) # put_nowait 不阻塞
return queue_data

class QuickstartUser(HttpUser):
# 接口地址
host="http://xxxx:8360"

# 单个并发内调用间隔
wait_time = constant(0)
#queue_data = queue_data()
data_q = queue_data()

# @task(1)
# def baidu_index(self):
# self.client.get("/")

@task(1)
def ping(self):
#self.client.get("/ping")
try:
keys = self.data_q.get()
print(keys)
d = {'keys': keys, 'scanner': 'AVAST,QVM,BITDEFENDER,KASPERSKY,QEX,AVE,AVIRA'}
except queue.Empty:
print('queue_data is empty')
exit(0)
r = self.client.post("/http_scanhash/scanhash.php",
data=d)
self.data_q.put_nowait(keys)
print(keys,r.text)
assert r.status_code == 200