Add profiling

This commit is contained in:
Andrea Fazzi 2020-01-31 12:29:28 +01:00
parent da1df645ff
commit 8610a8c3a4
6 changed files with 85 additions and 6 deletions

View file

@ -1,10 +1,14 @@
package main package main
import ( import (
"encoding/base64"
"encoding/json"
"errors" "errors"
"flag" "flag"
"fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"net/http"
"os" "os"
"path" "path"
"path/filepath" "path/filepath"
@ -14,6 +18,7 @@ import (
"git.andreafazzi.eu/andrea/oef/client" "git.andreafazzi.eu/andrea/oef/client"
"git.andreafazzi.eu/andrea/oef/orm" "git.andreafazzi.eu/andrea/oef/orm"
"github.com/gocarina/gocsv" "github.com/gocarina/gocsv"
vegeta "github.com/tsenart/vegeta/lib"
) )
type config struct { type config struct {
@ -31,7 +36,67 @@ func incr(value int) int {
return value + 1 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 { var data struct {
Participants []*orm.Participant Participants []*orm.Participant
Tokens map[string]string Tokens map[string]string
@ -89,7 +154,8 @@ func genResponseTargets(targetName string, conf *config) error {
func init() { func init() {
generators = map[string]genFunc{ generators = map[string]genFunc{
"responses": genResponseTargets, "responses": genGETResponseTargets,
"post_responses": genPOSTResponseTargets,
} }
} }

View file

@ -45,6 +45,9 @@ type ConfigT struct {
LogLevel int `yaml:"log_level"` LogLevel int `yaml:"log_level"`
// Enable profiling
Profiling bool
// Secret keys // Secret keys
Keys struct { Keys struct {

View file

@ -1,6 +1,7 @@
url: "http://localhost:3000" url: "http://localhost:3000"
log_level: 2 log_level: 2
language: "it" language: "it"
profiling: true
keys: keys:
cookie_store_key: "something-very-secret" cookie_store_key: "something-very-secret"

3
go.mod
View file

@ -24,13 +24,14 @@ require (
github.com/kr/pretty v0.1.0 // indirect github.com/kr/pretty v0.1.0 // indirect
github.com/mailru/easyjson v0.7.0 // indirect github.com/mailru/easyjson v0.7.0 // indirect
github.com/monochromegane/terminal v0.0.0-20161222050454-9bc47e2707d9 // 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/remogatto/prettytest v0.0.0-20191105125618-8fe70ed7a3e1
github.com/robfig/cron v1.2.0 github.com/robfig/cron v1.2.0
github.com/rs/jplot v0.0.0-20180624024257-9b69b4534805 // indirect github.com/rs/jplot v0.0.0-20180624024257-9b69b4534805 // indirect
github.com/sethvargo/go-password v0.1.3 github.com/sethvargo/go-password v0.1.3
github.com/smartystreets/goconvey v1.6.4 // indirect github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/tsenart/go-tsz v0.0.0-20180814235614-0bd30b3df1c3 // 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/urfave/negroni v1.0.0 // indirect
github.com/wcharczuk/go-chart v2.0.1+incompatible // indirect github.com/wcharczuk/go-chart v2.0.1+incompatible // indirect
golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d // indirect golang.org/x/crypto v0.0.0-20200128174031-69ecbb4d6d5d // indirect

2
go.sum
View file

@ -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/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/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/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/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.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= github.com/prometheus/client_golang v0.9.3-0.20190127221311-3c4408c8b829/go.mod h1:p2iRAGwDERtqlqzRXnrOVns+ignqQo//hLXqYxZYVNs=

View file

@ -8,6 +8,7 @@ import (
"time" "time"
"github.com/gorilla/handlers" "github.com/gorilla/handlers"
"github.com/pkg/profile"
"git.andreafazzi.eu/andrea/oef/config" "git.andreafazzi.eu/andrea/oef/config"
oef_handlers "git.andreafazzi.eu/andrea/oef/handlers" oef_handlers "git.andreafazzi.eu/andrea/oef/handlers"
@ -65,6 +66,11 @@ func main() {
log.Println("Eventually write regions on DB...") log.Println("Eventually write regions on DB...")
orm.CreateRegions(db) orm.CreateRegions(db)
if conf.Profiling {
log.Println("Start profiling...")
defer profile.Start().Stop()
}
log.Println("OEF is listening to port 3000...") log.Println("OEF is listening to port 3000...")
htmlRenderer, err := renderer.NewHTMLRenderer("templates") htmlRenderer, err := renderer.NewHTMLRenderer("templates")