安装docker + locust + boomer压测环境实现对接口的压测
locust本身设计的非常简单轻量,经典的压测架构 master -> slave的形式,master只负责数据的收集和消息的广播等操作,task的执行均由slave执行。
分发task的话需要编写对应的locustfile,文件内容里即指定task和相应的执行数量、权重、限制并发数等。
locust原生的slave是通过gevent来实现高并发的测试请求,而这里我们也可以使用boomer,这是一款Github上开源的Golang版实现的slave,完全兼容locust的协议!
进行安装步骤:
下载docker1.5.3 镜像
docker pull locustio/locust:1.5.3
创建容器
docker run -d –name locust1.5.3 -p 8089:8089 -p 5557:5557 -v /tmp/locust:/app -w /app b16e447fbd6b -f dummy.py –master -H 0.0.0.0:8089
执行slave端go脚本(启动boomer充当slave,并连接locust master)
go run main.go –master-host=0.0.0.0 –master-port=5557
访问locust
http://0.0.0.0:8089/
go boomer
package main
import (
"bytes"
"encoding/json"
"fmt"
"github.com/myzhan/boomer"
"io/ioutil"
"log"
"math/rand"
"net/http"
"time"
)
// api-server地址: 你要测试的api
var serverUrl = "http://www.zongscan.com"
func getDemo() {
start := time.Now()
resp, err := http.Get("http://www.zongscan.com")
if err != nil {
log.Println(err)
return
}
defer resp.Body.Close()
elapsed := time.Since(start)
if resp.Status == "200 OK" {
boomer.RecordSuccess("http", "test", elapsed.Nanoseconds()/int64(time.Millisecond), int64(10))
} else {
boomer.RecordFailure("http", "test", elapsed.Nanoseconds()/int64(time.Millisecond), "test not equal")
}
}
// event callback
func eventCallback() {
start := time.Now()
info := make(map[string]interface{})
info["test"] = 1
// 将map解析未[]byte类型
bytesData, _ := json.Marshal(info)
// 将解析之后的数据转为*Reader类型
reader := bytes.NewReader(bytesData)
resp, error1 := http.Post(serverUrl+"/test",
"application/json",
reader)
elapsed := time.Since(start)
if resp == nil {
fmt.Println(error1)
boomer.RecordFailure("http", "test", elapsed.Nanoseconds()/int64(time.Millisecond), "test not equal")
}else{
body, _ := ioutil.ReadAll(resp.Body)
if resp.Status == "200 OK"{
boomer.RecordSuccess("http", "test", elapsed.Nanoseconds()/int64(time.Millisecond), int64(10))
} else {
boomer.RecordFailure("http", "test", elapsed.Nanoseconds()/int64(time.Millisecond), "test not equal")
}
}
defer resp.Body.Close()
}
func main() {
//task1 := &boomer.Task{
// Name: "sostreq",
// // The weight is used to distribute goroutines over multiple tasks.
// Weight: 20,
// Fn: getDemo,
//}
task2 := &boomer.Task{
Name: "test",
// The weight is used to distribute goroutines over multiple tasks.
Weight: 10,
Fn: eventCallback,
}
//boomer.Run(task1, task2)
boomer.Run(task2)
}
相关文章