Java and Golang http performance compare : <p>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?</p>

<p>JAVA environment:</p>

<p>JDK1.8</p>

<p>Springboot2.1.7.RELEASE</p>

<p>Golang environment:</p>

<p>go1.9.3</p>

<h1>JAVA service1(LISTEN 10006 PORT):</h1>

<pre><code>        @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;
    }
</code></pre>

<h1>JAVA service2(LISTEN 10007 PORT):</h1>

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

<h1>Golang service1(LISTEN 10008 PORT):</h1>

<pre><code>        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)
}
</code></pre>

<h1>Golang service2(LISTEN 10009 PORT):</h1>

<pre><code>        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))
        }
</code></pre>

<p>I used browser to send request like this:</p>

<p><a href="http://localhost:10006/hello?name=tom" rel="nofollow noreferrer">http://localhost:10006/hello?name=tom</a></p>

<p><a href="http://localhost:10008/hello?name=tom" rel="nofollow noreferrer">http://localhost:10008/hello?name=tom</a></p>

<p>The result is mostly like the following</p>

<p>JAVA service1：</p>

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

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

<p>JAVA service2：</p>

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

<p>Golang service1:</p>

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

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

<p>Golang service2:</p>

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