| 1 |
// Copyright 2019 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package drivertest |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"fmt" |
| 20 |
"strconv" |
| 21 |
"sync/atomic" |
| 22 |
"testing" |
| 23 |
|
| 24 |
"gocloud.dev/docstore" |
| 25 |
) |
| 26 |
|
| 27 |
// RunBenchmarks runs benchmarks for docstore drivers. |
| 28 |
func RunBenchmarks(b *testing.B, coll *docstore.Collection) { |
| 29 |
b.Helper() |
| 30 |
|
| 31 |
defer coll.Close() |
| 32 |
ClearCollection(b, coll) |
| 33 |
b.Run("BenchmarkSingleActionPut", func(b *testing.B) { |
| 34 |
benchmarkSingleActionPut(b, 10, coll) |
| 35 |
}) |
| 36 |
b.Run("BenchmarkSingleActionGet", func(b *testing.B) { |
| 37 |
benchmarkSingleActionGet(b, 10, coll) |
| 38 |
}) |
| 39 |
b.Run("BenchmarkActionListPut", func(b *testing.B) { |
| 40 |
benchmarkActionListPut(b, 50, coll) |
| 41 |
}) |
| 42 |
b.Run("BenchmarkActionListGet", func(b *testing.B) { |
| 43 |
benchmarkActionListGet(b, 100, coll) |
| 44 |
}) |
| 45 |
ClearCollection(b, coll) |
| 46 |
} |
| 47 |
|
| 48 |
func benchmarkSingleActionPut(b *testing.B, n int, coll *docstore.Collection) { |
| 49 |
b.Helper() |
| 50 |
|
| 51 |
ctx := context.Background() |
| 52 |
const baseKey = "benchmarksingleaction-put-" |
| 53 |
var nextID uint32 |
| 54 |
|
| 55 |
b.ResetTimer() |
| 56 |
b.RunParallel(func(pb *testing.PB) { |
| 57 |
for pb.Next() { |
| 58 |
for range n { |
| 59 |
key := fmt.Sprintf("%s%d", baseKey, atomic.AddUint32(&nextID, 1)) |
| 60 |
doc := docmap{KeyField: key, "S": key} |
| 61 |
if err := coll.Put(ctx, doc); err != nil { |
| 62 |
b.Error(err) |
| 63 |
} |
| 64 |
} |
| 65 |
} |
| 66 |
}) |
| 67 |
} |
| 68 |
|
| 69 |
func benchmarkSingleActionGet(b *testing.B, n int, coll *docstore.Collection) { |
| 70 |
b.Helper() |
| 71 |
|
| 72 |
ctx := context.Background() |
| 73 |
const baseKey = "benchmarksingleaction-get-" |
| 74 |
docs := make([]docmap, n) |
| 75 |
puts := coll.Actions() |
| 76 |
for i := range n { |
| 77 |
docs[i] = docmap{KeyField: baseKey + strconv.Itoa(i), "n": i} |
| 78 |
puts.Put(docs[i]) |
| 79 |
} |
| 80 |
if err := puts.Do(ctx); err != nil { |
| 81 |
b.Fatal(err) |
| 82 |
} |
| 83 |
|
| 84 |
b.ResetTimer() |
| 85 |
b.RunParallel(func(pb *testing.PB) { |
| 86 |
for pb.Next() { |
| 87 |
for _, doc := range docs { |
| 88 |
got := docmap{KeyField: doc[KeyField]} |
| 89 |
if err := coll.Get(ctx, got); err != nil { |
| 90 |
b.Fatal(err) |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
}) |
| 95 |
} |
| 96 |
|
| 97 |
func benchmarkActionListPut(b *testing.B, n int, coll *docstore.Collection) { |
| 98 |
b.Helper() |
| 99 |
|
| 100 |
ctx := context.Background() |
| 101 |
const baseKey = "benchmarkactionlist-put-" |
| 102 |
var nextID uint32 |
| 103 |
|
| 104 |
b.ResetTimer() |
| 105 |
b.RunParallel(func(pb *testing.PB) { |
| 106 |
for pb.Next() { |
| 107 |
actions := coll.Actions() |
| 108 |
for range n { |
| 109 |
key := fmt.Sprintf("%s%d", baseKey, atomic.AddUint32(&nextID, 1)) |
| 110 |
doc := docmap{KeyField: key, "S": key} |
| 111 |
actions.Put(doc) |
| 112 |
} |
| 113 |
if err := actions.Do(ctx); err != nil { |
| 114 |
b.Error(err) |
| 115 |
} |
| 116 |
} |
| 117 |
}) |
| 118 |
} |
| 119 |
|
| 120 |
func benchmarkActionListGet(b *testing.B, n int, coll *docstore.Collection) { |
| 121 |
b.Helper() |
| 122 |
|
| 123 |
ctx := context.Background() |
| 124 |
const baseKey = "benchmarkactionlist-get-" |
| 125 |
docs := make([]docmap, n) |
| 126 |
puts := coll.Actions() |
| 127 |
for i := range n { |
| 128 |
docs[i] = docmap{KeyField: baseKey + strconv.Itoa(i), "n": i} |
| 129 |
puts.Put(docs[i]) |
| 130 |
} |
| 131 |
if err := puts.Do(ctx); err != nil { |
| 132 |
b.Fatal(err) |
| 133 |
} |
| 134 |
|
| 135 |
b.ResetTimer() |
| 136 |
b.RunParallel(func(pb *testing.PB) { |
| 137 |
for pb.Next() { |
| 138 |
gets := coll.Actions() |
| 139 |
for _, doc := range docs { |
| 140 |
got := docmap{KeyField: doc[KeyField]} |
| 141 |
gets.Get(got) |
| 142 |
} |
| 143 |
if err := gets.Do(ctx); err != nil { |
| 144 |
b.Fatal(err) |
| 145 |
} |
| 146 |
} |
| 147 |
}) |
| 148 |
} |