项目目录如下:
- coll
- test_op.py # 压测脚本
- socket_client.py # 自定义socket客户端
- work_id.txt # 测试数据
- compose
- Dockerfile
- pip.conf
- requirements.txt
- docker-compose.yml
#pip.conf 文件
[global]
timeout = 3600
index-url = http://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
disable-pip-version-check = true
# Dockerfile
FROM python:3.7
# 设置 python 环境变量
ENV PYTHONUNBUFFERED 1
ENV APT_KEY_DONT_WARN_ON_DANGEROUS_USAGE=DontWarn
COPY pip.conf /root/.pip/pip.conf
# 创建 locust 文件夹
RUN mkdir -p /mnt/locust
# 将 locust 文件夹为工作目录
WORKDIR /mnt/locust
# 将当前目录加入到工作目录中(. 表示当前目录)
ADD . /mnt/locust
# 更新pip版本
RUN /usr/local/bin/python3 -m pip install --upgrade pip
# 利用 pip 安装依赖(websocket库放在requirements文件安装无效,不知道为啥...)
RUN pip3 install -r requirements.txt
RUN pip3 install websocket
RUN pip3 install websocket-client
# docker-compose.yml
version: '3'
networks: # 自定义网络(默认桥接), 不使用links通信
coll_network:
driver: bridge
services:
master:
build: ./compose
networks:
- coll_network
ports:
- "8089:8089"
volumes:
- ./coll:/mnt/locust
command: locust -f test_op.py --master --web-host=0.0.0.0
worker:
build: ./compose
networks:
- coll_network
volumes:
- ./coll:/mnt/locust
command: locust -f test_op.py --worker --master-host master
depends_on:
- master
docker compose build构建项目
docker compose up —scale worker=4启动一个主节点和四个工作节点