locust+boomer

Golang
514
0
0
2022-09-20

locust+boomer 压测

下载1.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端 golang 脚本

go run main.go –master-host=0.0.0.0 –master-port=5557

访问 0.0.0.0:8089/

![locust+boomer]

locust+boomer

locust+boomer

package main

import (
    "bytes" 
    "encoding/json" 
    "fmt" 
    "github.com/myzhan/boomer" 
    "io/ioutil" 
    "log" 
    "math/rand" 
    "net/http" 
    "time"
)

// api-server地址
var serverUrl = "http://xxxx"

func getDemo() {
    start := time.Now()
    resp, err := http.Get("http://xxxx")

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