diff --git a/benchmarks/gen_targets.go b/benchmarks/gen_targets.go index 59705580..57ed60e1 100644 --- a/benchmarks/gen_targets.go +++ b/benchmarks/gen_targets.go @@ -1,10 +1,14 @@ package main import ( + "encoding/base64" + "encoding/json" "errors" "flag" + "fmt" "io/ioutil" "log" + "net/http" "os" "path" "path/filepath" @@ -14,6 +18,7 @@ import ( "git.andreafazzi.eu/andrea/oef/client" "git.andreafazzi.eu/andrea/oef/orm" "github.com/gocarina/gocsv" + vegeta "github.com/tsenart/vegeta/lib" ) type config struct { @@ -31,7 +36,67 @@ func incr(value int) int { return value + 1 } -func genResponseTargets(targetName string, conf *config) error { +func genPOSTResponseTargets(targetName string, conf *config) error { + var ( + participants []*orm.Participant + tokens map[string]string + ) + + log.Println("Read participants.csv...") + + input, err := ioutil.ReadFile("./testdata/participants.csv") + if err != nil { + return err + } + if err := gocsv.Unmarshal(strings.NewReader(string(input)), &participants); err != nil { + panic(err) + } + + log.Println("Obtaining tokens for each participants...") + tokens = make(map[string]string, 0) + for _, participant := range participants { + token, err := client.GetToken(conf.Url, participant.FiscalCode, participant.Password) + if err != nil { + return err + } + tokens[participant.FiscalCode] = token + } + + targets := make([]*vegeta.Target, 0) + for _, participant := range participants { + targets = append(targets, &vegeta.Target{ + Method: "POST", + URL: fmt.Sprintf("%s/responses/%d/update", conf.Url, participant.ID), + Body: []byte(base64.StdEncoding.EncodeToString([]byte("PUNCH!"))), + Header: http.Header{"Content-Type": []string{"application/x-www-form-urlencoded"}}, + }) + } + + err = os.Mkdir("targets", 0777) + if errors.Is(err, &os.PathError{}) { + return err + } + output, err := os.Create("./targets/post_response_targets.txt") + if err != nil { + return err + } + defer output.Close() + + for _, t := range targets { + jsonData, err := json.Marshal(t) + if err != nil { + return err + } + _, err = output.WriteString(string(jsonData) + "\n") + if err != nil { + return err + } + } + + return nil +} + +func genGETResponseTargets(targetName string, conf *config) error { var data struct { Participants []*orm.Participant Tokens map[string]string @@ -89,7 +154,8 @@ func genResponseTargets(targetName string, conf *config) error { func init() { generators = map[string]genFunc{ - "responses": genResponseTargets, + "responses": genGETResponseTargets, + "post_responses": genPOSTResponseTargets, } } diff --git a/config/config.go b/config/config.go index b73fb585..0f42669e 100644 --- a/config/config.go +++ b/config/config.go @@ -45,6 +45,9 @@ type ConfigT struct { LogLevel int `yaml:"log_level"` + // Enable profiling + Profiling bool + // Secret keys Keys struct { @@ -68,9 +71,9 @@ type ConfigT struct { // Handlers Handlers struct { - PathPatterns map[string]PathPattern - APIPathPatterns map[string]PathPattern - Permissions map[string]map[string][]int + PathPatterns map[string]PathPattern + APIPathPatterns map[string]PathPattern + Permissions map[string]map[string][]int AllowSessionURLQuery bool `yaml:"allow_session_url_query"` } diff --git a/docker/oef_test/config/config.yaml b/docker/oef_test/config/config.yaml index 66b04386..118f682b 100644 --- a/docker/oef_test/config/config.yaml +++ b/docker/oef_test/config/config.yaml @@ -1,6 +1,7 @@ url: "http://localhost:3000" log_level: 2 language: "it" +profiling: true keys: cookie_store_key: "something-very-secret" diff --git a/go.mod b/go.mod index 832054d7..ce5f242f 100644 --- a/go.mod +++ b/go.mod @@ -24,13 +24,14 @@ require ( github.com/kr/pretty v0.1.0 // indirect github.com/mailru/easyjson v0.7.0 // indirect github.com/monochromegane/terminal v0.0.0-20161222050454-9bc47e2707d9 // indirect + github.com/pkg/profile v1.4.0 github.com/remogatto/prettytest v0.0.0-20191105125618-8fe70ed7a3e1 github.com/robfig/cron v1.2.0 github.com/rs/jplot v0.0.0-20180624024257-9b69b4534805 // indirect github.com/sethvargo/go-password v0.1.3 github.com/smartystreets/goconvey v1.6.4 // indirect github.com/tsenart/go-tsz v0.0.0-20180814235614-0bd30b3df1c3 // indirect - github.com/tsenart/vegeta v12.7.0+incompatible // indirect + github.com/tsenart/vegeta v12.7.0+incompatible github.com/urfave/negroni v1.0.0 // indirect github.com/wcharczuk/go-chart v2.0.1+incompatible // indirect golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d // indirect diff --git a/go.sum b/go.sum index a992e70b..8640dd5a 100644 --- a/go.sum +++ b/go.sum @@ -110,6 +110,8 @@ github.com/onsi/gomega v1.4.3/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1Cpa github.com/openzipkin/zipkin-go v0.1.6/go.mod h1:QgAqvLzwWbR/WpD4A3cGpPtJrZXNIiJc5AZX7/PBEpw= github.com/pierrec/lz4 v2.0.5+incompatible/go.mod h1:pdkljMzZIN41W+lC3N2tnIh5sFi+IEE17M5jbnwPHcY= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pkg/profile v1.4.0 h1:uCmaf4vVbWAOZz36k1hrQD7ijGRzLwaME8Am/7a4jZI= +github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs= diff --git a/main.go b/main.go index f25051d8..25b7dd9d 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "time" "github.com/gorilla/handlers" + "github.com/pkg/profile" "git.andreafazzi.eu/andrea/oef/config" oef_handlers "git.andreafazzi.eu/andrea/oef/handlers" @@ -65,6 +66,11 @@ func main() { log.Println("Eventually write regions on DB...") orm.CreateRegions(db) + if conf.Profiling { + log.Println("Start profiling...") + defer profile.Start().Stop() + } + log.Println("OEF is listening to port 3000...") htmlRenderer, err := renderer.NewHTMLRenderer("templates")