go – Java and Golang http performance compare-ThrowExceptions

Exception or error:

I recently started using golang, and I did a test to compare the performance of java and golang http calls; I was surprised to find that golang is much faster than java, and sometimes it takes almost no time to call another service from one service. I am curious that this is due to the nature of the language or is the java framework doing too much to make it slower?

JAVA environment:

JDK1.8

Springboot2.1.7.RELEASE

Golang environment:

go1.9.3

JAVA service1(LISTEN 10006 PORT):

        @GetMapping("/hello")
    public String hello(String name) {
        log.info("in");
        String result = restTemplate.getForObject("http://localhost:10007/hello?name=" + name, String.class);
        log.info("out");
        return result;
    }

JAVA service2(LISTEN 10007 PORT):

    @GetMapping("/hello")
    public String hello(String name) {
        log.info("in");
        return "Hello " + name;
    }

Golang service1(LISTEN 10008 PORT):

        func main() {
           fmt.Println("start")
           http.HandleFunc("/hello", DefaultHandler)
           err := http.ListenAndServe(":10008", nil)
           if err != nil {
            fmt.Errorf("error: %s", err)
           }
        }
        func DefaultHandler(w http.ResponseWriter, r *http.Request) {
           name := r.FormValue("name")
           fmt.Printf("%s, %s\r\n", time.Now(), "in")
           resp, _ := http.Get("http://localhost:10009/hello?name=" + name)
           body, _ := ioutil.ReadAll(resp.Body)
           defer resp.Body.Close()
           fmt.Printf("%s, %s\r\n", time.Now(), "out")
           w.Write(body)
}

Golang service2(LISTEN 10009 PORT):

        func main() {
           fmt.Println("start")
           http.HandleFunc("/hello", DefaultHandler)
           err := http.ListenAndServe(":10009", nil)
           if err != nil {
             fmt.Errorf("error: %s", err)
           }
        }
        func DefaultHandler(w http.ResponseWriter, r *http.Request) {
           fmt.Printf("%s, %s\r\n", time.Now(), "in")
           name := r.FormValue("name")
           w.Write([]byte("Hello " + name))
        }

I used browser to send request like this:

http://localhost:10006/hello?name=tom

http://localhost:10008/hello?name=tom

The result is mostly like the following

JAVA service1:

2019-10-21 22:04:46.406 INFO 16936 — [ XNIO-1 task-12] com.web.test6.controller.TestCtrl : in

2019-10-21 22:04:46.412 INFO 16936 — [ XNIO-1 task-12] com.web.test6.controller.TestCtrl : out

JAVA service2:

2019-10-21 22:04:46.410 INFO 16200 — [ XNIO-1 task-6] com.web.test7.controller.TestCtrl : in

Golang service1:

2019-10-21 22:06:36.1888823 +0800 CST m=+1438.272728801, in

2019-10-21 22:06:36.1888823 +0800 CST m=+1438.272728801, out

Golang service2:

2019-10-21 22:06:36.1888823 +0800 CST m=+1436.497480201, in

How to solve:

Here, you are comparing:

Springboot2.1.7.RELEASE

against:

Golang environment: go1.9.3

And your Go code puts up a minimalistic HTTP service. Whereas your Java solution seems to go with a “full fledged” Spring boot instance.

Which is like: comparing a single apple against a truckload of melons.

In other words: in order to get to meaningful numbers, you should compare “about equal” context. Spring boot is “much” more than a simple HTTP server (well, unless you strip it down to just be that).

Meaning: I would suggest to not go with Spring boot, but the most “minimalistic” http server/framework you can find for java.

Leave a Reply

Your email address will not be published. Required fields are marked *