Go语言的初步接触

[文章作者:磨延城 转载请注明原文出处: https://mo2g.com/view/63/ ]

虽然接触go语言有一段时间了,但都只是停留在书面上,最近突然有想法,就想体验一下go语言,于是顺手拿PHP语言来做一个参照,也许这样更容易进一步了解go语言.因为go语言生成的程序,本身就实现了web服务器的功能,为了有一个参照对比,再添加一个基准测试,测试静态html的性能.

虽然接触go语言有一段时间了,但都只是停留在书面上,最近突然有想法,就想体验一下go语言,于是顺手拿PHP语言来做一个参照,也许这样更容易进一步了解go语言。

因为go语言生成的程序,本身就实现了web服务器的功能,为了有一个参照对比,再添加一个基准测试,测试静态html的性能。所以测试如下:

1)访问nginx服务器上的html静态页面

2)访问nginx服务器上的php-fpm

3)访问go程序

其实很简单,就是用APACHE BENCHMARK(简称ab)来做压力测试,1000的并发连接数,总共10000次请求连接。

1)hello.html文件的代码如下

hello

2)hello.php文件的代码如下

<?php
echo 'hello';

3)go语言用的如下代码生成程序

package main
import(
    "fmt"
    "net/http"
)
func test(w http.ResponseWriter, r *http.Request) {
    fmt.Fprintln(w,"hello")
}
func main() {
    http.HandleFunc("/", test)
    http.ListenAndServe(":59", nil)
}

静态html的ab压力测试结果如下

[mo2g@localhost git]# ab -c 1000 -n 10000 192.168.1.9/hello.html
Server Software:        nginx/1.2.6
Server Hostname:        192.168.1.9
Server Port:            80

Document Path:          /hello.html
Document Length:        5 bytes

Concurrency Level:      1000
Time taken for tests:   1.036 seconds
Complete requests:      10000
Failed requests:        633
   (Connect: 0, Receive: 0, Length: 633, Exceptions: 0)
Write errors:           0
Non-2xx responses:      633
Total transferred:      2376600 bytes
HTML transferred:       168546 bytes
Requests per second:    9653.84 [#/sec] (mean)
Time per request:       103.586 [ms] (mean)
Time per request:       0.104 [ms] (mean, across all concurrent requests)
Transfer rate:          2240.56 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   23 118.7      7    1005
Processing:     2   14   9.6     11     234
Waiting:        0    9   7.8      8     230
Total:          6   37 119.6     18    1016

PHP程序的ab压力测试结果如下

[mo2g@localhost git]#  ab -c 1000 -n 10000 192.168.1.9/hello.php
Server Software:        nginx/1.2.6
Server Hostname:        192.168.1.9
Server Port:            80

Document Path:          /hello.php
Document Length:        5 bytes

Concurrency Level:      1000
Time taken for tests:   20.335 seconds
Complete requests:      10000
Failed requests:        2053
   (Connect: 0, Receive: 0, Length: 2053, Exceptions: 0)
Write errors:           0
Non-2xx responses:      2053
Total transferred:      2257654 bytes
HTML transferred:       427751 bytes
Requests per second:    491.76 [#/sec] (mean)
Time per request:       2033.498 [ms] (mean)
Time per request:       2.033 [ms] (mean, across all concurrent requests)
Transfer rate:          108.42 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0  105 390.2      1    3001
Processing:     0  951 2352.2    103   15020
Waiting:        0  948 2352.8    102   15020
Total:          0 1056 2495.7    109   16019

go语言的ab压力测试结果如下

[mo2g@localhost git]# ab -c 1000 -n 10000 192.168.1.9:59/
Server Software:
Server Hostname:        192.168.1.9
Server Port:            59

Document Path:          /
Document Length:        6 bytes

Concurrency Level:      1000
Time taken for tests:   1.373 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Total transferred:      1456953 bytes
HTML transferred:       61998 bytes
Requests per second:    7285.10 [#/sec] (mean)
Time per request:       137.266 [ms] (mean)
Time per request:       0.137 [ms] (mean, across all concurrent requests)
Transfer rate:          1036.53 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0   72 230.4     16    1022
Processing:     2   30  26.3     26     261
Waiting:        0   24  25.2     20     250
Total:          2  102 242.2     44    1279

1)html静态文件,9653.84 [#/sec] (mean)

2)PHP语言,491.76 [#/sec] (mean)

3)GO语言,7285.10 [#/sec] (mean)

从上边的数据很明显可以看出来,静态html的性能是最好的;php程序明显的产生了很大的内耗,性能严重下跌;而Go程序的性能非常出色,远远的超出了phper的想象,我开始有点小激动了,又幻想着自己的博客(磨途歌)性能又要提升了。

评论:

  1. 暂无评论...
  2. 我来说两句:

      切换  

    磨途歌检测发现,您当前使用的浏览器版本过低,要想使用画板模式,请先更新浏览器

      切换  

    磨途歌随机验证码