From 2201d9c9001577ef726ef75e995b6efb82455a76 Mon Sep 17 00:00:00 2001 From: k1LoW Date: Sat, 23 Sep 2023 21:48:44 +0900 Subject: [PATCH] Use binary --- .gitignore | 1 + Makefile | 6 ++++-- testdata/testbin/go.mod | 13 ++++++++++++ testdata/testbin/go.sum | 8 +++++++ testdata/testbin/main.go | 45 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 testdata/testbin/go.mod create mode 100644 testdata/testbin/go.sum create mode 100644 testdata/testbin/main.go diff --git a/.gitignore b/.gitignore index d8c0e3d..b928ace 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ coverage.out custom_metrics_benchmark.json +testdata/testbin/testbin diff --git a/Makefile b/Makefile index ade19d1..2803004 100644 --- a/Makefile +++ b/Makefile @@ -14,8 +14,10 @@ benchmark: depsdev go test -modfile=testdata/go_test.mod -bench . -benchmem -benchtime 10000x -run Benchmark | octocov-go-test-bench --tee > custom_metrics_benchmark.json cachegrind: depsdev - go mod tidy -modfile=testdata/go_test.mod - valgrind --tool=cachegrind --I1=8192,8,64 --D1=16384,4,64 --LL=32768,8,64 go test -modfile=testdata/go_test.mod -bench . -benchmem -benchtime 10000x -run Benchmark + cd testdata/testbin && go build -o testbin + valgrind --tool=cachegrind --I1=8192,8,64 --D1=16384,4,64 --LL=32768,8,64 ./testdata/testbin/testbin 10000 + valgrind --tool=cachegrind --I1=8192,8,64 --D1=16384,4,64 --LL=32768,8,64 ./testdata/testbin/testbin 10000 + valgrind --tool=cachegrind --I1=8192,8,64 --D1=16384,4,64 --LL=32768,8,64 ./testdata/testbin/testbin 10000 lint: go mod tidy diff --git a/testdata/testbin/go.mod b/testdata/testbin/go.mod new file mode 100644 index 0000000..0ac5c7e --- /dev/null +++ b/testdata/testbin/go.mod @@ -0,0 +1,13 @@ +module github.com/k1LoW/rl/testdata/testbin + +go 1.21.1 + +require ( + github.com/go-chi/httprate v0.7.4 + github.com/k1LoW/rl v0.6.1 +) + +require ( + github.com/cespare/xxhash/v2 v2.1.2 // indirect + golang.org/x/sync v0.3.0 // indirect +) diff --git a/testdata/testbin/go.sum b/testdata/testbin/go.sum new file mode 100644 index 0000000..d3648a8 --- /dev/null +++ b/testdata/testbin/go.sum @@ -0,0 +1,8 @@ +github.com/cespare/xxhash/v2 v2.1.2 h1:YRXhKfTDauu4ajMg1TPgFO5jnlC2HCbmLXMcTG5cbYE= +github.com/cespare/xxhash/v2 v2.1.2/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/go-chi/httprate v0.7.4 h1:a2GIjv8he9LRf3712zxxnRdckQCm7I8y8yQhkJ84V6M= +github.com/go-chi/httprate v0.7.4/go.mod h1:6GOYBSwnpra4CQfAKXu8sQZg+nZ0M1g9QnyFvxrAB8A= +github.com/k1LoW/rl v0.6.1 h1:97cFVVOMm3iXK9UdHxO78wO6FCmh4IA8edOoly64R8o= +github.com/k1LoW/rl v0.6.1/go.mod h1:FwcYhUX7i153XHIyR4OuOgR2a2KIzcBxrfanE9/O0S4= +golang.org/x/sync v0.3.0 h1:ftCYgMx6zT/asHUrPw8BLLscYtGznsLAnjq5RH9P66E= +golang.org/x/sync v0.3.0/go.mod h1:FU7BRWz2tNW+3quACPkgCx/L+uEAv1htQ0V83Z9Rj+Y= diff --git a/testdata/testbin/main.go b/testdata/testbin/main.go new file mode 100644 index 0000000..9ce6de4 --- /dev/null +++ b/testdata/testbin/main.go @@ -0,0 +1,45 @@ +package main + +import ( + "log" + "net/http" + "net/http/httptest" + "os" + "strconv" + + "github.com/go-chi/httprate" + "github.com/k1LoW/rl" + "github.com/k1LoW/rl/testutil" +) + +func main() { + n, err := strconv.Atoi(os.Args[1]) + if err != nil { + log.Fatal(err) + } + r := http.NewServeMux() + r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + _, _ = w.Write([]byte("Hello, world")) + }) + m := rl.New( + testutil.NewLimiter(10, httprate.KeyByIP, 0), + testutil.NewLimiter(10, testutil.KeyByHost, 0), + ) + ts := httptest.NewServer(m(r)) + defer ts.Close() + + log.Printf("start %d requests", n) + for i := 0; i < n; i++ { + req, err := http.NewRequest("GET", ts.URL, nil) + if err != nil { + log.Fatal(err) + } + req.Host = "a.example.com" + res, err := http.DefaultClient.Do(req) + if err != nil { + log.Fatal(err) + } + res.Body.Close() + } + log.Println("done") +}