forked from smallnest/go-web-framework-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
617 lines (566 loc) · 13.4 KB
/
server.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
package main
import (
"fmt"
"io"
"net/http"
"os"
"runtime"
"strconv"
"time"
"github.com/Unknwon/macaron"
"github.com/ant0ine/go-json-rest/rest"
"github.com/astaxie/beego"
"github.com/astaxie/beego/context"
"github.com/bmizerany/pat"
"github.com/buaazp/fasthttprouter"
"github.com/celrenheit/lion"
"github.com/claygod/Bxog"
"github.com/dimfeld/httptreemux"
"github.com/emicklei/go-restful"
"github.com/gin-gonic/gin"
"github.com/go-martini/martini"
"github.com/go-playground/lars"
"github.com/go-zoo/bone"
"github.com/gocraft/web"
"github.com/gorilla/mux"
"github.com/julienschmidt/httprouter"
"github.com/kataras/iris"
echov2 "github.com/labstack/echo"
echov2fasthttp "github.com/labstack/echo/engine/fasthttp"
echov2standard "github.com/labstack/echo/engine/standard"
llog "github.com/lunny/log"
"github.com/lunny/tango"
vulcan "github.com/mailgun/route"
"github.com/mikespook/possum"
possumrouter "github.com/mikespook/possum/router"
possumview "github.com/mikespook/possum/view"
"github.com/naoina/denco"
"github.com/pilu/traffic"
"github.com/plimble/ace"
routing "github.com/qiangxue/fasthttp-routing"
"github.com/rcrowley/go-tigertonic"
"github.com/valyala/fasthttp"
"github.com/vanng822/r2router"
goji "github.com/zenazn/goji/web"
gojiv2 "goji.io"
gojiv2pat "goji.io/pat"
gcontext "golang.org/x/net/context"
"gopkg.in/baa.v1"
echo "gopkg.in/labstack/echo.v1"
)
var port = 8080
var sleepTime = 0
var sleepTimeDuration time.Duration
var message = []byte("hello world")
var messageStr = "hello world"
var samplingPoint = 20 //seconds
// server [default] [10] [8080]
func main() {
args := os.Args
argsLen := len(args)
webFramework := "default"
if argsLen > 1 {
webFramework = args[1]
}
if argsLen > 2 {
sleepTime, _ = strconv.Atoi(args[2])
}
if argsLen > 3 {
port, _ = strconv.Atoi(args[3])
}
if argsLen > 4 {
samplingPoint, _ = strconv.Atoi(args[4])
}
sleepTimeDuration = time.Duration(sleepTime) * time.Millisecond
samplingPointDuration := time.Duration(samplingPoint) * time.Second
go func() {
time.Sleep(samplingPointDuration)
var mem runtime.MemStats
runtime.ReadMemStats(&mem)
var u uint64 = 1024 * 1024
fmt.Printf("TotalAlloc: %d\n", mem.TotalAlloc/u)
fmt.Printf("Alloc: %d\n", mem.Alloc/u)
fmt.Printf("HeapAlloc: %d\n", mem.HeapAlloc/u)
fmt.Printf("HeapSys: %d\n", mem.HeapSys/u)
}()
switch webFramework {
case "default":
startDefaultMux()
case "ace":
startAce()
case "baa":
startBaa()
case "beego":
startBeego()
case "bone":
startBone()
case "bxog":
startBxog()
case "denco":
startDenco()
case "echov1":
startEchoV1()
case "echov2standard":
startEchoV2Standard()
case "echov2fasthttp":
startEchoV2Fasthttp()
case "fasthttp-raw":
startFasthttp()
case "fasthttprouter":
startFastHttpRouter()
case "fasthttp-routing":
startFastHttpRouting()
case "gin":
startGin()
case "gocraftWeb":
startGocraftWeb()
case "goji":
startGoji()
case "gojiv2":
startGojiv2()
case "gojsonrest":
startGoJsonRest()
case "gorestful":
startGoRestful()
case "gorilla":
startGorilla()
case "httprouter":
startHttpRouter()
case "httptreemux":
starthttpTreeMux()
case "iris":
startIris()
case "lars":
startLars()
case "lion":
startLion()
case "macaron":
startMacaron()
case "martini":
startMartini()
case "pat":
startPat()
case "possum":
startPossum()
case "r2router":
startR2router()
case "tango":
startTango()
case "tiger":
startTigerTonic()
case "traffic":
startTraffic()
case "vulcan":
startVulcan()
}
}
// default mux
func helloHandler(w http.ResponseWriter, r *http.Request) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startDefaultMux() {
http.HandleFunc("/hello", helloHandler)
http.ListenAndServe(":"+strconv.Itoa(port), nil)
}
//ace
func aceHandler(c *ace.C) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
c.Writer.Write(message)
}
func startAce() {
mux := ace.New()
mux.GET("/hello", aceHandler)
mux.Run(":" + strconv.Itoa(port))
}
// baa
func baaHandler(ctx *baa.Context) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
ctx.Text(200, message)
}
func startBaa() {
mux := baa.New()
mux.Get("/hello", baaHandler)
mux.Run(":" + strconv.Itoa(port))
}
//beego
func beegoHandler(ctx *context.Context) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
ctx.WriteString(messageStr)
}
func startBeego() {
beego.BConfig.RunMode = beego.PROD
beego.BeeLogger.Close()
mux := beego.NewControllerRegister()
mux.Get("/hello", beegoHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// bone
func startBone() {
mux := bone.New()
mux.HandleFunc("/hello", helloHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// bxog
func bxogHandler(w http.ResponseWriter, req *http.Request, r *bxog.Router) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startBxog() {
mux := bxog.New()
mux.Add("/hello", bxogHandler)
mux.Start(":" + strconv.Itoa(port))
}
// denco
func dencoHandler(w http.ResponseWriter, r *http.Request, params denco.Params) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startDenco() {
mux := denco.NewMux()
handler, _ := mux.Build([]denco.Handler{mux.GET("/hello", denco.HandlerFunc(dencoHandler))})
http.ListenAndServe(":"+strconv.Itoa(port), handler)
}
// echov1
func echov1Handler(c *echo.Context) error {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
c.Response().Write(message)
return nil
}
func startEchoV1() {
mux := echo.New()
mux.Get("/hello", echov1Handler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// echov2-standard
func echov2Handler(c echov2.Context) error {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
c.Response().Write(message)
return nil
}
func startEchoV2Standard() {
mux := echov2.New()
mux.Get("/hello", echov2Handler)
mux.Run(echov2standard.New(":" + strconv.Itoa(port)))
}
// echov2-fasthttp
func startEchoV2Fasthttp() {
mux := echov2.New()
mux.Get("/hello", echov2Handler)
mux.Run(echov2fasthttp.New(":" + strconv.Itoa(port)))
}
//fasthttp
func fastHttpRawHandler(ctx *fasthttp.RequestCtx) {
if string(ctx.Method()) == "GET" {
switch string(ctx.Path()) {
case "/hello":
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
ctx.Write(message)
default:
ctx.Error("Unsupported path", fasthttp.StatusNotFound)
}
return
}
ctx.Error("Unsupported method", fasthttp.StatusMethodNotAllowed)
}
func startFasthttp() {
fasthttp.ListenAndServe(":"+strconv.Itoa(port), fastHttpRawHandler)
}
//fasthttprouter
func fastHttpHandler(ctx *fasthttp.RequestCtx, ps fasthttprouter.Params) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
ctx.Write(message)
}
func startFastHttpRouter() {
mux := fasthttprouter.New()
mux.GET("/hello", fastHttpHandler)
fasthttp.ListenAndServe(":"+strconv.Itoa(port), mux.Handler)
}
//fasthttprouting
func fastHttpRoutingHandler(c *routing.Context) error {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
c.Write(message)
return nil
}
func startFastHttpRouting() {
mux := routing.New()
mux.Get("/hello", fastHttpRoutingHandler)
fasthttp.ListenAndServe(":"+strconv.Itoa(port), mux.HandleRequest)
}
// gin
func ginHandler(c *gin.Context) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
c.Writer.Write(message)
}
func startGin() {
gin.SetMode(gin.ReleaseMode)
mux := gin.New()
mux.GET("/hello", ginHandler)
mux.Run(":" + strconv.Itoa(port))
}
// gocraftWeb
type gocraftWebContext struct{}
func gocraftWebHandler(w web.ResponseWriter, r *web.Request) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startGocraftWeb() {
mux := web.New(gocraftWebContext{})
mux.Get("/hello", gocraftWebHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// goji
func gojiHandler(c goji.C, w http.ResponseWriter, r *http.Request) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startGoji() {
mux := goji.New()
mux.Get("/hello", gojiHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// gojiv2
func gojiv2Handler(ctx gcontext.Context, w http.ResponseWriter, r *http.Request) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startGojiv2() {
mux := gojiv2.NewMux()
mux.HandleFuncC(gojiv2pat.Get("/hello"), gojiv2Handler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// goJsonRest
func goJsonRestHandler(w rest.ResponseWriter, req *rest.Request) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
iow := w.(io.Writer)
iow.Write(message)
}
func startGoJsonRest() {
api := rest.NewApi()
router, _ := rest.MakeRouter(
&rest.Route{HttpMethod: "GET", PathExp: "/hello", Func: goJsonRestHandler},
)
api.SetApp(router)
http.ListenAndServe(":"+strconv.Itoa(port), api.MakeHandler())
}
// goRestful
func goRestfulHandler(r *restful.Request, w *restful.Response) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startGoRestful() {
wsContainer := restful.NewContainer()
ws := new(restful.WebService)
ws.Route(ws.GET("/hello").To(goRestfulHandler))
wsContainer.Add(ws)
http.ListenAndServe(":"+strconv.Itoa(port), wsContainer)
}
// gorilla
func startGorilla() {
mux := mux.NewRouter()
mux.HandleFunc("/hello", helloHandler).Methods("GET")
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// httprouter
func httpRouterHandler(w http.ResponseWriter, _ *http.Request, ps httprouter.Params) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startHttpRouter() {
mux := httprouter.New()
mux.GET("/hello", httpRouterHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// httpTreeMux
func httpTreeMuxHandler(w http.ResponseWriter, _ *http.Request, vars map[string]string) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func starthttpTreeMux() {
mux := httptreemux.New()
mux.GET("/hello", httpTreeMuxHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// iris
func irisHandler(c *iris.Context) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
c.SetBody(message)
}
func startIris() {
mux := iris.New()
mux.Get("/hello", irisHandler)
mux.Listen(":" + strconv.Itoa(port))
}
// lars
func larsHandler(c lars.Context) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
c.Response().Write(message)
}
func startLars() {
mux := lars.New()
mux.Get("/hello", larsHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux.Serve())
}
// lion
func lionHandler(c gcontext.Context, w http.ResponseWriter, r *http.Request) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startLion() {
mux := lion.New()
mux.GetFunc("/hello", lionHandler)
mux.Run(":" + strconv.Itoa(port))
}
// Macaron
func macaronHandler(c *macaron.Context) string {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
return messageStr
}
func startMacaron() {
mux := macaron.New()
mux.Get("/hello", macaronHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// Martini
func martiniHandlerWrite(params martini.Params) string {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
return messageStr
}
func startMartini() {
mux := martini.NewRouter()
mux.Get("/hello", martiniHandlerWrite)
martini := martini.New()
martini.Action(mux.Handle)
http.ListenAndServe(":"+strconv.Itoa(port), martini)
}
// pat
func startPat() {
mux := pat.New()
mux.Get("/hello", http.HandlerFunc(helloHandler))
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// Possum
func possumHandler(c *possum.Context) error {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
c.Response.Write(message)
return nil
}
func startPossum() {
mux := possum.NewServerMux()
mux.HandleFunc(possumrouter.Simple("/hello"), possumHandler, possumview.Simple("text/html", "utf-8"))
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// R2router
func r2routerHandler(w http.ResponseWriter, req *http.Request, params r2router.Params) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startR2router() {
mux := r2router.NewRouter()
mux.Get("/hello", r2routerHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
//Tango
func tangoHandler(ctx *tango.Context) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
ctx.Write(message)
}
func startTango() {
llog.SetOutput(new(mockResponseWriter))
llog.SetOutputLevel(llog.Lnone)
mux := tango.NewWithLog(llog.Std)
mux.Get("/hello", tangoHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// Tiger Tonic
func startTigerTonic() {
mux := tigertonic.NewTrieServeMux()
mux.Handle("GET", "/hello", http.HandlerFunc(helloHandler))
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// traffic
func trafficHandler(w traffic.ResponseWriter, r *traffic.Request) {
if sleepTime > 0 {
time.Sleep(sleepTimeDuration)
}
w.Write(message)
}
func startTraffic() {
traffic.SetVar("env", "bench")
mux := traffic.New()
mux.Get("/hello", trafficHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// vulcan
func startVulcan() {
mux := vulcan.NewMux()
expr := fmt.Sprintf(`Method("%s") && Path("%s")`, "GET", "/hello")
mux.HandleFunc(expr, helloHandler)
http.ListenAndServe(":"+strconv.Itoa(port), mux)
}
// mock
type mockResponseWriter struct{}
func (m *mockResponseWriter) Header() (h http.Header) {
return http.Header{}
}
func (m *mockResponseWriter) Write(p []byte) (n int, err error) {
return len(p), nil
}
func (m *mockResponseWriter) WriteString(s string) (n int, err error) {
return len(s), nil
}
func (m *mockResponseWriter) WriteHeader(int) {}