Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created February 8, 2018 20:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xeoncross/315b759037eb43877e1ca9a453d54765 to your computer and use it in GitHub Desktop.
Save xeoncross/315b759037eb43877e1ca9a453d54765 to your computer and use it in GitHub Desktop.
Golang Benchmark Gongular Processing
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/mustafaakin/gongular"
)
func use(...interface{}) {}
func newEngineTest() *gongular.Engine {
e := gongular.NewEngine()
// e.SetErrorHandler(defaultErrorHandler)
e.SetRouteCallback(gongular.NoOpRouteCallback)
return e
}
func respWrap(e *gongular.Engine, path, method string, reader io.Reader) (*httptest.ResponseRecorder, string) {
resp := httptest.NewRecorder()
uri := path
req, err := http.NewRequest(method, uri, reader)
if err != nil {
// t.Fatal(err)
return resp, ""
}
e.GetHandler().ServeHTTP(resp, req)
p, err := ioutil.ReadAll(resp.Body)
if err != nil {
// t.Fail()
return resp, ""
}
return resp, string(p)
}
func get(e *gongular.Engine, path string) (*httptest.ResponseRecorder, string) {
return respWrap(e, path, "GET", nil)
}
type multiParam struct {
Param struct {
UserID string
Page int
}
}
func (m *multiParam) Handle(c *gongular.Context) error {
c.SetBody(fmt.Sprintf("%s:%d", m.Param.UserID, m.Param.Page))
return nil
}
// func TestMultiParam(t *testing.T) {
// e := newEngineTest()
// e.GetRouter().GET("/user/:UserID/page/:Page", &multiParam{})
//
// resp, content := get(e, "/user/ahmet/page/5")
// assert.Equal(t, http.StatusOK, resp.Code)
// assert.Equal(t, content, "\"ahmet:5\"")
// }
func BenchmarkProcessing(b *testing.B) {
e := newEngineTest()
e.GetRouter().GET("/user/:UserID/page/:Page", &multiParam{})
for n := 0; n < b.N; n++ {
resp, content := get(e, "/user/ahmet/page/5")
use(resp, content)
}
}
@xeoncross
Copy link
Author

go test -bench=.
goos: darwin
goarch: amd64
BenchmarkProcessing-4   	  200000	      9619 ns/op

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment