Created
December 3, 2014 14:49
-
-
Save nikhilofthesouth/ef89e30dbee942b06060 to your computer and use it in GitHub Desktop.
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 ( | |
"fmt" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"os" | |
"strconv" | |
"time" | |
) | |
const NANO_TO_MILLI = 1000 * 1000 | |
func main() { | |
if len(os.Args) != 4 { | |
fmt.Println("usage:", os.Args[0], "url numTests numRequests") | |
os.Exit(3) | |
} | |
numTests, error := strconv.Atoi(os.Args[2]) | |
if error != nil { | |
log.Fatal(error) | |
os.Exit(1) | |
} | |
numRequests, error := strconv.Atoi(os.Args[3]) | |
if error != nil { | |
log.Fatal(error) | |
os.Exit(2) | |
} | |
transport := &http.Transport{ | |
DisableKeepAlives: false, | |
MaxIdleConnsPerHost: numRequests, | |
} | |
client := &http.Client{ | |
Transport: transport, | |
} | |
for i := 0; i < numTests; i++ { | |
runTest(client, numRequests) | |
} | |
} | |
func runTest(client *http.Client, numRequests int) { | |
times := make(chan int64) | |
for i := 0; i < numRequests; i++ { | |
go getPing(client, times) | |
} | |
totalTime := int64(0) | |
for i := 0; i < numRequests; i++ { | |
time := <-times | |
totalTime += time | |
} | |
timeNanoseconds := float64(totalTime) / float64(numRequests) | |
timeMilliseconds := timeNanoseconds / NANO_TO_MILLI | |
fmt.Println("avg request time:", timeMilliseconds, "ms") | |
} | |
func getPing(client *http.Client, times chan int64) { | |
start := time.Now() | |
response, error := client.Get(os.Args[1]) | |
if error != nil { | |
log.Fatal(error) | |
} | |
_, error = ioutil.ReadAll(response.Body) | |
response.Body.Close() | |
times <- time.Since(start).Nanoseconds() | |
if error != nil { | |
log.Fatal(error) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment