Skip to content
Myr

benchmarks_test.go

This file

Path

benchmarks_test.go

160 lines · 3.9 KiB · Go

Last changed by

refactor: for loop can be modernized using range over int (#4392)wanghaolong613before adoptione3118cc

History of this file

Take it

The walk stopped after 200 commits: lines older than cf32d2d point at it.

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 1–89why, one step back
1// Copyright 2017 Manu Martinez-Almeida. All rights reserved.
2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package gin67import (8 "html/template"9 "net/http"10 "os"11 "testing"12)1314func BenchmarkOneRoute(B *testing.B) {15 router := New()16 router.GET("/ping", func(c *Context) {})17 runRequest(B, router, http.MethodGet, "/ping")18}1920func BenchmarkRecoveryMiddleware(B *testing.B) {21 router := New()22 router.Use(Recovery())23 router.GET("/", func(c *Context) {})24 runRequest(B, router, http.MethodGet, "/")25}2627func BenchmarkLoggerMiddleware(B *testing.B) {28 router := New()29 router.Use(LoggerWithWriter(newMockWriter()))30 router.GET("/", func(c *Context) {})31 runRequest(B, router, http.MethodGet, "/")32}3334func BenchmarkManyHandlers(B *testing.B) {35 router := New()36 router.Use(Recovery(), LoggerWithWriter(newMockWriter()))37 router.Use(func(c *Context) {})38 router.Use(func(c *Context) {})39 router.GET("/ping", func(c *Context) {})40 runRequest(B, router, http.MethodGet, "/ping")41}4243func Benchmark5Params(B *testing.B) {44 DefaultWriter = os.Stdout45 router := New()46 router.Use(func(c *Context) {})47 router.GET("/param/:param1/:params2/:param3/:param4/:param5", func(c *Context) {})48 runRequest(B, router, http.MethodGet, "/param/path/to/parameter/john/12345")49}5051func BenchmarkOneRouteJSON(B *testing.B) {52 router := New()53 data := struct {54 Status string `json:"status"`55 }{"ok"}56 router.GET("/json", func(c *Context) {57 c.JSON(http.StatusOK, data)58 })59 runRequest(B, router, http.MethodGet, "/json")60}6162func BenchmarkOneRouteHTML(B *testing.B) {63 router := New()64 t := template.Must(template.New("index").Parse(`65 <html><body><h1>{{.}}</h1></body></html>`))66 router.SetHTMLTemplate(t)6768 router.GET("/html", func(c *Context) {69 c.HTML(http.StatusOK, "index", "hola")70 })71 runRequest(B, router, http.MethodGet, "/html")72}7374func BenchmarkOneRouteSet(B *testing.B) {75 router := New()76 router.GET("/ping", func(c *Context) {77 c.Set("key", "value")78 })79 runRequest(B, router, http.MethodGet, "/ping")80}8182func BenchmarkOneRouteString(B *testing.B) {83 router := New()84 router.GET("/text", func(c *Context) {85 c.String(http.StatusOK, "this is a plain text")86 })87 runRequest(B, router, http.MethodGet, "/text")88}89

test(benchmarks): fix the incorrect function name (#4375)Meng Xunbefore adoptioned150e7

line 90why, one step back
90func BenchmarkManyRoutesFirst(B *testing.B) {

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 91–156why, one step back
91 router := New()
92 router.Any("/ping", func(c *Context) {})93 runRequest(B, router, http.MethodGet, "/ping")94}9596func BenchmarkManyRoutesLast(B *testing.B) {97 router := New()98 router.Any("/ping", func(c *Context) {})99 runRequest(B, router, "OPTIONS", "/ping")100}101102func Benchmark404(B *testing.B) {103 router := New()104 router.Any("/something", func(c *Context) {})105 router.NoRoute(func(c *Context) {})106 runRequest(B, router, http.MethodGet, "/ping")107}108109func Benchmark404Many(B *testing.B) {110 router := New()111 router.GET("/", func(c *Context) {})112 router.GET("/path/to/something", func(c *Context) {})113 router.GET("/post/:id", func(c *Context) {})114 router.GET("/view/:id", func(c *Context) {})115 router.GET("/favicon.ico", func(c *Context) {})116 router.GET("/robots.txt", func(c *Context) {})117 router.GET("/delete/:id", func(c *Context) {})118 router.GET("/user/:id/:mode", func(c *Context) {})119120 router.NoRoute(func(c *Context) {})121 runRequest(B, router, http.MethodGet, "/viewfake")122}123124type mockWriter struct {125 headers http.Header126}127128func newMockWriter() *mockWriter {129 return &mockWriter{130 http.Header{},131 }132}133134func (m *mockWriter) Header() (h http.Header) {135 return m.headers136}137138func (m *mockWriter) Write(p []byte) (n int, err error) {139 return len(p), nil140}141142func (m *mockWriter) WriteString(s string) (n int, err error) {143 return len(s), nil144}145146func (m *mockWriter) WriteHeader(int) {}147148func runRequest(B *testing.B, r *Engine, method, path string) {149 // create fake request150 req, err := http.NewRequest(method, path, nil)151 if err != nil {152 panic(err)153 }154 w := newMockWriter()155 B.ReportAllocs()156 B.ResetTimer()

refactor: for loop can be modernized using range over int (#4392)wanghaolong613before adoptione3118cc

line 157why, one step back
157 for B.Loop() {

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 158–160why, one step back
158 r.ServeHTTP(w, req)
159 }160}