Commit d755c2db authored by Kirill Smelkov's avatar Kirill Smelkov Committed by Bryton Lacquement

golang: Switch to fasthttp by default

Standard net/http is known to be putting more focus on idiomaticy instead
of performance. Use github.com/valyala/fasthttp whose main focus is
performance. It is still all pure go, but it is different Go code.

On my laptop the result for / is:

(-fast=0):

	kirr@deco:~/src/tools/net/wrk$ ./wrk -t 4 -c 40 -d 20 http://127.0.0.1:8080
	Running 20s test @ http://127.0.0.1:8080
	  4 threads and 40 connections
	  Thread Stats   Avg      Stdev     Max   +/- Stdev
	    Latency     1.02ms    2.58ms  63.90ms   93.04%
	    Req/Sec    27.33k     3.98k   39.64k    68.38%
	  2184698 requests in 20.09s, 270.85MB read
	Requests/sec: 108752.84
	Transfer/sec:     13.48MB

(-fast=1):

	kirr@deco:~/src/tools/net/wrk$ ./wrk -t 4 -c 40 -d 20 http://127.0.0.1:8080
	Running 20s test @ http://127.0.0.1:8080
	  4 threads and 40 connections
	  Thread Stats   Avg      Stdev     Max   +/- Stdev
	    Latency   284.88us  645.44us  20.31ms   96.55%
	    Req/Sec    48.97k     4.16k   71.85k    76.75%
	  3900962 requests in 20.02s, 550.60MB read
	Requests/sec: 194809.91
	Transfer/sec:     27.50MB

i.e. it is ~ 1.8x improvement.

I tested this only briefly and had not looked in detail what
improvements could also be there.

/cc @bminusl, @jp

/reviewed-on !1
parent c82c941f
package main
import (
"flag"
"fmt"
"net/http"
"github.com/valyala/fasthttp"
//"runtime"
)
......@@ -25,10 +27,52 @@ func handle_fibonacci(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Fibonacci(10^6) = %d (with overflow)\n", fibonacci(1000000))
}
func main() {
// mainStdHTTP serves HTTP via net/http.
func mainStdHTTP() {
//runtime.GOMAXPROCS(2)
http.HandleFunc("/", handle_root)
http.HandleFunc("/fibonacci", handle_fibonacci)
fmt.Println("Serving on :8080")
fmt.Println("Serving on :8080 (net/http)")
http.ListenAndServe(":8080", nil)
}
// ---- fast http handlers ----
func fastHandleRoot(ctx *fasthttp.RequestCtx) {
fmt.Fprint(ctx, "Hello, World!")
}
func fastHandleFibo(ctx *fasthttp.RequestCtx) {
fmt.Fprintf(ctx, "Fibonacci(10^6) = %d (with overflow)\n", fibonacci(1000000))
}
// mainFastHTTP serves HTTP via fasthttp.
func mainFastHTTP() {
m := func(ctx *fasthttp.RequestCtx) {
switch string(ctx.Path()) {
case "/":
fastHandleRoot(ctx)
case "/fibonacci":
fastHandleFibo(ctx)
default:
ctx.Error("not found", fasthttp.StatusNotFound)
}
}
fmt.Println("Serving on :8080 (fasthttp)")
fasthttp.ListenAndServe(":8080", m)
}
func main() {
fast := flag.Bool("fast", true, "use fasthttp instead of net/http")
flag.Parse()
if *fast {
mainFastHTTP()
} else {
mainStdHTTP()
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment