golang实现的ab测试http代理工具

压测工具ab不能统计http请求的错误情况,包括http状态码错误和响应正文的错误关键字。

所以加层代理用于统计http错误情况,重在统计错误情况,而不是代理的性能,主要用于功能接口的测试,比如测试一下请求多少次接口会返回空数据。

被认为是错误的情况

1、非200状态码

2、响应正文里不包含表示正确关键字

3、响应正文里包含表示错误关键字

编译方式:

go build go_ab_proxy.go

启动方式:

./go_ab_proxy

./go_ab_proxy -okstr 'code":"0'

./go_ab_proxy -failstr error

测试方式:

ab -c 2 -n 10 -X 127.0.0.1:9090 'http://www.baidu.com/'

curl -x 127.0.0.1:9090 'http://www.baidu.com/'

查看错误统计:

ctrl-c或kill结束进程时显示统计结果。

golang代码如下:

go_ab_proxy.go

Go 复制代码
package main

import (
	"bytes"
	"flag"
	"fmt"
	"net/http"
	"net/http/httputil"
	"os"
	"os/signal"
	"strings"
	"sync/atomic"
	"syscall"
)

var (
	successCount int32
	failCount    int32
	okStr        = flag.String("okstr", "", "indicate ok string")
	failStr      = flag.String("failstr", "", "indicate fail string")
)

func main() {
	flag.Parse()
	if *okStr != "" && *failStr != "" {
		fmt.Printf("-okstr and -failstr only one can be used\n")
		return
	}
	http.HandleFunc("/", middleware(func(w http.ResponseWriter, r *http.Request) {
		proxy := httputil.NewSingleHostReverseProxy(r.URL)
		proxy.ServeHTTP(w, r)
	}))
	go installSignal()
	err := http.ListenAndServe(":9090", nil)
	if err != nil {
		fmt.Printf("proxy server start fail, %v\n", err)
		return
	}
}

func middleware(handler http.HandlerFunc) http.HandlerFunc {
	return func(response http.ResponseWriter, request *http.Request) {
		responseWrapper := &ResponseWithRecorder{
			ResponseWriter: response,
			StatusCode:     http.StatusOK,
			Body:           bytes.Buffer{},
		}
		handler(responseWrapper, request)
		if responseWrapper.StatusCode != http.StatusOK {
			atomic.AddInt32(&failCount, 1)
		} else {
			if *okStr == "" && *failStr == "" {
				atomic.AddInt32(&successCount, 1)
				return
			}
			resBody := string(responseWrapper.Body.Bytes())
			if *okStr != "" && !strings.Contains(resBody, *okStr) {
				atomic.AddInt32(&failCount, 1)
				return
			} else if *failStr != "" && strings.Contains(resBody, *failStr) {
				atomic.AddInt32(&failCount, 1)
				return
			}
			atomic.AddInt32(&successCount, 1)
		}
	}
}

func installSignal() {
	sigs := make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
	go func() {
		<-sigs
		fmt.Printf("successCount=%d, failCount=%d\n", atomic.LoadInt32(&successCount), atomic.LoadInt32(&failCount))
		os.Exit(0)
	}()
}

type ResponseWithRecorder struct {
	http.ResponseWriter
	StatusCode int
	Body       bytes.Buffer
}

func (rec *ResponseWithRecorder) WriteHeader(statusCode int) {
	rec.ResponseWriter.WriteHeader(statusCode)
	rec.StatusCode = statusCode
}

func (rec *ResponseWithRecorder) Write(d []byte) (n int, err error) {
	n, err = rec.ResponseWriter.Write(d)
	if err != nil {
		return
	}
	rec.Body.Write(d)
	return
}

--end--

相关推荐
zh73141 小时前
Go 1.23 → 1.26 升级检查文档
开发语言·chrome·golang
平头哥AI18 小时前
Day 01 | go run 跑通第一个 Go 程序,go build 留下一个能拷走的 exe
开发语言·后端·golang
2601_9622965119 小时前
如何使用Golang包路径_管理本地和远程模块引用
golang·数据转换·变量定义·常用库·编程语言区别
FfHUCisI1 天前
GMP 调度器:Go 并发的心脏是如何跳动的
开发语言·golang·php
我不会起名字3221 天前
一天一道算法题(26):栈的简单应用
java·数据结构·python·算法·leetcode·golang·
gsls2008081 天前
告别 Vault 的复杂度:用 Go 标准库给 Windows 凭据管理器装上 MCP
windows·golang·mcp
名字还没想好☜1 天前
Go 时间格式化为什么用 2006-01-02:time.Format/Parse 的参考时间、时区与解析踩坑
开发语言·后端·golang·go
FfHUCisI2 天前
Golang Map 哈希表实现
golang·哈希算法·散列表
源代码•宸2 天前
前置准备:定时微服务背景和现状
开发语言·经验分享·后端·微服务·云原生·架构·golang
李燚2 天前
把规则搬回家:三个 BC 的贫血→充血重构实录(第103篇)
golang·agent·ddd·领域驱动设计·eino·deepflux·eino adk