Created
June 29, 2019 11:07
-
-
Save monsterxx03/8217a664d301a1b3c07c983eefcb1a3b to your computer and use it in GitHub Desktop.
dnsbench
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"net" | |
"time" | |
) | |
var ( | |
concurrency = flag.Int("c", 100, "concurrency requests") | |
totalReq = flag.Int("n", 10000, "total requests") | |
slowSec = flag.Float64("s", 5, "response larger than this value, considered as slow") | |
host = flag.String("d", "google.com", "host to query") | |
) | |
func main() { | |
flag.Parse() | |
slowCount := 0 | |
sem := make(chan bool, *concurrency) | |
for i := 0; i < *totalReq; i++ { | |
sem <- true | |
go func() { | |
defer func() { <-sem }() | |
resolver := &net.Resolver{} | |
t := time.Now() | |
_, err := resolver.LookupIPAddr(context.Background(), *host) | |
if err != err { | |
fmt.Println(err) | |
} | |
cost := time.Since(t).Seconds() | |
if cost >= *slowSec { | |
slowCount += 1 | |
fmt.Println("slow resp:", cost) | |
} | |
}() | |
} | |
for i := 0; i < cap(sem); i++ { | |
sem <- true | |
} | |
fmt.Println("slowCount:", slowCount) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment