上一篇(传送门)介绍了测试平台 locust + boomer 的环境搭建,以及运行http压测用例,观测性能指数、图表。这篇接上篇,继续讲go boomer如何实现。

setup

Install the master branch

$ go get github.com/myzhan/boomer

Install a tagged version that works with locust 1.6.0

$ go get github.com/myzhan/boomer@v1.6.0

install gomq

$ go get -u github.com/zeromq/gomq

quick start

run master

创建python文件 workspace/dummy.py

from locust import Locust, TaskSet, task
class MyTaskSet(TaskSet):
    @task(20)
    def hello(self):
        pass
class Dummy(Locust):
    task_set = MyTaskSet

运行:

$ locust –master -f dummy.py output:

$locust.main: Starting web interface at http://0.0.0.0:8089 (accepting connections from all network interfaces)
$locust.main: Starting Locust 2.9.1.dev23
run slave

创建go文件 workspace/main.go

package main

import(
	"fmt"
	"io/ioutil"
	"net/http"
	"time"
	"github.com/myzhan/boomer"
)

func helloTask() {
	start := time.Now()
	err := HttpGet("hello")
	elapsed := time.Since(start)
	if err != nil {
		boomer.RecordFailure("http", "world", elapsed.Nanoseconds()/int64(time.Millisecond), err.Error())
		return
	}
    
/*    Report your test result as a success, if you write it in locust, it will looks like this    events.request_success.fire(request_type="http", name="world", response_time=100, response_length=10)    */
	boomer.RecordSuccess("http", "world", elapsed.Nanoseconds()/int64(time.Millisecond), int64(10))
}

func worldTask() {
	start := time.Now()
	err := HttpGet("world")
	elapsed := time.Since(start)
	if err != nil {
		boomer.RecordFailure("udp", "world", elapsed.Nanoseconds()/int64(time.Millisecond), err.Error())
		return
	} 
/*  Report your test result as a failure, if you write it in locust, it will looks like this    events.request_failure.fire(request_type="udp", name="hello", response_time=100, exception=Exception("udp error"))    */
	boomer.RecordSuccess("udp", "world", elapsed.Nanoseconds()/int64(time.Millisecond), int64(10))
}

func main() {
	task1 := &boomer.Task{
		// 同时跑多个 tasks 的时候,Weight 字段用于分配 goroutines
		Weight: 10,
		Fn:     helloTask,
	}

	task2 := &boomer.Task{
		Weight: 10,
		Fn:     worldTask,
	}

	// 连接到 master,等待页面上下发指令,支持多个 Task
	boomer.Run(task1, task2)
}


func HttpGet(path string) error {
	url := fmt.Sprintf("http://localhost:8090/%s", path)
	method := "GET"

	client := &http.Client{}
	req, err := http.NewRequest(method, url, nil)

	if err != nil {
		fmt.Println(err)
		return err
	}
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
		return err
	}
	defer res.Body.Close()

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		fmt.Println(err)
		return err
	}
	fmt.Println(string(body))
	return nil
}


go run main.go

output

$ Boomer is built with gomq support.
$ Boomer is connected to master(tcp://127.0.0.1:5557) press Ctrl+c to quit.

说明启动slave成功,查看是否连接上master

$ locust.runners: Client 'crazyMac.local_axxbyy123456' reported as ready. Currently 1 clients ready to swarm.

说明已经连接上master 。

testing

启动测试,output

succeed

小结

本文主要介绍了如何利用go boomer 实现locust的通讯协议,以及使用boomer实现一个上一篇的http压测例子。

reference