Created
February 8, 2018 20:56
-
-
Save xeoncross/315b759037eb43877e1ca9a453d54765 to your computer and use it in GitHub Desktop.
Golang Benchmark Gongular Processing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} | |
Author
xeoncross
commented
Feb 8, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment