Merge pull request #13 from tevino/adjust-bench

Adjust benchmark
This commit is contained in:
Tevin 2019-03-06 16:36:16 +08:00 committed by GitHub
commit 9f5b7a96d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 4 deletions

View File

@ -3,6 +3,9 @@ os:
- osx - osx
language: go language: go
go: go:
- "master"
- "1.11.x" - "1.11.x"
install: install:
- go get "github.com/pkg/errors" - go get "github.com/pkg/errors"
script: go test -v -bench=. -benchmem ./...

View File

@ -3,8 +3,10 @@ package tcp
import ( import (
"context" "context"
"fmt" "fmt"
"net"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os"
"runtime" "runtime"
"sync" "sync"
"testing" "testing"
@ -12,10 +14,33 @@ import (
) )
const ( const (
AddrDead = "127.0.0.1:1" AddrDead = "127.0.0.1:1"
AddrTimeout = "10.255.255.1:80"
) )
var timeoutAddrs = []string{
"10.255.255.1:80",
"10.0.0.0:1",
}
var AddrTimeout = timeoutAddrs[0]
func _setAddrTimeout() {
for _, addr := range timeoutAddrs {
conn, err := net.DialTimeout("tcp", addr, time.Millisecond*50)
if err == nil {
conn.Close()
continue
}
if os.IsTimeout(err) {
AddrTimeout = addr
return
}
}
}
func init() {
_setAddrTimeout()
}
// assert calls t.Fatal if the result is false // assert calls t.Fatal if the result is false
func assert(t *testing.T, result bool) { func assert(t *testing.T, result bool) {
if !result { if !result {

View File

@ -26,7 +26,8 @@ func _benchmarkChecker(b *testing.B, c *Checker, addr string) {
b.ResetTimer() b.ResetTimer()
b.RunParallel(func(pb *testing.PB) { b.RunParallel(func(pb *testing.PB) {
for pb.Next() { for pb.Next() {
c.CheckAddr(addr, time.Second) // if timeout >= 1s, this func will run only once in the benchmark.
c.CheckAddr(addr, time.Millisecond*900)
} }
}) })
b.StopTimer() b.StopTimer()