Google Go:为什么http服务器包不能同时提供超过5个请求?

时间:2011-09-27 18:20:05

标签: windows http go httpserver

我正在尝试编写一个小型http服务器,以便以后在Google的Go语言中进行扩展。我在Windows上使用Go(MinGw编译版)。

这种语言非常简单,因为它已经有了必要的包:

package main

import (
    "http"
    "io"
    "os"
    "fmt"
    "strconv"
)  


func FileTest(w http.ResponseWriter, req *http.Request) {
    w.Header().Add("Content-Type", "image/jpeg")
    w.Header().Add("Content-Disposition", "inline; filename=image.jpg")
    inName := "d:\\googlego\\somepic.jpg";
    inFile, inErr := os.OpenFile(inName, os.O_RDONLY, 0666);
    if inErr == nil {
        inBufLen := 16;
        inBuf := make([]byte, inBufLen);

        _, inErr := inFile.Read(inBuf);
        for inErr == nil {
            w.Write(inBuf)
            _, inErr = inFile.Read(inBuf);
        } 
    }
    inErr = inFile.Close(); 

}

func MainPage(w http.ResponseWriter, req *http.Request) {
    io.WriteString(w, "Hi, download here: <a href=\"/FileTest\">HERE</a>")

}


func main() {
    fmt.Print("Port: ")
    var hi int 
    fmt.Scanf("%d", &hi)
    http.HandleFunc("/FileTest", FileTest)
    http.HandleFunc("/", MainPage)

    err := http.ListenAndServe("0.0.0.0:" + strconv.Itoa(hi), nil)

    if err != nil {
        fmt.Print(err) 
        fmt.Print((hi))
    }
}

这将启动一个服务器,该服务器提供主页面和从图像下载。两者都运行良好,我从ab(Apache基准测试)获得了非常好的结果,最多6个并发线程:

> ab -n 10000 -c 6 http://localhost:8080/
Concurrency Level:      6
Time taken for tests:   1.678096 seconds
Complete requests:      10000

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      1
  80%      1
  90%      2
  95%      2
  98%      2
  99%      2
 100%      3 (longest request)

当并发级别设置得更高时,会发生这种情况:

>ab -n 1000 -c 7 http://localhost:8080/
Concurrency Level:      7
Time taken for tests:   10.239586 seconds
Complete requests:      1000

Percentage of the requests served within a certain time (ms)
  50%      1
  66%      2
  75%      2
  80%      3
  90%    499
  95%    505
  98%    507
  99%    507
 100%    510 (longest request)

请注意,这次我只提出了1 000个请求,但它仍然花费了近6倍的时间。

这两个基准测试甚至都没有请求该文件。

我对Go还不是很了解,但似乎Go运行时没有创建足够的操作系统线程来放置gorout,或类似的东西?

编辑:我从07.10.2011下载了新的r60.2。

现在它甚至更糟

>ab -c 7 -n 1000 http://localhost:8080/
Concurrency Level:      7
Time taken for tests:   12.622722 seconds
Complete requests:      1000
Percentage of the requests served within a certain time (ms)
  50%      1
  66%      1
  75%      2
  80%      2
  90%    496
  95%    503
  98%    506
  99%    506
 100%    507 (longest request)

2 个答案:

答案 0 :(得分:4)

截至今天(2011年9月),Go的Windows端口正在进行中。它在一些重要的措施(包括稳定性和性能)方面落后于其他支持的平台(Linux等)(尽管它每天都在改进)。我建议您在64位Linux平台上尝试测试,看看它有何不同,然后也许您可以开始解构Windows下出现的问题。

答案 1 :(得分:2)

我刚刚尝试使用Go 64位的基准测试,并获得了以下结果(在Core 2 Duo 2GHz,Windows 7 x64上):

C:\Program Files (x86)\Apache Software Foundation\Apache2.2\bin>ab -c 7 -n 1000 http://localhost:8080/
Concurrency Level:      7
Time taken for tests:   0.458 seconds
Complete requests:      1000
Percentage of the requests served within a certain time (ms)
  50%      3
  66%      3
  75%      3
  80%      3
  90%      4
  95%      5
  98%      7
  99%      8
 100%      9 (longest request)