首页 文章 网络编程 使用Go开发HTTP服务器以及Benchmark测试
0
0
0
16

使用Go开发HTTP服务器以及Benchmark测试

http Benchmark 服务器 HTTP

由于最近在学习Docker,而Docker是采用Go语言开发的;早就耳闻Go语言的威名,因此准备借机学习一下

在学习了Go的原生HTTP库后,我写了个简单的HTTP服务器Demo,代码如下:

// demo.go
package main

import (
    "fmt"
    "net/http"
)

func index(w http.ResponseWriter, req *http.Request) {
    fmt.Println("DEBUG: ", req)
    fmt.Fprint(w, "hello world")
}

func main() {
    http.HandleFunc("/", index)
    err := http.ListenAndServe(":12345", nil)
    if err != nil {
        fmt.Println("ListenAndServe: ", err)
    }
}
编译后在Ubuntu上运行:

$ go build -o demo demo.go && demo
通过wget、curl或者浏览器就可以进行访问了

在配置为2GB内存、2核的Ubuntu虚拟机中对此HTTP服务器Demo进行Benchmark测试,使用的测试工具为Apache ab,测试结果如下, 可见Go语言在并发方面也是颇具威力的:

$ ab -n 10000 -c 100 http://192.168.239.148:12345/
This is ApacheBench, Version 2.3 <$Revision: 1706008 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking 192.168.239.148 (be patient)
Completed 1000 requests
Completed 2000 requests
Completed 3000 requests
Completed 4000 requests
Completed 5000 requests
Completed 6000 requests
Completed 7000 requests
Completed 8000 requests
Completed 9000 requests
Completed 10000 requests
Finished 10000 requests


Server Software:        
Server Hostname:        192.168.239.148
Server Port:            12345

Document Path:          /
Document Length:        11 bytes

Concurrency Level:      100
Time taken for tests:   5.030 seconds
Complete requests:      10000
Failed requests:        0
Total transferred:      1280000 bytes
HTML transferred:       110000 bytes
Requests per second:    1987.90 [#/sec] (mean)
Time per request:       50.304 [ms] (mean)
Time per request:       0.503 [ms] (mean, across all concurrent requests)
Transfer rate:          248.49 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    8   5.5      8      24
Processing:     0   42  95.4     15     455
Waiting:        0   38  95.4     12     451
Total:          1   50  94.7     24     466

Percentage of the requests served within a certain time (ms)
  50%     24
  66%     28
  75%     31
  80%     32
  90%     39
  95%    385
  98%    411
  99%    454
 100%    466 (longest request)


到此这篇关于“使用Go开发HTTP服务器以及Benchmark测试”的文章就介绍到这了,更多文章或继续浏览下面的相关文章,希望大家以后多多支持Go语言编程网!

相关文章

创建博客

开始创作
写作能提升自己能力,也能为他人分享知识。

在线教程

查看更多
  • Go入门指南

    Go入门指南

  • Go语言高级编程

    Go语言高级编程

  • Go Web 编程

    Go Web 编程

  • GO专家编程

    GO专家编程

  • Go语言四十二章经

    Go语言四十二章经

  • 数据结构和算法(Golang实现)

    数据结构和算法(Golang实现)

Go语言编程网

微信扫码关注订阅号


博客 资讯 教程 我的