1. 准备一个http服务

    package main
    
    import (
    	"io/ioutil"
    	"log"
    	"net/http"
    )
    
    func echo(wr http.ResponseWriter, r *http.Request) {
    	msg, err := ioutil.ReadAll(r.Body)
    	if err != nil {
    		wr.Write([]byte("echo error"))
    		return
    	}
    	wr.WriteHeader(200)
    	wr.Write([]byte("hello"))
    	writelen, err := wr.Write(msg)
    	if err != nil || writelen != len(msg) {
    		log.Println(err, "writen len: ", writelen)
    	}
    }
    
    func main() {
    	http.HandleFunc("/", echo)
    	err := http.ListenAndServe(":8080", nil)
    	if err != nil {
    		log.Fatal(err)
    	}
    }
    
    
  2. 下载并编译wrk

    git clone https://github.com/wg/wrk.git  
    

可能缺少openssl,sudo apt-get install libssl-dev

编译

make
  1. 测试

    wrk -t12 -c100 -d30s http://www.baidu.com  
    ./wrk -t12 -c100 -d30s http://192.168.126.1:8080
    
  2. 结果

    [root@c1 wrk-4.1.0]# ./wrk -t12 -c100 -d10s http://192.168.108.1:8080
    Running 10s test @ http://192.168.108.1:8080
    12 threads and 100 connections
    Thread Stats   Avg      Stdev     Max   +/- Stdev
    Latency    19.99ms   30.05ms 168.74ms   84.31%
    Req/Sec     1.44k   332.65     3.91k    83.01%
    170055 requests in 10.10s, 19.62MB read
    Requests/sec:  16841.88
    Transfer/sec:      1.94M