Skip to content

Commit

Permalink
Add Api to riot
Browse files Browse the repository at this point in the history
  • Loading branch information
iknite committed Feb 19, 2019
1 parent aca281d commit 58cd7a5
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ require (
github.com/hashicorp/raft v1.0.0
github.com/hashicorp/serf v0.8.1 // indirect
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d // indirect
github.com/imdario/mergo v0.3.7 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/influxdata/tdigest v0.0.0-20181121200506-bf2b5ad3c0a9 // indirect
github.com/klauspost/compress v1.4.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,8 @@ github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/imdario/mergo v0.3.7 h1:Y+UAYTZ7gDEuOfhxKWy+dvb5dRQ6rJjFSdX2HZY1/gI=
github.com/imdario/mergo v0.3.7/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/influxdata/tdigest v0.0.0-20180711151920-a7d76c6f093a h1:vMqgISSVkIqWxCIZs8m1L4096temR7IbYyNdMiBxSPA=
Expand Down
59 changes: 53 additions & 6 deletions tests/stress.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@
package main

import (
"encoding/json"
"fmt"
"net/http"
"os"

"github.com/imdario/mergo"
"github.com/spf13/cobra"
"github.com/spf13/viper"

Expand Down Expand Up @@ -66,7 +69,7 @@ func newRiotCommand() *cobra.Command {
},
Run: func(cmd *cobra.Command, args []string) {
if APIMode {
Api(config)
Serve(config)
} else {
Run(config)
}
Expand All @@ -76,7 +79,7 @@ func newRiotCommand() *cobra.Command {
f := cmd.Flags()
f.BoolVar(&APIMode, "api", false, "Raise a HTTP api in port 11111 ")
f.StringVar(&config.Endpoint, "endpoint", "http://localhost:8800", "The endopoint to make the load")
f.StringVar(&config.ApiKey, "apikey", "my-key", "The key to use qed servers")
f.StringVar(&config.APIKey, "apikey", "my-key", "The key to use qed servers")
f.BoolVar(&config.Insecure, "insecure", false, "Allow self-signed TLS certificates")
f.BoolVar(&config.WantAdd, "add", false, "Execute add benchmark")
f.BoolVarP(&config.WantMembership, "membership", "m", false, "Benchmark MembershipProof")
Expand All @@ -96,7 +99,52 @@ func newRiotCommand() *cobra.Command {
return cmd
}

func Run(conf Config) error {
func Serve(defaultConf Config) {
mux := http.NewServeMux()
mux.HandleFunc("/run", func(w http.ResponseWriter, r *http.Request) {
if r.Method != "POST" {
w.Header().Set("Allow", "POST")
w.WriteHeader(http.StatusMethodNotAllowed)
return
}

if r.Body == nil {
http.Error(w, "Please send a request body", http.StatusBadRequest)
return
}

var newConf Config
err := json.NewDecoder(r.Body).Decode(&newConf)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

var localConf Config
if err := mergo.Merge(&localConf, defaultConf); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

if err := mergo.Merge(&localConf, newConf); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}

fmt.Printf(">>>>>>>>>>>>> %+v", localConf)
})

api := &http.Server{
Addr: ":18800",
Handler: mux,
}

if err := api.ListenAndServe(); err != http.ErrServerClosed {
log.Errorf("Can't start Riot API HTTP server: %s", err)
}
}

func Run(conf Config) {
var attack Attack

if conf.WantAdd { // nolint:gocritic
Expand All @@ -109,9 +157,9 @@ func Run(conf Config) error {

attack = Attack{
kind: "membership",
balloonVersion: uint64(conf.numRequests + conf.offset - 1),
balloonVersion: uint64(conf.NumRequests + conf.Offset - 1),
}
} else if conf.wantIncremental {
} else if conf.WantIncremental {
log.Info("Benchmark INCREMENTAL")

attack = Attack{
Expand All @@ -122,7 +170,6 @@ func Run(conf Config) error {
attack.config = conf

attack.Run()
return nil
}

type Attack struct {
Expand Down

0 comments on commit 58cd7a5

Please sign in to comment.