diff --git a/main.go b/main.go index 14ccb1b..81519c7 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "io/ioutil" "net/http" "os" + "time" jsonhandler "github.com/apex/log/handlers/json" "github.com/aws/aws-sdk-go-v2/aws/endpoints" @@ -109,8 +110,6 @@ func New() (h handler, err error) { log.WithError(err).Fatal("error opening database") return } - h.db.SetMaxOpenConns(2) - h.db.SetMaxIdleConns(1) return } @@ -143,7 +142,6 @@ func (h handler) BasicEngine() http.Handler { } func (h handler) runsql(sqlfile string, unitID string) (res sql.Result, err error) { - if unitID == "" { return res, fmt.Errorf("id is unset") } @@ -255,13 +253,14 @@ func (h handler) createUnit(w http.ResponseWriter, r *http.Request) { ctx.Info("inserted") + start := time.Now() _, err = h.runsql("unit_create_new.sql", unit.MefeUnitID) if err != nil { ctx.WithError(err).Errorf("unit_create_new.sql failed") response.BadRequest(w, err.Error()) return } - ctx.Infof("ran unit_create_new.sql") + ctx.WithField("duration", time.Since(start)).Infof("ran unit_create_new.sql") ProductID, err := h.getProductID(unit.MefeUnitID) if err != nil { ctx.WithError(err).Errorf("unit_create_new.sql failed") diff --git a/main_test.go b/main_test.go index c16ebf7..d217316 100644 --- a/main_test.go +++ b/main_test.go @@ -1,10 +1,12 @@ package main import ( + "encoding/json" "net/http" "os" "testing" + "github.com/Pallinder/go-randomdata" "github.com/appleboy/gofight" _ "github.com/go-sql-driver/mysql" "github.com/stretchr/testify/assert" @@ -43,3 +45,26 @@ func TestRoutes(t *testing.T) { assert.Equal(t, http.StatusOK, r.Code) }) } + +func Benchmark_RealCreate(b *testing.B) { + b.ResetTimer() + for n := 0; n < b.N; n++ { + name := randomdata.SillyName() + r := gofight.New() + u := []unit{unit{ + MefeUnitID: name, + MefeCreatorUserID: "user", + BzfeCreatorUserID: 55, + ClassificationID: 2, + UnitName: name, + UnitDescriptionDetails: "Up on the hills and testing", + }} + uJSON, _ := json.Marshal(u) + r.POST("/create"). + SetBody(string(uJSON)). + Run(h.BasicEngine(), func(r gofight.HTTPResponse, rq gofight.HTTPRequest) { + assert.Contains(b, r.Body.String(), name) + assert.Equal(b, http.StatusOK, r.Code) + }) + } +}