bench fasthttp 和 庫自帶的net.http效能,fasthttp完敗?

codinghxl發表於2019-11-15

bench fasthttp 和 net.http 效能,fasthttp 完敗?

和官方號稱的比庫 http 快 10 倍,虛假的嗎?

測試程式碼如下:


import (

"github.com/valyala/fasthttp"

"net/http"

"testing"

)

const url = "https://api.fcoin.pro/v2/market/ticker/btcusdt"

func BenchmarkFastHttpGet(b *testing.B) {

b.ResetTimer()

for i := 0; i < b.N; i++ {

fasthttp.Get(nil, url)

}

}


func BenchmarkNetHttp(b *testing.B) {

b.ResetTimer()

for i := 0; i < b.N; i++ {

http.Get(url)

}

}


測試結果如下:

 > go test -bench . -benchtime 10s

goos: darwin

goarch: amd64

pkg: github.com/nntaoli/fasthttp_test

BenchmarkFastHttpGet-8         30  367232385 ns/op

BenchmarkGoEx-8                   50   291842516 ns/op

PASS

ok  github.com/nntaoli/fasthttp_test 38.366s


更多原創文章乾貨分享,請關注公眾號
  • bench fasthttp 和 庫自帶的net.http效能,fasthttp完敗?
  • 加微信實戰群請加微信(註明:實戰群):gocnio

相關文章