func BenchmarkGetParamWego(B *testing.B) {
web, _ := NewWeb()
web.Config.ShowUrlLog = false
web.initModule()
web.GET("/text", func(c *WebContext) {
name := c.QueryParam.GetString("name")
c.QueryParam.GetString("name_9871")
c.WriteText(200, name.Value)
})
var buf bytes.Buffer
buf.WriteString("/text?name=lisi")
buf.WriteString("&name_9871=value_9871")
buf.WriteString("&name_9872=value_9872")
buf.WriteString("&name_9873=value_9873")
buf.WriteString("&name_9874=value_9874")
buf.WriteString("&name_9875=value_9875")
buf.WriteString("&name_9876=value_9876")
buf.WriteString("&name_9877=value_9877")
buf.WriteString("&name_9878=value_9878")
buf.WriteString("&name_9879=value_9879")
query := buf.String()
w := newMockWriter()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
req, err := http.NewRequest("GET", query, nil)
if err != nil {
B.Log(err)
return
}
web.ServeHTTP(w, req)
}
}
//go test -v -run=none -bench="BenchmarkGetParamGolang" -benchmem
func BenchmarkGetParamGolang(B *testing.B) {
mux := http.NewServeMux()
mux.HandleFunc("/text", func (w http.ResponseWriter, r *http.Request) {
name := r.FormValue("name")
r.FormValue("name_9871")
w.Write([]byte(name))
})
var buf bytes.Buffer
buf.WriteString("/text?name=lisi")
buf.WriteString("&name_9871=value_9871")
buf.WriteString("&name_9872=value_9872")
buf.WriteString("&name_9873=value_9873")
buf.WriteString("&name_9874=value_9874")
buf.WriteString("&name_9875=value_9875")
buf.WriteString("&name_9876=value_9876")
buf.WriteString("&name_9877=value_9877")
buf.WriteString("&name_9878=value_9878")
buf.WriteString("&name_9879=value_9879")
query := buf.String()
w := newMockWriter()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
req, err := http.NewRequest("GET", query, nil)
if err != nil {
B.Log(err)
return
}
mux.ServeHTTP(w, req)
}
}
测试结果:
pkg: Wego
BenchmarkGetParamWego-6----------601780--------1705 ns/op---------464 B/op--------4 allocs/op
pkg: Golang
BenchmarkGetParamGolang-6--------342019--------3136 ns/op--------1775 B/op--------18 allocs/op
//go test -v -run=none -bench="BenchmarkWriteJsonWego" -benchmem
func BenchmarkWriteJsonWego(B *testing.B) {
web, _ := NewWeb()
web.Config.ShowUrlLog = false
web.initModule()
web.GET("/json", func(c *WebContext) {
var user User
user.Name = "lisi"
user.Age = 12
c.WriteJSON(200, user)
})
w := newMockWriter()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
req, err := http.NewRequest("GET", "/json", nil)
if err != nil {
B.Log(err)
return
}
web.ServeHTTP(w, req)
}
}
//go test -v -run=none -bench="BenchmarkWriteJsonGolang" -benchmem
func BenchmarkWriteJsonGolang(B *testing.B) {
mux := http.NewServeMux()
mux.HandleFunc("/json", func (w http.ResponseWriter, r *http.Request) {
var user User
user.Name = "lisi"
user.Age = 12
data, err := json.Marshal(user)
if err != nil {
B.Log(err)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.Write(data)
})
w := newMockWriter()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
req, err := http.NewRequest("GET", "/json", nil)
if err != nil {
B.Log(err)
return
}
mux.ServeHTTP(w, req)
}
}
测试结果:
pkg: Wego
BenchmarkWriteJsonWego-6----------1540137--------780.3 ns/op--------496 B/op--------5 allocs/op
pkg: Golang
BenchmarkWriteJsonGolang-6--------1482672--------809.0 ns/op--------528 B/op--------6 allocs/op
json数据的输出二者持平
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title> New Document </title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
hello {{.}}
</body>
</html>
//go test -v -run=none -bench="BenchmarkWriteHTMLWego" -benchmem
func BenchmarkWriteHTMLWego(B *testing.B) {
web, _ := NewWeb()
web.Config.ShowUrlLog = false
web.initModule()
web.GET("/html", func(c *WebContext) {
c.WriteHTML(200, "./test.html", "demo")
})
w := newMockWriter()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
req, err := http.NewRequest("GET", "/html", nil)
if err != nil {
B.Log(err)
return
}
web.ServeHTTP(w, req)
}
}
//go test -v -run=none -bench="BenchmarkWriteHTMLGolang" -benchmem
func BenchmarkWriteHTMLGolang(B *testing.B) {
mux := http.NewServeMux()
mux.HandleFunc("/html", func (w http.ResponseWriter, r *http.Request) {
t, err := template.ParseFiles("./test.html")
if err!=nil {
B.Log(err)
return
}
t.Execute(w, "demo")
})
w := newMockWriter()
B.ReportAllocs()
B.ResetTimer()
for i := 0; i < B.N; i++ {
req, err := http.NewRequest("GET", "/html", nil)
if err != nil {
B.Log(err)
return
}
mux.ServeHTTP(w, req)
}
}
测试结果:
pkg: Wego
BenchmarkWriteHTMLWego-6---------718764----------1495 ns/op----------704 B/op--------12 allocs/op
pkg: Golang
BenchmarkWriteHTMLGolang-6--------10000--------119648 ns/op--------14345 B/op--------88 allocs/op
二者差距比较大,主要是因为wego会缓存模板数据,因此性能比较好。