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

发布于 2024-12-06 23:07:38 字数 2639 浏览 0 评论 0原文

我正在尝试编写一个小型 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 运行时似乎没有创建足够的操作系统线程来放置 goroutine,或者类似的东西?

编辑:我从 2011 年 10 月 7 日下载了新的 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)

I'm trying to code a small http server for later extension in Google's Go language. I am using Go on Windows (MinGw compiled version).

This is quite easy in this language since it already has the necessary package:

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))
    }
}

This starts a server that serves a main page and a download from an image. Both work very well and I get very good results from ab (Apache benchmark) up to 6 concurrent threads:

> 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)

When the concurrency level is set higher, this happens:

>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)

Note that I only made 1'000 requests this time and it still took almost 6 times as much time.

Both benchmarks don't even request the file yet.

I don't know a lot about Go yet, but it seems that the Go runtime doesn't create enough OS threads to put the goroutines on, or something like that?

EDIT: I downloaded the new r60.2 from 07.10.2011.

Now it went even worse:

>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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

っ〆星空下的拥抱 2024-12-13 23:07:38

截至今天(2011 年 9 月),Go 的 Windows 端口仍在开发中。它在稳定性和性能等一些重要指标上落后于其他支持的平台(Linux 等)(尽管它每天都在进步)。我建议你在 64 位 Linux 平台上尝试测试,看看它有何不同,然后也许你可以开始解构 Windows 下的问题所在。

As of today (Sep 2011) the Windows port of Go is a work-in-progress. It lags behind the other supported platforms (Linux, etc.) in some important measures including stability and performance (though it is improving every day). I would suggest that you try your test on a 64-bit Linux platform, and see how it differs, then maybe you can start deconstructing what's going wrong under Windows.

差↓一点笑了 2024-12-13 23:07:38

我刚刚在 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)

I just tried this benchmark with Go 64-bit at tip, and got the following results (on a 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文