【locust】使用locust + boomer实现对接口的压测

目录

背景

环境安装

脚本编写

master

slave节点(golang/boomer)

问题

资料获取方法


背景

很早之前,考虑单机执行能力,使用locust做过公司短信网关的压测工作,后来发现了一个golang版本的locust,性能是python版本的5到10倍以上,但是一直没有机会使用。

最近公司想做一个性能测试平台,技术选型要求和开发的语言一致,即golang,所以我想到了boomer,本文为boomer的使用记录。

环境安装

开发环境 安装
Python 3.7 略
locust 0.11.0 pip install locustio
golang 略
boomer go get github.com/myzhan/boomer

注:最新版本的boomer兼容了goczmq,需要将locust升级到较高版本才能完成兼容。

脚本编写

master

这部分的代码不重要,只要能启动就行。

python 复制代码
from locust import Locust, TaskSet, task


class MyTaskSet(TaskSet):
    @task(20)
    def hello(self):
        pass


class Dummy(Locust):
    task_set = MyTaskSet

slave节点(golang/boomer)

python 复制代码
package main

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"time"

	"github.com/myzhan/boomer"
)

func getDemo() {
	start := time.Now()
	resp, err := http.Get("http://httpbin.org/get?name=Detector")

	if err != nil {
		log.Println(err)
		return
	}
	defer resp.Body.Close()
	fmt.Println(resp.Status)
	elapsed := time.Since(start)
	if resp.Status == "200 OK" {
		boomer.RecordSuccess("http", "sostreq", elapsed.Nanoseconds()/int64(time.Millisecond), int64(10))
	} else {
		boomer.RecordFailure("http", "sostreq", elapsed.Nanoseconds()/int64(time.Millisecond), "sostreq not equal")
	}
}

func postDemo() {
	start := time.Now()

	info := make(map[string]interface{})
	info["name"] = "Detector"
	info["age"] = 15
	info["loc"] = "深圳"
	// 将map解析未[]byte类型
	bytesData, _ := json.Marshal(info)
	// 将解析之后的数据转为*Reader类型
	reader := bytes.NewReader(bytesData)
	resp, _ := http.Post("http://httpbin.org/post",
		"application/json",
		reader)
	body, _ := ioutil.ReadAll(resp.Body)
	fmt.Println(string(body))
	elapsed := time.Since(start)
	if resp.Status == "200 OK" {
		boomer.RecordSuccess("http", "sostreq", elapsed.Nanoseconds()/int64(time.Millisecond), int64(10))
	} else {
		boomer.RecordFailure("http", "sostreq", elapsed.Nanoseconds()/int64(time.Millisecond), "sostreq not equal")
	}
}

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: "sostreq",
		// The weight is used to distribute goroutines over multiple tasks.
		Weight: 10,
		Fn:     postDemo,
	}
	boomer.Run(task1, task2)
}

实际效果如下:

问题

Failed to execute dot. Is Graphviz installed? Error: exec: "dot": executable file not found in $PATH


资料获取方法

【留言777】

各位想获取源码等教程资料的朋友请 点赞 + 评论 + 收藏 ,三连!

三连之后我会在评论区挨个私信发给你们~

相关推荐
ychqsq29 分钟前
207.炸酱面
经验分享·职场和发展
路弥行至8 小时前
【Head First 设计模式】第 2 章:观察者模式 —— 用现代 C++ 重新解读“交互对象的松耦合
c++·经验分享·笔记·观察者模式·设计模式·入门教程·headfirst
ychqsq8 小时前
206.归程
经验分享·职场和发展
胡二拉二胡10 小时前
抗遗忘单词表 App:让单词记忆不再「背了就忘」
经验分享·笔记·学习
2301_7893804910 小时前
【踩坑记录】Windows资源管理器无限重启,改注册表、看日志
windows·经验分享·笔记
by2099910 小时前
学会使用 std::string 类,并理解其内部是如何管理字符串的详细阐述(下)
c++·经验分享·笔记·字符串·类和对象·string
2601_9672127213 小时前
智能轨道插座系统技术选型逻辑与行业技术路线分析
网络·经验分享
ychqsq14 小时前
204.现场
经验分享·职场和发展
2601_967212721 天前
专利型电源轨道系统技术路线对比与选型框架
大数据·运维·网络·经验分享
SHARK_pssm1 天前
【C++——类和对象(下)】
开发语言·c++·经验分享·笔记