From d54fe42ec75cd6ea23d11de6916358016ac0ce30 Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Fri, 23 Apr 2021 09:39:49 +0200 Subject: [PATCH] profiling tests to fiddle with pprof just for fun --- .gitignore | 3 ++ app/webmention/recv/receive_test.go | 38 ++++++++++++++++++++++++-- app/webmention/send/discoverer_test.go | 2 +- app/webmention/send/send_test.go | 2 +- mocks/restclient.go | 6 ++-- 5 files changed, 43 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 32cf5c6..76e392c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,9 @@ .DS_Store +*.prof +*.test + config.json data/* diff --git a/app/webmention/recv/receive_test.go b/app/webmention/recv/receive_test.go index 0f4cbc5..e4aa070 100644 --- a/app/webmention/recv/receive_test.go +++ b/app/webmention/recv/receive_test.go @@ -5,7 +5,9 @@ import ( "brainbaking.com/go-jamming/db" "encoding/json" "errors" + "github.com/rs/zerolog" "github.com/stretchr/testify/assert" + "io/ioutil" "net/http" "testing" "time" @@ -56,7 +58,7 @@ func TestSaveAuthorPictureLocally(t *testing.T) { Conf: conf, Repo: repo, RestClient: &mocks.RestClientMock{ - GetBodyFunc: mocks.RelPathGetBodyFunc(t, "../../../mocks/"), + GetBodyFunc: mocks.RelPathGetBodyFunc("../../../mocks/"), }, } @@ -74,6 +76,36 @@ func TestSaveAuthorPictureLocally(t *testing.T) { } } +func BenchmarkReceive(b *testing.B) { + origLog := zerolog.GlobalLevel() + zerolog.SetGlobalLevel(zerolog.Disabled) + defer zerolog.SetGlobalLevel(origLog) + + wm := mf.Mention{ + Source: "https://brainbaking.com/valid-indieweb-source.html", + Target: "https://jefklakscodex.com/articles", + } + data, err := ioutil.ReadFile("../../../mocks/valid-indieweb-source.html") + assert.NoError(b, err) + html := string(data) + + repo := db.NewMentionRepo(conf) + recv := &Receiver{ + Conf: conf, + Repo: repo, + RestClient: &mocks.RestClientMock{ + GetBodyFunc: func(s string) (http.Header, string, error) { + return http.Header{}, html, nil + }, + }, + } + + b.ReportAllocs() + for i := 0; i < b.N; i++ { + recv.Receive(wm) + } +} + func TestReceive(t *testing.T) { cases := []struct { label string @@ -143,7 +175,7 @@ func TestReceive(t *testing.T) { Conf: conf, Repo: repo, RestClient: &mocks.RestClientMock{ - GetBodyFunc: mocks.RelPathGetBodyFunc(t, "../../../mocks/"), + GetBodyFunc: mocks.RelPathGetBodyFunc("../../../mocks/"), }, } @@ -194,7 +226,7 @@ func TestReceiveTargetThatDoesNotPointToTheSourceDoesNothing(t *testing.T) { Conf: conf, Repo: repo, RestClient: &mocks.RestClientMock{ - GetBodyFunc: mocks.RelPathGetBodyFunc(t, "../../../mocks/"), + GetBodyFunc: mocks.RelPathGetBodyFunc("../../../mocks/"), }, } diff --git a/app/webmention/send/discoverer_test.go b/app/webmention/send/discoverer_test.go index abfe7e4..babb891 100644 --- a/app/webmention/send/discoverer_test.go +++ b/app/webmention/send/discoverer_test.go @@ -9,7 +9,7 @@ import ( func TestDiscover(t *testing.T) { var sender = &Sender{ RestClient: &mocks.RestClientMock{ - GetBodyFunc: mocks.RelPathGetBodyFunc(t, "../../../mocks/"), + GetBodyFunc: mocks.RelPathGetBodyFunc("../../../mocks/"), }, } diff --git a/app/webmention/send/send_test.go b/app/webmention/send/send_test.go index a2a4594..f9984fd 100644 --- a/app/webmention/send/send_test.go +++ b/app/webmention/send/send_test.go @@ -151,7 +151,7 @@ func TestSendIntegrationTestCanSendBothWebmentionsAndPingbacks(t *testing.T) { Conf: conf, Repo: db.NewMentionRepo(conf), RestClient: &mocks.RestClientMock{ - GetBodyFunc: mocks.RelPathGetBodyFunc(t, "./../../../mocks/"), + GetBodyFunc: mocks.RelPathGetBodyFunc("./../../../mocks/"), PostFunc: func(url string, contentType string, body string) error { lock.Lock() defer lock.Unlock() diff --git a/mocks/restclient.go b/mocks/restclient.go index 3bc2432..27c872b 100644 --- a/mocks/restclient.go +++ b/mocks/restclient.go @@ -2,7 +2,7 @@ package mocks import ( "encoding/json" - "fmt" + "github.com/rs/zerolog/log" "io/ioutil" "net/http" "net/url" @@ -42,9 +42,9 @@ func toHttpHeader(header map[string]interface{}) http.Header { return httpHeader } -func RelPathGetBodyFunc(t *testing.T, relPath string) func(string) (http.Header, string, error) { +func RelPathGetBodyFunc(relPath string) func(string) (http.Header, string, error) { return func(url string) (http.Header, string, error) { - fmt.Println(" - GET call at " + url) + log.Debug().Str("url", url).Msg(" - GET call") // url: https://brainbaking.com/something-something.html // want: ../../mocks/something-something.html mockfile := relPath + strings.ReplaceAll(url, "https://brainbaking.com/", "")