~egtann/srp

c4476361a177ff0172149ac9ecde1da79f9a60b7 — Evan Tann 2 years ago f093321
add ServeHTTP benchmark
2 files changed, 48 insertions(+), 5 deletions(-)

M proxy.go
A proxy_test.go
M proxy.go => proxy.go +16 -5
@@ 4,11 4,13 @@ import (
	"context"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"math/rand"
	"net"
	"net/http"
	"net/http/httputil"
	"os"
	"strings"
	"sync"
	"time"


@@ 104,12 106,10 @@ func (r *ReverseProxy) ServeHTTP(w http.ResponseWriter, req *http.Request) {
	r.rp.ServeHTTP(w, req)
}

// NewRegistry for a given configuration file. This reports an error if any
// frontend host has no backends.
func NewRegistry(filename string) (Registry, error) {
	byt, err := ioutil.ReadFile(filename)
func newRegistry(r io.Reader) (Registry, error) {
	byt, err := ioutil.ReadAll(r)
	if err != nil {
		return nil, fmt.Errorf("read config file %q: %s", filename, err)
		return nil, err
	}
	reg := Registry{}
	err = json.Unmarshal(byt, &reg)


@@ 124,6 124,17 @@ func NewRegistry(filename string) (Registry, error) {
	return reg, nil
}

// NewRegistry for a given configuration file. This reports an error if any
// frontend host has no backends.
func NewRegistry(filename string) (Registry, error) {
	fi, err := os.Open(filename)
	if err != nil {
		return nil, fmt.Errorf("read file %s: %s", filename, err)
	}
	defer fi.Close()
	return newRegistry(fi)
}

// Hosts for the registry.
func (r Registry) Hosts() []string {
	hosts := []string{}

A proxy_test.go => proxy_test.go +32 -0
@@ 0,0 1,32 @@
package srp

import (
	"bytes"
	"net/http/httptest"
	"testing"
)

func BenchmarkServeHTTP(b *testing.B) {
	content := `{
		"frontend": {
			"HealthPath": "/health",
			"Backends": ["1", "2"]
		}
	}`
	reg, err := newRegistry(bytes.NewBufferString(content))
	if err != nil {
		panic(err)
	}
	proxy := NewProxy(noopLogger{}, reg)
	w := httptest.NewRecorder()
	r := httptest.NewRequest("GET", "/", nil)
	b.ReportAllocs()
	for i := 0; i < b.N; i++ {
		proxy.ServeHTTP(w, r)
	}
}

type noopLogger struct{}

func (n noopLogger) Printf(format string, vals ...interface{})           {}
func (n noopLogger) ReqPrintf(reqID, format string, vals ...interface{}) {}