Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
dnsbench
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
You can’t perform that action at this time.