我是听朋友提起的"蝗虫"(Locust),然而她不想用python,我就拿来试一试~

http的 各种压测工具也已经太多了,所以主要是试试locust在当前比较流行的rpc协议上的效果

目的 -- 调研locust应用于grpc协议

服务 -- grpc的helloworld

一  环境准备

1 需要python环境,我使用的是python2.7

2 安装locust

pip install locustio
pip install pyzmq // 分布式运行多个起压locust时使用
pip install gevent==1.1 // 据说是因为升级后的gevent不能很好的支持python2系列?
locust -h                // 查看可用参数

二 被测服务编写

1 windows的web界面测试

① python代码,写好保存在为test_001.py

from locust import HttpLocust,TaskSet,task
import subprocess
import json class UserBehavior(TaskSet):
def on_start(self):
pass @task()
def list_header(self):
r = self.client.get("/") class WebUserLocust(HttpLocust):
weight =
task_set = UserBehavior
host = "http://www.baidu.com"
min_wait =
max_wait =

② 在命令行执行 

locust -f E:\Work\PyCharmTest\locusttest\test_001.py

③ 打开浏览器,输入网址:localhost:8089       (locust 的默认监听端口为8089)

在显示页面输入模拟用户数和每个用户的req/s,点击 start swarming 即可开始 

④ 需要手动结束,在页面点击stop或者在命令行ctrl+c退出locust启动命令,即可查看结果如下:

显示总请求数、失败请求数、请求的耗时分布、qps、传输字节数等

2 linux运行

① python代码 -- 此处写的proto文件,helloworld

syntax = "proto3";
option java_package = "io.grpc.examples";
package helloworld; // The greeter service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
} // The request meesage containing the user'name.
message HelloRequest {
string name = ;
} // The response message containing the greetings
message HelloReply {
string message = ;
}

② 转成python

执行如下命令,生成helloworld_pb2.py和helloworld_pb2_grpc两个文件

protoc  --python_out=. helloworld.proto

③ 启动helloworld的server(该部分随意,根据实际设置)

该部分用的python的server,启动端口设置为50052

import sys
import grpc
import time
sys.path.append("..") from concurrent import futures
from helloworld.helloworld import helloworld_pb2, helloworld_pb2_grpc _ONE_DAY_IN_SECONDS = * *
_HOST = 'localhost'
_PORT = ''
class Greeter(helloworld_pb2_grpc.GreeterServicer):
def SayHello(self, request, context):
str = request.name
return helloworld_pb2.HelloReply(message='hello, '+str) def serve():
grpcServer = grpc.server(futures.ThreadPoolExecutor(max_workers=))
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), grpcServer)
grpcServer.add_insecure_port(_HOST + ':' + _PORT)
grpcServer.start() try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
grpcServer.stop() if __name__ == '__main__':
serve()

④ 编写locust文件,对端口50052的服务进行压测,locust文件如下:

我抄的,从这儿:http://codegist.net/code/grpc%20locust/

#!/usr/bin/env python 2.7
# -*- coding:utf- -*- import os,sys, argparse
sys.path.append("..")
import grpc
import json
import requests
import time
import random
import string from helloworld.helloworld import helloworld_pb2, helloworld_pb2_grpc
from locust import Locust, TaskSet, task, events
from locust.stats import RequestStats
from locust.events import EventHook def parse_arguments():
parser = argparse.ArgumentParser(prog='locust')
parser.add_argument('--hatch_rate')
parser.add_argument('--master', action='store_true')
args, unknown = parser.parse_known_args()
opts = vars(args)
return args.host, int(args.clients), int(args.hatch_rate) #HOST, MAX_USERS_NUMBER, USERS_PRE_SECOND = parse_arguments()
HOST, MAX_USERS_NUMBER, USERS_PRE_SECOND = '127.0.0.1:50052', ,

# 分布式,需要启多台压测时,有一个master,其余为slave
slaves_connect = []
slave_report = EventHook()
ALL_SLAVES_CONNECTED = True
SLAVES_NUMBER =
def on_my_event(client_id, data):
global ALL_SLAVES_CONNECTED
if not ALL_SLAVES_CONNECTED:
slaves_connect.append(client_id)
if len(slaves_connect) == SLAVES_NUMBER:
print "All Slaves Connected"
ALL_SLAVES_CONNECTED = True
# print events.slave_report._handlers
header = {'Content-Type': 'application/x-www-form-urlencoded'}
import resource rsrc = resource.RLIMIT_NOFILE
soft, hard = resource.getrlimit(rsrc)
print 'RLIMIT_NOFILE soft limit starts as : ', soft soft, hard = resource.getrlimit(rsrc)
print 'RLIMIT_NOFILE soft limit change to: ', soft events.slave_report += on_my_event class GrpcLocust(Locust):
def __init__(self, *args, **kwargs):
super(GrpcLocust, self).__init__(*args, **kwargs) class ApiUser(GrpcLocust):
min_wait =
max_wait =
stop_timeout = class task_set(TaskSet):
def getEnviron(self, key, default):
if key in os.environ:
return os.environ[key]
else:
return default def getToken(self):
consumer_key = self.getEnviron('SELDON_OAUTH_KEY', 'oauthkey')
consumer_secret = self.getEnviron('SELDON_OAUTH_SECRET', 'oauthsecret')
params = {} params["consumer_key"] = consumer_key
params["consumer_secret"] = consumer_secret url = self.oauth_endpoint + "/token"
r = requests.get(url, params=params)
if r.status_code == requests.codes.ok:
j = json.loads(r.text)
# print j
return j["access_token"]
else:
print "failed call to get token"
return None def on_start(self):
# print "on start"
# self.oauth_endpoint = self.getEnviron('SELDON_OAUTH_ENDPOINT', "http://127.0.0.1:50053")
# self.token = self.getToken()
self.grpc_endpoint = self.getEnviron('SELDON_GRPC_ENDPOINT', "127.0.0.1:50052")
self.data_size = int(self.getEnviron('SELDON_DEFAULT_DATA_SIZE', "")) @task
def get_prediction(self):
conn = grpc.insecure_channel(self.grpc_endpoint)
client = helloworld_pb2_grpc.GreeterStub(conn)
start_time = time.time()
name = string.join(random.sample(['a','b','c','d','e','f','g'], )).replace(" ","")
request = helloworld_pb2.HelloRequest(name=name)
try:
reply = client.SayHello(request)
except Exception,e:
total_time = int((time.time() - start_time) * )
events.request_failure.fire(request_type="grpc", name=HOST, response_time=total_time, exception=e)
else:
total_time = int((time.time() - start_time) * )
events.request_success.fire(request_type="grpc", name=HOST, response_time=total_time, response_length=)

⑤ 启动locust,查看结果

locust --help可以看到命令的使用帮助。注意 -c -n -r 参数的使用需要配合 --no-web

三 问题及解决

问题倒是有,但是我没解决,我不知道这个起压的req怎么设置的

原本以为 -c  模拟用户数  -r 每个用户发出的秒请求数 --- 那么服务每秒承受的压力就应该为-c * -r

但是我跑出来的结果确实这样的,特别像是-c * 10 ,大脸懵逼了

 

压测工具Locuse的使用的更多相关文章

  1. Http压测工具wrk使用指南

    用过了很多压测工具,却一直没找到中意的那款.最近试了wrk感觉不错,写下这份使用指南给自己备忘用,如果能帮到你,那也很好. 安装 wrk支持大多数类UNIX系统,不支持windows.需要操作系统支持 ...

  2. web压测工具http_load原理分析

    一.前言 http_load是一款测试web服务器性能的开源工具,从下面的网址可以下载到最新版本的http_load: http://www.acme.com/software/http_load/ ...

  3. [软件测试]网站压测工具Webbench源码分析

    一.我与webbench二三事 Webbench是一个在linux下使用的非常简单的网站压测工具.它使用fork()模拟多个客户端同时访问我们设定的URL,测试网站在压力下工作的性能.Webbench ...

  4. Http压测工具wrk使用指南【转】

    用过了很多压测工具,却一直没找到中意的那款.最近试了wrk感觉不错,写下这份使用指南给自己备忘用,如果能帮到你,那也很好. 安装 wrk支持大多数类UNIX系统,不支持windows.需要操作系统支持 ...

  5. python服务端多进程压测工具

    本文描述一个python实现的多进程压测工具,这个压测工具的特点如下: 多进程 在大多数情况下,压测一般适用于IO密集型场景(如访问接口并等待返回),在这种场景下多线程多进程的区分并不明显(详情请参见 ...

  6. 内存压测工具Memtester

    在做压力测试时,发现一个内存压测工具Memtester,可以随意设置内存占用大小,非常方便 下载地址:http://pyropus.ca/software/memtester/old-versions ...

  7. Http 压测工具 wrk 基本使用

    Http 压测工具 wrk 基本使用 Intro wrk 是一款现代HTTP基准测试工具,能够在单个多核CPU上运行时产生显着负载.它将多线程设计与可扩展事件通知系统(如epoll和kqueue)结合 ...

  8. 压测工具使用(vegeta)

    一.压测工具vegeta 1.介绍 Vegeta 是一个用 Go 语言编写的多功能的 HTTP 负载测试工具,它提供了命令行工具和一个开发库. 官方地址:https://github.com/tsen ...

  9. 003_ab http压测工具

    一. ab -r -k -c 20000 -n 25000000 https://www.uuwatch.me/abtest #ab压测工具单机最大并发为20000,可以在多台机器上执行以增大并发 y ...

随机推荐

  1. 解决RTMP推送时间戳问题引起HLS切片不均匀导致手机浏览器播放卡顿的问题

    本文转自EasyDarwin开源团队成员Kim的博客:http://blog.csdn.net/jinlong0603/article/details/74161115 引言 最近在测试EasyNVR ...

  2. Vim技能修炼教程(10) - 代码跳转

    程序员功能 前面我们用了5讲的篇幅来讲基本编辑的基本功:第4讲是基本操作,第5讲是操作符,第6讲行编辑ex命令,第7讲可视模式,第8讲多窗口,第9讲缓冲区和标签页. 从这一讲开始,我们从通用功能向程序 ...

  3. uglify-es 解决webpack 不能压缩es6 的问题

    https://www.npmjs.com/package/uglify-es

  4. Scala函数式编程——近半年的痛并快乐着

    从9月初啃完那本让人痛不欲生却又欲罢不能的<七周七并发模型>,我差不多销声匿迹了整整4个月.这几个月里,除了忙着讨食,便是继续啃另一本"锯著"--<Scala函数 ...

  5. BDD测试框架Spock概要

    前言 为了找到一个适合自己的.更具操作性的.以DDD为核心的开发方法,我最近一直在摸索如何揉合BDD与DDD.围绕这个目标,我找到了Impact Mapping → Cucumber → Spock ...

  6. 实战maven私有仓库三部曲之二:上传到私有仓库

    在上一章<实战maven私有仓库三部曲之一:搭建和使用>我们搭建了maven私有仓库,并体验了私有仓库缓存jar包的能力,避免了局域网内开发人员去远程中央仓库下载的痛苦等待,本章我们再来体 ...

  7. BZOJ1220 HNOI2002 跳蚤 【容斥原理+高精度】*

    BZOJ1220 HNOI2002 跳蚤 Description Z城市居住着很多只跳蚤.在Z城市周六生活频道有一个娱乐节目.一只跳蚤将被请上一个高空钢丝的正中央.钢丝很长,可以看作是无限长.节目主持 ...

  8. Codeforces 954H Path Counting 【DP计数】*

    Codeforces 954H Path Counting LINK 题目大意:给你一棵n层的树,第i层的每个节点有a[i]个儿子节点,然后问你树上的简单路径中长度在1~n*2-2之间的每个有多少条 ...

  9. 【2018.06.26NOIP模拟】T2号码bachelor 【数位DP】*

    [2018.06.26NOIP模拟]T2号码bachelor 题目描述 Mike 正在在忙碌地发着各种各样的的短信.旁边的同学 Tom 注意到,Mike 发出短信的接收方手机号码似乎都满足着特别的性质 ...

  10. Linux环境安装配置Swftools

    系统:CentOS6.5的64位版本   这里有一位仁兄的几个错误处理办法,下面是swftools的安装配置步骤:   1.安装所需的库和组件.机器之前安装过了,主要安装的是下面几个组件.如果不安装会 ...