Created
July 10, 2017 18:31
-
-
Save danielberlinger/1bdf2e9c419164ef73b6fdc4763a7898 to your computer and use it in GitHub Desktop.
This file contains hidden or 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" | |
"github.com/leesper/go_rng" | |
"io" | |
"io/ioutil" | |
"log" | |
"net/http" | |
"time" | |
) | |
var delay = 100 * time.Millisecond | |
var N = 100 | |
var r *rng.PoissonGenerator | |
type delayFunc func() float32 | |
func constant() float32 { | |
return 1 | |
} | |
func uniform() float32 { | |
return rand.Float32() | |
} | |
func poisson() float32 { | |
return float32(r.Poisson(1)) | |
} | |
func makeRequest() { | |
url := "http://localhost:5000/" | |
resp, err := http.Get(url) | |
if err != nil { | |
log.Fatal(err) | |
} | |
io.Copy(ioutil.Discard, resp.Body) | |
resp.Body.Close() | |
if resp.StatusCode == http.StatusOK { | |
fmt.Printf(".") | |
} else { | |
fmt.Printf("🔥") | |
} | |
} | |
func Loop(name string, fn delayFunc) { | |
fmt.Println(name) | |
for i := 0; i < N; i++ { | |
v := fn() * float32(delay) | |
time.Sleep(time.Duration(v)) | |
makeRequest() | |
} | |
fmt.Println() | |
} | |
func init() { | |
seed := time.Now().UnixNano() | |
rand.Seed(seed) | |
r = rng.NewPoissonGenerator(seed) | |
go func() { | |
for { | |
fmt.Printf(" ") | |
time.Sleep(delay) | |
} | |
}() | |
} | |
func main() { | |
Loop("Constant", constant) | |
Loop("Uniform", uniform) | |
Loop("Poisson", poisson) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment