Skip to content

Commit

Permalink
go test -v -run TestRealCreate -bench=. -benchtime=10s
Browse files Browse the repository at this point in the history
  • Loading branch information
Kai Hendry committed Apr 22, 2019
1 parent 0b4def0 commit 20e4f6a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
7 changes: 3 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -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")
Expand Down
25 changes: 25 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit 20e4f6a

Please sign in to comment.