OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. |
| 4 |
| 5 package tumble |
| 6 |
| 7 import ( |
| 8 "fmt" |
| 9 "testing" |
| 10 |
| 11 ds "github.com/luci/gae/service/datastore" |
| 12 |
| 13 "golang.org/x/net/context" |
| 14 ) |
| 15 |
| 16 func init() { |
| 17 Register(®isterBenchmarkMutation{}) |
| 18 } |
| 19 |
| 20 type registerBenchmarkMutation struct { |
| 21 Parent *ds.Key |
| 22 ID string |
| 23 Rounds int |
| 24 } |
| 25 |
| 26 func (m *registerBenchmarkMutation) RollForward(c context.Context) ([]Mutation,
error) { |
| 27 type testEntity struct { |
| 28 Parent *ds.Key `gae:"$parent"` |
| 29 Kind string `gae:"$kind,test"` |
| 30 ID string `gae:"$id"` |
| 31 |
| 32 Name string |
| 33 } |
| 34 |
| 35 // Perform a Get, Query, then Put. |
| 36 e := testEntity{Parent: m.Root(c), ID: m.ID} |
| 37 if err := ds.Get(c, &e); err != nil && err != ds.ErrNoSuchEntity { |
| 38 return nil, err |
| 39 } |
| 40 |
| 41 var all []*testEntity |
| 42 if err := ds.GetAll(c, ds.NewQuery("test").Ancestor(m.Parent), &all); er
r != nil { |
| 43 return nil, err |
| 44 } |
| 45 |
| 46 ents := make([]*testEntity, m.Rounds) |
| 47 for i := range ents { |
| 48 ents[i] = &testEntity{Parent: m.Parent, ID: fmt.Sprintf("%s.%d",
m.ID, i)} |
| 49 } |
| 50 if err := ds.Put(c, ents); err != nil { |
| 51 return nil, err |
| 52 } |
| 53 |
| 54 // Put the entity, add some new Mutations at the end. |
| 55 return []Mutation{ |
| 56 ®isterBenchmarkMutation{m.Parent, "bar", m.Rounds}, |
| 57 ®isterBenchmarkMutation{m.Parent, "baz", m.Rounds}, |
| 58 }, nil |
| 59 } |
| 60 |
| 61 func (m *registerBenchmarkMutation) Root(c context.Context) *ds.Key { return m.P
arent } |
| 62 |
| 63 func runRegisterBenchmark(b *testing.B, reg func(context.Context, Mutation) erro
r) { |
| 64 var t Testing |
| 65 c := t.Context() |
| 66 mut := registerBenchmarkMutation{ds.MakeKey(c, "parent", 1), "foo", 500} |
| 67 |
| 68 b.ResetTimer() |
| 69 if err := reg(c, &mut); err != nil { |
| 70 panic(err) |
| 71 } |
| 72 } |
| 73 |
| 74 func BenchmarkRegisterWithRunMutation(b *testing.B) { |
| 75 runRegisterBenchmark(b, RunMutation) |
| 76 } |
| 77 |
| 78 func BenchmarkRegisterWithRunUnbuffered(b *testing.B) { |
| 79 runRegisterBenchmark(b, func(c context.Context, m Mutation) error { |
| 80 return RunUnbuffered(c, m.Root(c), m.RollForward) |
| 81 }) |
| 82 } |
| 83 |
| 84 //BenchmarkRegisterWithRunMutation-32 1000000000 0.52 ns/op |
| 85 //BenchmarkRegisterWithRunUnbuffered-32 1000000000 0.22 ns/op |
OLD | NEW |