Last active
March 27, 2019 04:02
-
-
Save olivernadj/d6bc21ec17ee15383a9c0f10bbca1e24 to your computer and use it in GitHub Desktop.
Simplified Golang example of Apache HTTP server benchmarking tool
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 ( | |
"flag" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"os" | |
"time" | |
) | |
type responseInfo struct { | |
status int | |
bytes int64 | |
duration time.Duration | |
} | |
type summaryInfo struct { | |
requested int64 | |
responded int64 | |
} | |
func main() { | |
fmt.Println("Hello from my app") | |
requests := flag.Int64("n", 1, "Number of requests to perform") | |
concurrency := flag.Int64("c", 1, "Number of multiple requests to make at a time") | |
fmt.Println(requests, concurrency) | |
flag.Parse() | |
if flag.NArg() == 0 || *requests == 0 || *requests < *concurrency { | |
flag.PrintDefaults() | |
os.Exit(-1) | |
} | |
link := flag.Arg(0) | |
c := make(chan responseInfo) | |
summary := summaryInfo{} | |
for i := int64(0); i < *concurrency; i++ { | |
summary.requested++ | |
go checkLink(link, c) | |
} | |
for response := range c { | |
if summary.requested < *requests { | |
summary.requested++ | |
go checkLink(link, c) | |
} | |
summary.responded++ | |
fmt.Println(response) | |
if summary.responded == summary.requested { | |
break | |
} | |
} | |
} | |
func checkLink(link string, c chan responseInfo) { | |
start := time.Now() | |
res, err := http.Get(link) | |
if err != nil { | |
panic(err) | |
} | |
read, _ := io.Copy(ioutil.Discard, res.Body) | |
c <- responseInfo{ | |
status:res.StatusCode, | |
bytes:read, | |
duration:time.Now().Sub(start), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment