Finology 大数据金融

通过大数据以量化金融

locust是一款性能测试框架,比JMeter好的地方在于能产生更多的请求,因为locust是基于协程的,而JMeter是基于线程的。

安装

1
pip install locust

安装成功后查看版本信息:

1
2
locust -V
locust 1.4.3

编写代码

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
from locust import HttpUser, TaskSet, task, between, tag

# 模拟n个用户,其实就是创建了n个MyTask的实例
class MyTask(TaskSet):

token = ''

def on_start(self):
self.login_or_register()

def login_or_register(self):
url = 'http://api'
body = {'id': 1}
headers = {'token': 'xxxx'}
with self.client.post(url, json=body, headers=headers, name='login_or_register') as response:
if response.text != '':
res_dict = json.loads(response.text)
if res_dict['code'] == 0:
pass

@task
@tag('view_page')
def view_page(self):
pass


# 这个类是一个配置类, 只被初始化一次, 在这里写一些配置相关信息
# 要访问哪些id, 要生成哪些用户的token, 可以在这里生成
class MyUser(HttpUser):

# 测试的api的host信息
host = ''
# 每次访问之间间隔的时间
wait_time = between(1, 2)

运行

执行命令

1
locust -f file.py

如果只执行带有tag的业务代码

1
locust -f file.py --tags view_page

如果要进行分布式测试,首先启动master,master是管理节点,在这个节点上面查看统计数据。

1
locust -f file.py --tags view_page --master

再开两个worker节点, 如果主节点和从节点不在一台机器上,那需要指定master-host参数。

1
locust -f file.py --tags view_page --worker --master-host=192.168.1.2

当worker节点启动后,master的console里面会显示:

1
2
[2021-03-05 18:42:39,333] /INFO/locust.runners: Client '1140b3a3ec57404194845264ee8dae1f' reported as ready. Currently 1 clients ready to swarm.
[2021-03-05 18:45:13,222] /INFO/locust.runners: Client '8f1e5206c38f4f99a55641f1cf0c44d1' reported as ready. Currently 2 clients ready to swarm.

打开压测页面,http://localhost:8089/

最近在使用一个性能测试框架,locust,是用Python写的。模拟测试逻辑时,可用Python来编写需要进行测试的逻辑。

这篇文章讲讲如何使用 requests 这个库,以发送http请求。

Get请求

1
2
3
4
5
6
7
8
url = 'http://localhost/api'
payload = {'id': 100}
headers = {'token': 'xxxxxxx'}
with self.client.get(url, params=payload, headers=headers) as response:
if response.status_code == 200:
if response.text != '':
res_dict = json.loads(response.text)
...

Post请求

当我们使用json方式提交请求时,其实是不用在代码里面指定Content-type: applicaion/json的,只需要使用json参数就可以了。

1
2
3
4
5
6
7
8
url = 'http://localhost/api'
payload = {'id': 100}
headers = {'token': 'xxxxxxx'}
with self.client.post(url, json=payload, headers=headers) as response:
if response.status_code == 200:
if response.text != '':
res_dict = json.loads(response.text)
...

上传文件有点特殊,也不需要指定Content-type,使用files参数即可。java服务端,使用MultipartFile来接收这个文件即可。

1
2
3
4
5
6
7
8
url = 'http://localhost/api'
file = {'file': open('/opt/test.txt', 'rb')}
headers = {'token': 'xxxxxxx'}
with self.client.post(url, files=file, headers=headers) as response:
if response.status_code == 200:
if response.text != '':
res_dict = json.loads(response.text)
...

这篇文章讲讲在macOS系统中,如何一步一步创建一个基于Anaconda的项目。

首先我们得确定已经在macOS中安装好了Anaconda。

新建Python环境

先查看一下当前环境,只有一个base。

1
2
3
4
$ conda env list
# conda environments:
#
base * /opt/anaconda3

以克隆方式创建一个和Anaconda base(root)环境一样的环境,取名叫py37

1
2
3
$ conda create --name py37 --clone base
Source: /opt/anaconda3
Destination: /opt/anaconda3/envs/py37

按提示激活命令激活环境。

1
$ conda activate py37

新建Python项目

File -> New -> Project…

选择Python项目,New一个Project SDK

添加Python解析器

选择 Conda Environment,使用Conda来管理包。

我们前面使用命令行的方式创建一个env,所以选择Existing Environment。

指定Interpreter,为刚才新建环境py37目录下的python。

/opt/anaconda3/envs/py37/bin/python

然后下一步,确定就创建好了。

检查

创建好项目后,我们写两行python代码。

1
2
3
import pandas as pd

print('hello world')

检查,pandas, print下面没有红线,按Command,同时鼠标点击pandas或print,均能查看得到源代码,运行时,也会正常运行,说明配置一切正常。

我们再看看项目结构。

0%