squeaky-pl/japronto

Fix Golang code for Benchmark

Closed this issue · 4 comments

Please fix the golang code for benchmark purpose -

package main
import (
    "net/http"
)

func main() {
	http.HandleFunc("/", func hello(w http.ResponseWriter, r *http.Request) {
	    w.Write([]byte("Hello world!"))
        })
	http.ListenAndServe("0.0.0.0:8080", nil)
}

Use Above code and please test the bechmark of golang with japronto. Same goes with fasthttp

This was discussed many times. The benchmark purpose is comparing single-core performance. Anyway if you are looking for multi core numbers these were done: #14

@squeaky-pl - Are you serious ? Above code is single-core performance. But using unwated if code unwanted variable creation.

package main
import "net/http"

/*
** Below Code is not required .
var (
	helloResp    = []byte("Hello world!")
	notFoundResp = []byte("Not Found")
)
*/
func hello(w http.ResponseWriter, r *http.Request) {

/*
* ===   Seriously ???  Why did you forget more of "if" with    if r.URL.Path != "404"
* === , "500", "401" and all other variant too  ====   This is un wanted call below.

	if r.URL.Path != "/" {
		w.WriteHeader(http.StatusNotFound)
		w.Write(notFoundResp)
		return
	}

*/

// You can simply call []byte("Hello World") here without any variable creation. 

//	w.Write(helloResp)

w.Write([]byte("Hello World"))
}

func main() {
        // You can call `func` right here inside `HandleFunc` but many people prefer to use same way. 
       // So, i will leave that as it is.
	http.HandleFunc("/", hello)
	http.ListenAndServe("0.0.0.0:8080", nil)
}

Above is your code . Can you see difference ?? Doing same work running on "single-core performance" but code and performance will vary a lot.

Especially when getting nodejs result as closer to golang itself looks funny.

Please compare this with other benchmarks, they all have the same logic for 404. Go won't do this by default so it needs to be added. Otherwise it is unfair. Also reading a global variable will be faster because it reduces pressure on Go garbage collector.

The particular global variable change was done here: https://github.com/squeaky-pl/japronto/pull/12/files and measured 18% speed up for Go.