oef/benchmarks/gen_targets.go

229 lines
5.1 KiB
Go
Raw Normal View History

package main
import (
2020-01-31 15:03:26 +01:00
"bytes"
2020-01-31 12:29:28 +01:00
"encoding/json"
"errors"
"flag"
2020-01-31 12:29:28 +01:00
"fmt"
"io/ioutil"
"log"
2020-01-31 12:29:28 +01:00
"net/http"
2020-01-31 15:03:26 +01:00
"net/url"
"os"
"path"
2020-01-31 08:18:38 +01:00
"path/filepath"
"strings"
"text/template"
2020-01-31 08:18:38 +01:00
"git.andreafazzi.eu/andrea/oef/client"
"git.andreafazzi.eu/andrea/oef/orm"
"github.com/gocarina/gocsv"
2020-01-31 12:29:28 +01:00
vegeta "github.com/tsenart/vegeta/lib"
)
2020-01-31 10:50:28 +01:00
type config struct {
2020-02-14 10:06:07 +01:00
Url string
SubscribePassword string
2020-01-31 10:50:28 +01:00
}
type genFunc func(string, *config) error
var (
generators map[string]genFunc
templateDir string = "./templates"
)
func incr(value int) int {
return value + 1
}
2020-02-14 10:06:07 +01:00
func genGETschoolSubscription(targetName string, conf *config) error {
token, err := client.GetToken(conf.Url, "", conf.SubscribePassword)
if err != nil {
return err
}
err = os.Mkdir("targets", 0777)
if errors.Is(err, &os.PathError{}) {
return err
}
tmplPath := path.Join(templateDir, targetName+".tpl")
log.Printf("Parse %s template", tmplPath)
tmpl, err := template.New(filepath.Base(tmplPath)).Funcs(template.FuncMap{"incr": incr}).ParseFiles(tmplPath)
if err != nil {
return err
}
output, err := os.Create("./targets/school_subscription.txt")
if err != nil {
return err
}
defer output.Close()
log.Printf("Execute %s template and generate targets...", targetName)
data := struct {
Token string
Config *config
}{token, conf}
err = tmpl.Execute(output, data)
if err != nil {
return err
}
return nil
}
2020-01-31 12:29:28 +01:00
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)
2020-01-31 15:03:26 +01:00
form := url.Values{}
form.Set("Singleresponses.0", "32")
2020-01-31 12:49:33 +01:00
for i, participant := range participants {
2020-01-31 12:29:28 +01:00
targets = append(targets, &vegeta.Target{
Method: "POST",
2020-01-31 12:49:33 +01:00
URL: fmt.Sprintf("%s/responses/%d/update?format=html&tpl_content=responses_add_update&tpl_layout=base&update=true&login_session=%s", conf.Url, i+1, tokens[participant.FiscalCode]),
2020-01-31 15:03:26 +01:00
Body: []byte(form.Encode()),
2020-01-31 12:29:28 +01:00
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 {
2020-01-31 15:03:26 +01:00
var line string
buf := bytes.NewBufferString(line)
encoder := json.NewEncoder(buf)
encoder.SetEscapeHTML(false)
err := encoder.Encode(t)
// jsonData, err := json.Marshal(t)
2020-01-31 12:29:28 +01:00
if err != nil {
return err
}
2020-01-31 15:03:26 +01:00
_, err = output.WriteString(buf.String() + "\n\n")
2020-01-31 12:29:28 +01:00
if err != nil {
return err
}
}
return nil
}
func genGETResponseTargets(targetName string, conf *config) error {
var data struct {
Participants []*orm.Participant
Tokens map[string]string
2020-01-31 10:50:28 +01:00
Config *config
}
2020-01-31 10:50:28 +01:00
data.Config = conf
log.Println("Read participants.csv...")
2020-01-30 14:51:35 +01:00
input, err := ioutil.ReadFile("./testdata/participants.csv")
if err != nil {
return err
}
if err := gocsv.Unmarshal(strings.NewReader(string(input)), &data.Participants); err != nil {
panic(err)
}
log.Println("Obtaining tokens for each participants...")
data.Tokens = make(map[string]string, 0)
2020-01-31 08:18:38 +01:00
for _, participant := range data.Participants {
2020-01-31 10:50:28 +01:00
token, err := client.GetToken(data.Config.Url, participant.FiscalCode, participant.Password)
2020-01-31 08:18:38 +01:00
if err != nil {
return err
}
data.Tokens[participant.FiscalCode] = token
}
tmplPath := path.Join(templateDir, targetName+".tpl")
2020-01-30 14:51:35 +01:00
log.Printf("Parse %s template", tmplPath)
2020-01-31 08:18:38 +01:00
tmpl, err := template.New(filepath.Base(tmplPath)).Funcs(template.FuncMap{"incr": incr}).ParseFiles(tmplPath)
if err != nil {
return err
}
2020-01-31 08:18:38 +01:00
err = os.Mkdir("targets", 0777)
if errors.Is(err, &os.PathError{}) {
return err
}
2020-01-30 14:51:35 +01:00
output, err := os.Create("./targets/response_targets.txt")
if err != nil {
return err
}
defer output.Close()
log.Printf("Execute %s template and generate targets...", targetName)
2020-01-30 14:51:35 +01:00
err = tmpl.Execute(output, data)
if err != nil {
return err
}
return nil
}
func init() {
generators = map[string]genFunc{
2020-02-14 10:06:07 +01:00
"responses": genGETResponseTargets,
"post_responses": genPOSTResponseTargets,
"school_subscription": genGETschoolSubscription,
}
}
func main() {
2020-01-31 12:49:33 +01:00
target := flag.String("targets", "responses", "Generate targets for participants responses.")
2020-01-31 10:50:28 +01:00
url := flag.String("url", "http://localhost:3000", "The URL of the host.")
flag.Parse()
fn, ok := generators[*target]
if !ok {
log.Fatal(errors.New("Unknown target"))
}
2020-02-14 10:06:07 +01:00
err := fn(*target, &config{Url: *url, SubscribePassword: "subscribe"})
if err != nil {
panic(err)
}
}