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

本文介绍了如何在Golang环境中安装和使用Boomer进行性能测试,包括master节点的脚本编写和slave节点的HTTP请求示例。作者还提到了在执行过程中遇到的问题,如dot命令找不到。最后提供获取资料的方法。
摘要由CSDN通过智能技术生成

目录

背景

环境安装

脚本编写

master

slave节点(golang/boomer)

问题

 资料获取方法


背景

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

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

环境安装

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

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

脚本编写

master

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

from locust import Locust, TaskSet, task


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


class Dummy(Locust):
    task_set = MyTaskSet
slave节点(golang/boomer)
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】

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

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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Python和Locust来进行对ES7的压测。首先,确保你已经安装了Python和Locust。 接下来,你需要安装elasticsearch-py库,它是Python与Elasticsearch进行交互的库。可以使用以下命令安装: ``` pip install elasticsearch ``` 然后,创建一个Python脚本,导入必要的模块和库: ```python from locust import HttpUser, task, between from elasticsearch import Elasticsearch class ESUser(HttpUser): wait_time = between(1, 5) def on_start(self): # 创建一个Elasticsearch客户端连接 self.client = Elasticsearch(['localhost:9200']) @task def search(self): # 定义一个搜索任务 query = { "query": { "match_all": {} } } # 发送搜索请求 response = self.client.search(index='your_index', body=query) # 打印搜索结果 print(response) ``` 在上面的代码中,我们创建了一个名为ESUser的Locust用户类。在`on_start`方法中,我们创建了一个Elasticsearch客户端连接。 然后,在`@task`装饰的`search`方法中,我们定义了一个搜索任务。你可以根据自己的需求修改查询条件。在该方法中,我们发送了一个搜索请求,并打印了搜索结果。 最后,你可以在命令行中使用Locust命令来启动压测: ``` locust -f your_script.py --host=http://localhost:9200 ``` 替换`your_script.py`为你的脚本文件名,`http://localhost:9200`为你的ES7的地址。 然后,你可以在浏览器中访问Locust的Web界面(默认为http://localhost:8089)来配置并启动压测。 注意:在进行压测之前,请确保你已经在ES7中创建了索引,并且数据已经准备好。另外,压测会对目标系统造成一定的负载,请谨慎使用
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值