Skip to content

Instantly share code, notes, and snippets.

@mmirolim
Created July 31, 2014 03:06
Show Gist options
  • Save mmirolim/f1019fa2f2e97a8a1dfa to your computer and use it in GitHub Desktop.
Save mmirolim/f1019fa2f2e97a8a1dfa to your computer and use it in GitHub Desktop.
Simple load producer
package main
import (
"fmt"
"flag"
//"errors"
//"os"
//"strings"
"strconv"
"math/rand"
"time"
//"net/http"
)
type Visitor struct {
id int
r string
pg string
c string
j string
wh string
px int
js float64
col string
t string
p string
}
func main() {
start := time.Now()
var h = flag.String("w", "http://localhost/", "Address of resource")
var r = flag.Int("r", 1, "Requests per second")
var t = flag.Int("t", 1, "Duration in seconds")
flag.Parse()
/*var ErrHelp = errors.New("flag: help requested")
var Usage = func () {
fmt.Fprintf(os.Stderr, "Usage of %s:\n", os.Args[0])
flag.PrintDefaults()
}*/
// set of monitor resolutions
res := []string{"340x480", "420x640", "600x1024", "720x1280", "1050x1680", "1080x1980"}
// set of colors
colors := []string{"FFFFFF", "FFCC00", "FF3399", "9933FF", "6600FF", "00FF00"}
// js versions
js := []float64{1.3, 1.2, 1.1, 1.0}
// set pointer to visitor
v := new(Visitor)
// show set flags
fmt.Println("Target", *h, "Request/second", *r, "Duration", *t)
// set timers
rps := time.Duration(1000/(*r))
// set duration of requests
d := time.Duration(*t)
// set ticker interval
ticker := time.NewTicker(time.Millisecond * rps)
var i = 1;
go func () {
for t := range ticker.C {
v.id = i
v.r = res[rand.Intn(len(res))]
v.col = colors[rand.Intn(len(res))]
v.js = js[rand.Intn(len(js))]
url := *h + "?id=" + strconv.Itoa(v.id) + "&r=" + v.r + "&j=" + strconv.FormatFloat(v.js, 'f', -1, 32) + "&col=" + v.col
fmt.Println("URL =>", url, t)
i++
}
}()
time.Sleep(time.Second * d)
ticker.Stop()
fmt.Println("Time spent", time.Since(start))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment