Skip to content

Instantly share code, notes, and snippets.

@lcaballero
Last active December 20, 2015 12:39
Show Gist options
  • Save lcaballero/6133101 to your computer and use it in GitHub Desktop.
Save lcaballero/6133101 to your computer and use it in GitHub Desktop.
Go version of AB
package main
import (
"flag"
"io/ioutil"
"http"
"time"
"fmt"
"os"
)
var url string
func get(start, done chan bool) {
var err os.Error
var resp *http.Response
for {
<-start
resp, _, err = http.Get(url)
if err != nil {
fmt.Printf("%v\n", err)
} else {
ioutil.ReadAll(resp.Body)
resp.Body.Close()
}
done <- true
}
}
func main() {
n := flag.Int("n", 1, "total number of requests")
c := flag.Int("c", 1, "number of parallel requests")
flag.Parse()
url = flag.Args()[0] // no defaults
loop := *n / *c
done := make(chan bool, *n)
start := make(chan bool, *n)
b := time.Nanoseconds()
for i := 0; i < *c; i++ {
go get(start, done)
start <- true // start some goroutines immediately
}
for i := *c; i < *n; i++ {
start <- true // fill in the chan so everybody can work
}
for i := 0; i < *n; i++ {
<-done
if i % loop == 0 {
fmt.Printf("%d reqs end in %d msecs\n", i, (time.Nanoseconds()-b)/1e6)
}
}
e := time.Nanoseconds()
fmt.Printf("time used %d\n", (e-b)/1e6)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment