【locust】使用locust + boomer實現對接口的壓測

背景

很早以前,考慮單機執行能力,使用locust作過公司短信網關的壓測工做,後來發現了一個golang版本的locust,性能是python版本的5到10倍以上,可是一直沒有機會使用。python

最近公司想作一個性能測試平臺,技術選型要求和開發的語言一致,即golang,因此我想到了boomer,本文爲boomer的使用記錄。git

環境安裝

開發環境 安裝
Python 3.7
locust 0.11.0 pip install locustio
golang
boomer go get github.com/myzhan/boomer

:最新版本的boomer兼容了goczmq,須要將locust升級到較高版本才能完成兼容。github

腳本編寫

master

這部分的代碼不重要,只要能啓動就行。golang

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)
}

實際效果以下: json

問題

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

相關文章
相關標籤/搜索