diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 0e00edb..8292468 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -3,9 +3,9 @@
+
-
@@ -62,18 +62,18 @@
-
+
-
+
-
+
-
+
@@ -91,7 +91,8 @@
-
+
+
true
@@ -101,14 +102,15 @@
-
-
+
+
-
+
+
-
+
@@ -117,38 +119,46 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -157,10 +167,10 @@
-
+
-
+
diff --git a/http.go b/http.go
index 7b567ea..018b3fe 100644
--- a/http.go
+++ b/http.go
@@ -11,6 +11,7 @@ import (
"net/http/cookiejar"
"net/url"
"strings"
+ "time"
)
type httpClientContextKeyType int
@@ -25,7 +26,7 @@ func NewHTTPClient(ctx context.Context, baseURI string) *HTTPClient {
lt := FromContext(ctx)
std_client := &http.Client{
- Timeout: lt.Config.RequestTimeout,
+ Timeout: time.Second * time.Duration(lt.Config.RequestTimeout),
Jar: jar,
}
client := &HTTPClient{
diff --git a/loadtest.go b/loadtest.go
index f667abf..67c29ac 100644
--- a/loadtest.go
+++ b/loadtest.go
@@ -18,28 +18,28 @@ var loadtestContextKey loadtestContextKeyType
type Config struct {
// Host to bind the REST API to. default all (empty string).
- APIHost string
+ APIHost string `json:"api_host"`
// Port to bind the REST API to, default 4141
- APIPort int
- NumUsers int
+ APIPort int `json:"api_port"`
+ NumUsers int `json:"num_users"`
// How many users to spawn each second
- NumSpawnPerSecond int
+ NumSpawnPerSecond int `json:"num_spawn_per_second"`
// Default 10 seconds
- RequestTimeout time.Duration
+ RequestTimeout int `json:"request_timeout"`
// Custom user type to override the DefaultUser
- UserType User
+ UserType User `json:"-"`
// Min sleep time between tasks in seconds
- MinSleepTime int
+ MinSleepTime int `json:"min_sleep_time"`
// Max sleep time between tasks in seconds
- MaxSleepTime int
+ MaxSleepTime int `json:"max_sleep_time"`
// Verbose logging
- Verbose bool
+ Verbose bool `json:"verbose"`
// If we should start spawning users on startup
- SpawnOnStartup bool
+ SpawnOnStartup bool `json:"spawn_on_startup"`
// Logging params
- LogOutput io.Writer
- LogPrefix string
- LogFlags int
+ LogOutput io.Writer `json:"-"`
+ LogPrefix string `json:"log_prefix"`
+ LogFlags int `json:"log_flags"`
}
type StatusType int
@@ -195,11 +195,10 @@ type LoadTest struct {
}
func NewConfigFromFlags() Config {
- var req_timeout int
conf := Config{}
flag.IntVar(&conf.NumUsers, "num-users", 5, "Number of users to spawn")
- flag.IntVar(&req_timeout, "request-timeout", 5000, "Request timeout in ms (Default 5000)")
+ flag.IntVar(&conf.RequestTimeout, "request-timeout", 5, "Request timeout in seconds (Default 5)")
flag.IntVar(&conf.MinSleepTime, "min-sleep-time", 1, "Minimum sleep time between a user's tasks in seconds (Default 1)")
flag.IntVar(&conf.MaxSleepTime, "max-sleep-time", 10, "Maximum sleep time between a user's tasks in seconds (Default 10)")
flag.IntVar(&conf.NumSpawnPerSecond, "num-spawn-per-sec", 1, "Number of user to spawn per second (Default 1)")
@@ -210,7 +209,6 @@ func NewConfigFromFlags() Config {
flag.BoolVar(&conf.SpawnOnStartup, "spawn-on-startup", false, "If true, spawning will begin on startup (Default false)")
flag.Parse()
- conf.RequestTimeout = time.Millisecond * time.Duration(req_timeout)
if conf.LogOutput == nil {
conf.LogOutput = os.Stdout
}
diff --git a/restapi.go b/restapi.go
index 5b273ee..1f74c91 100644
--- a/restapi.go
+++ b/restapi.go
@@ -4,9 +4,24 @@ import (
"encoding/json"
"fmt"
"net/http"
+ "strconv"
)
func RunAPIServer(lt *LoadTest) error {
+ http.HandleFunc("/config", func(writer http.ResponseWriter, request *http.Request) {
+ data, err := json.Marshal(lt.Config)
+
+ if err != nil {
+ lt.Log.Printf("error marshalling config: %s\n", err.Error())
+ writer.WriteHeader(http.StatusInternalServerError)
+ return
+ }
+
+ writer.Header().Set("Content-Type", "application/json")
+ writer.WriteHeader(http.StatusOK)
+ writer.Write(data)
+ })
+
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
lt.Stats.Lock()
lt.Stats.Calculate()
@@ -25,6 +40,11 @@ func RunAPIServer(lt *LoadTest) error {
})
http.HandleFunc("/start", func(writer http.ResponseWriter, request *http.Request) {
+ numUsers, _ := strconv.Atoi(request.URL.Query().Get("num-users"))
+ if numUsers > 0 {
+ lt.Config.NumUsers = numUsers
+ }
+
lt.SetStatus(StatusSpawning)
writer.WriteHeader(http.StatusOK)
})