Skip to content

Commit

Permalink
Added a get config endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
Jahaja committed Sep 19, 2020
1 parent 838bc75 commit 178aa40
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 61 deletions.
98 changes: 54 additions & 44 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion http.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"net/http/cookiejar"
"net/url"
"strings"
"time"
)

type httpClientContextKeyType int
Expand All @@ -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{
Expand Down
30 changes: 14 additions & 16 deletions loadtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)")
Expand All @@ -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
}
Expand Down
20 changes: 20 additions & 0 deletions restapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)
})
Expand Down

0 comments on commit 178aa40

Please sign in to comment.