profiling tests to fiddle with pprof just for fun

This commit is contained in:
Wouter Groeneveld 2021-04-23 09:39:49 +02:00
parent f964c7442f
commit d54fe42ec7
5 changed files with 43 additions and 8 deletions

3
.gitignore vendored
View File

@ -1,6 +1,9 @@
.DS_Store .DS_Store
*.prof
*.test
config.json config.json
data/* data/*

View File

@ -5,7 +5,9 @@ import (
"brainbaking.com/go-jamming/db" "brainbaking.com/go-jamming/db"
"encoding/json" "encoding/json"
"errors" "errors"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"io/ioutil"
"net/http" "net/http"
"testing" "testing"
"time" "time"
@ -56,7 +58,7 @@ func TestSaveAuthorPictureLocally(t *testing.T) {
Conf: conf, Conf: conf,
Repo: repo, Repo: repo,
RestClient: &mocks.RestClientMock{ 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) { func TestReceive(t *testing.T) {
cases := []struct { cases := []struct {
label string label string
@ -143,7 +175,7 @@ func TestReceive(t *testing.T) {
Conf: conf, Conf: conf,
Repo: repo, Repo: repo,
RestClient: &mocks.RestClientMock{ RestClient: &mocks.RestClientMock{
GetBodyFunc: mocks.RelPathGetBodyFunc(t, "../../../mocks/"), GetBodyFunc: mocks.RelPathGetBodyFunc("../../../mocks/"),
}, },
} }
@ -194,7 +226,7 @@ func TestReceiveTargetThatDoesNotPointToTheSourceDoesNothing(t *testing.T) {
Conf: conf, Conf: conf,
Repo: repo, Repo: repo,
RestClient: &mocks.RestClientMock{ RestClient: &mocks.RestClientMock{
GetBodyFunc: mocks.RelPathGetBodyFunc(t, "../../../mocks/"), GetBodyFunc: mocks.RelPathGetBodyFunc("../../../mocks/"),
}, },
} }

View File

@ -9,7 +9,7 @@ import (
func TestDiscover(t *testing.T) { func TestDiscover(t *testing.T) {
var sender = &Sender{ var sender = &Sender{
RestClient: &mocks.RestClientMock{ RestClient: &mocks.RestClientMock{
GetBodyFunc: mocks.RelPathGetBodyFunc(t, "../../../mocks/"), GetBodyFunc: mocks.RelPathGetBodyFunc("../../../mocks/"),
}, },
} }

View File

@ -151,7 +151,7 @@ func TestSendIntegrationTestCanSendBothWebmentionsAndPingbacks(t *testing.T) {
Conf: conf, Conf: conf,
Repo: db.NewMentionRepo(conf), Repo: db.NewMentionRepo(conf),
RestClient: &mocks.RestClientMock{ RestClient: &mocks.RestClientMock{
GetBodyFunc: mocks.RelPathGetBodyFunc(t, "./../../../mocks/"), GetBodyFunc: mocks.RelPathGetBodyFunc("./../../../mocks/"),
PostFunc: func(url string, contentType string, body string) error { PostFunc: func(url string, contentType string, body string) error {
lock.Lock() lock.Lock()
defer lock.Unlock() defer lock.Unlock()

View File

@ -2,7 +2,7 @@ package mocks
import ( import (
"encoding/json" "encoding/json"
"fmt" "github.com/rs/zerolog/log"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"net/url" "net/url"
@ -42,9 +42,9 @@ func toHttpHeader(header map[string]interface{}) http.Header {
return httpHeader 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) { 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 // url: https://brainbaking.com/something-something.html
// want: ../../mocks/something-something.html // want: ../../mocks/something-something.html
mockfile := relPath + strings.ReplaceAll(url, "https://brainbaking.com/", "") mockfile := relPath + strings.ReplaceAll(url, "https://brainbaking.com/", "")