Add end-to-end benchmaking

This commit is contained in:
Vikram Rangnekar 2019-06-17 01:58:00 -04:00
parent cd6d6f97a8
commit 4c07ad1102
9 changed files with 43 additions and 15 deletions

8
bench/bench.sh Executable file
View File

@ -0,0 +1,8 @@
#!/bin/sh
if brew ls --versions wrk > /dev/null; then
wrk -t12 -c400 -d30s --timeout 10s --script=query.lua --latency http://localhost:8080/api/v1/graphql
else
brew install wek
wrk -t12 -c400 -d30s --timeout 10s --script=query.lua --latency http://localhost:8080/api/v1/graphql
fi

3
bench/query.lua Normal file
View File

@ -0,0 +1,3 @@
wrk.method = "POST"
wrk.headers["Content-Type"] = "application/json"
wrk.body = [[{"query":" { products( limit: 30, order_by: { price: desc }, distinct: [ price ] where: { id: { and: { greater_or_equals: 20, lt: 28 } } }) { id name price user { id email } } } "}]]

View File

@ -1,7 +1,9 @@
app_name: "Super Graph Development"
host_port: 0.0.0.0:8080
web_ui: true
debug_level: 1
# debug, info, warn, error, fatal, panic
log_level: "debug"
# enabling tracing also disables query caching
enable_tracing: true

View File

@ -1,7 +1,9 @@
app_name: "Super Graph Production"
host_port: 0.0.0.0:8080
web_ui: false
debug_level: 0
# debug, info, warn, error, fatal, panic, disable
log_level: "info"
# disabled tracing enables query caching
enable_tracing: false

2
demo
View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/sh
if [ "$1" == "setup" ]; then
echo "Downloading pre-built docker images"

View File

@ -11,7 +11,7 @@ type config struct {
Env string
HostPort string `mapstructure:"host_port"`
WebUI bool `mapstructure:"web_ui"`
DebugLevel int `mapstructure:"debug_level"`
LogLevel string `mapstructure:"log_level"`
EnableTracing bool `mapstructure:"enable_tracing"`
AuthFailBlock string `mapstructure:"auth_fail_block"`
Inflections map[string]string

View File

@ -283,7 +283,7 @@ func (c *coreContext) resolveSQL(qc *qcode.QCode, vars variables) (
finalSQL := stmt.String()
if conf.DebugLevel > 0 {
if conf.LogLevel == "debug" {
os.Stdout.WriteString(finalSQL)
}

View File

@ -10,7 +10,6 @@ import (
"time"
"github.com/gorilla/websocket"
"github.com/rs/zerolog/log"
)
const (
@ -69,18 +68,25 @@ func apiv1Http(w http.ResponseWriter, r *http.Request) {
ctx := &coreContext{Context: r.Context()}
if authFailBlock == authFailBlockAlways && authCheck(ctx) == false {
http.Error(w, "Not authorized", 401)
err := "Not authorized"
logger.Debug().Msg(err)
http.Error(w, err, 401)
return
}
b, err := ioutil.ReadAll(io.LimitReader(r.Body, maxReadBytes))
defer r.Body.Close()
if err != nil {
logger.Err(err).Msg("failed to read request body")
errorResp(w, err)
return
}
if err := json.Unmarshal(b, &ctx.req); err != nil {
err = json.Unmarshal(b, &ctx.req)
if err != nil {
logger.Err(err).Msg("failed to decode json request body")
errorResp(w, err)
return
}
@ -128,19 +134,18 @@ func apiv1Http(w http.ResponseWriter, r *http.Request) {
err = ctx.handleReq(w, r)
if err == errUnauthorized {
http.Error(w, "Not authorized", 401)
err := "Not authorized"
logger.Debug().Msg(err)
http.Error(w, err, 401)
}
if err != nil {
logger.Err(err).Msg("Failed to handle request")
errorResp(w, err)
}
}
func errorResp(w http.ResponseWriter, err error) {
if conf.DebugLevel > 0 {
log.Error().Err(err)
}
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(gqlResp{Error: err.Error()})
}

View File

@ -40,7 +40,10 @@ var (
func initLog() *zerolog.Logger {
logger := zerolog.New(zerolog.ConsoleWriter{Out: os.Stderr}).
With().Caller().Logger()
With().
Timestamp().
Caller().
Logger()
return &logger
/*
@ -69,7 +72,6 @@ func initConf() (*config, error) {
vi.SetDefault("host_port", "0.0.0.0:8080")
vi.SetDefault("web_ui", false)
vi.SetDefault("debug_level", 0)
vi.SetDefault("enable_tracing", false)
vi.SetDefault("auth_fail_block", "always")
@ -184,6 +186,12 @@ func Init() {
logger.Fatal().Err(err).Msg("failed to read config")
}
logLevel, err := zerolog.ParseLevel(conf.LogLevel)
if err != nil {
logger.Error().Err(err).Msg("error setting log_level")
}
zerolog.SetGlobalLevel(logLevel)
db, err = initDB(conf)
if err != nil {
logger.Fatal().Err(err).Msg("failed to connect to database")