From 6bae52d652832aed386cf7a7586f6400f62a4b90 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Mon, 29 Oct 2018 17:44:48 +0100 Subject: [PATCH] models: Added benchmarks to changekeys, added shortcuts for common types there could be many of. --- models/changekeys/many_test.go | 27 +++++++++++++++++++++++++++ models/changekeys/one.go | 9 +++++++++ models/changekeys/one_test.go | 16 ++++++++++++++++ 3 files changed, 52 insertions(+) diff --git a/models/changekeys/many_test.go b/models/changekeys/many_test.go index 2833b70..4986a21 100644 --- a/models/changekeys/many_test.go +++ b/models/changekeys/many_test.go @@ -50,3 +50,30 @@ func TestMany(t *testing.T) { } } } + +func BenchmarkMany(b *testing.B) { + data := []interface{}{ + models.Log{ID: "Stuff"}, + []interface{}{ + models.Post{ID: "P1"}, + models.Post{ID: "P2"}, + models.Post{ID: "P3"}, + models.Post{ID: "P4"}, + models.Post{ID: "P5"}, + models.Post{ID: "P6"}, + models.Post{ID: "P7"}, + models.Post{ID: "P8"}, + models.Post{ID: "P9"}, + models.Post{ID: "P10"}, + models.Post{ID: "P11"}, + }, + models.Character{ID: "C17"}, + models.Channel{Name: "#Stuff"}, + } + + b.ResetTimer() + + for n := 0; n < b.N; n++ { + changekeys.Many(data...) + } +} diff --git a/models/changekeys/one.go b/models/changekeys/one.go index 9316d2f..462530f 100644 --- a/models/changekeys/one.go +++ b/models/changekeys/one.go @@ -8,6 +8,15 @@ import ( // One makes a ChangeKey for a model, or panics if it's not supported. func One(object interface{}) models.ChangeKey { + switch v := object.(type) { + case models.Post: + return models.ChangeKey{Model: "Post", ID: v.ID} + case models.Character: + return models.ChangeKey{Model: "Character", ID: v.ID} + case models.Channel: + return models.ChangeKey{Model: "Channel", ID: v.Name} + } + model := "" if t := reflect.TypeOf(object); t.Kind() == reflect.Ptr { model = t.Elem().Name() diff --git a/models/changekeys/one_test.go b/models/changekeys/one_test.go index d4d6d80..e8a2859 100644 --- a/models/changekeys/one_test.go +++ b/models/changekeys/one_test.go @@ -40,3 +40,19 @@ func TestOne(t *testing.T) { }) } } + +func BenchmarkOneHit(b *testing.B) { + var post = models.Post{ID: "P123412341234123"} + + for n := 0; n < b.N; n++ { + changekeys.One(post) + } +} + +func BenchmarkOneMiss(b *testing.B) { + var story = models.Story{ID: "P123412341234123"} + + for n := 0; n < b.N; n++ { + changekeys.One(story) + } +}