httptestbench

package module
v0.1.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 1, 2022 License: MIT Imports: 11 Imported by: 0

README

HTTP test benchmark helper for Go

Build Status Coverage Status GoDevDoc Code lines Comments

This module provides benchmark helper to send concurrent http requests and assert responses.

There are a few custom metrics added to benchmark result:

  • 50%:ms, 90%:ms, 99%:ms, 99.9%:ms are percentile values of round trip latency in milliseconds,
  • B:rcvd/op and B:sent/op are average bytes received and sent per request,
  • rps is requests per second rate.

The client is using github.com/valyala/fasthttp for better performance and lower impact on benchmark results.

Example

func Benchmark_helloWorld(b *testing.B) {
	srv := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
		_, err := writer.Write([]byte("Hello World!"))
		if err != nil {
			b.Fatal(err)
		}
	}))
	defer srv.Close()

	httptestbench.RoundTrip(b, 50,
		func(i int, req *fasthttp.Request) {
			req.SetRequestURI(srv.URL)
		},
		func(i int, resp *fasthttp.Response) bool {
			return resp.StatusCode() == http.StatusOK
		},
	)
}

Sample result:

Benchmark_helloWorld-12    	  142648	      8230 ns/op	         0.3469 50%:ms	         0.6917 90%:ms	         1.428 99%:ms	         2.721 99.9%:ms	       129.0 B:rcvd/op	        63.00 B:sent/op	    121496 rps	    1551 B/op	      17 allocs/op

Performance expectations can also be locked with a test:

func TestRoundTrip_helloWorld(t *testing.T) {
	res := testing.Benchmark(Benchmark_helloWorld)

	if httptestbench.RaceDetectorEnabled {
		assert.Less(t, res.Extra["B:rcvd/op"], 130.0)
		assert.Less(t, res.Extra["B:sent/op"], 64.0)
		assert.Less(t, res.AllocsPerOp(), int64(25))
		assert.Less(t, res.AllocedBytesPerOp(), int64(5000))
	} else {
		assert.Less(t, res.Extra["B:rcvd/op"], 130.0)
		assert.Less(t, res.Extra["B:sent/op"], 64.0)
		assert.Less(t, res.AllocsPerOp(), int64(16))
		assert.Less(t, res.AllocedBytesPerOp(), int64(1500))
	}
}

Documentation

Overview

Package httptestbench provides benchmark helper to send concurrent http requests and assert responses.

Index

Constants

View Source
const RaceDetectorEnabled = false

RaceDetectorEnabled exposes Go race detector status.

Race detector affects performance and allocations so it has to be taken in account when asserting benchmark results.

Variables

This section is empty.

Functions

func RoundTrip

func RoundTrip(
	b *testing.B,
	concurrency int,
	setupRequest func(i int, req *fasthttp.Request),
	responseIsValid func(i int, resp *fasthttp.Response) bool,
)

RoundTrip sends b.N requests concurrently and asserts valid response.

func ServeHTTP added in v0.1.1

func ServeHTTP(
	b *testing.B,
	concurrency int,
	h http.Handler,
	makeRequest func(i int) *http.Request,
	responseIsValid func(i int, resp *httptest.ResponseRecorder) bool,
)

ServeHTTP serves b.N requests with http.Handler concurrently and asserts valid response.

Types

type Server

type Server struct {
	URL string // base URL of form http://ipaddr:port with no trailing slash
	// contains filtered or unexported fields
}

Server is a fasthttp server for tests.

func NewServer

func NewServer(handler fasthttp.RequestHandler) *Server

NewServer starts new fasthttp test server with free local port.

func (*Server) Close

func (s *Server) Close()

Close shutdowns test server.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL