Browse Source

models: Added benchmarks to changekeys, added shortcuts for common types there could be many of.

1.1
Gisle Aune 6 years ago
parent
commit
6bae52d652
  1. 27
      models/changekeys/many_test.go
  2. 9
      models/changekeys/one.go
  3. 16
      models/changekeys/one_test.go

27
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...)
}
}

9
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()

16
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)
}
}
Loading…
Cancel
Save