|
4 | 4 | "context"
|
5 | 5 | "fmt"
|
6 | 6 | "testing"
|
| 7 | + "time" |
7 | 8 |
|
8 | 9 | . "github.com/onsi/gomega"
|
9 | 10 | clientv3 "go.etcd.io/etcd/client/v3"
|
@@ -77,6 +78,24 @@ func TestUpdate(t *testing.T) {
|
77 | 78 | g.Expect(resp.Kvs[0].ModRevision).To(BeNumerically(">", resp.Kvs[0].CreateRevision))
|
78 | 79 | }
|
79 | 80 | })
|
| 81 | + t.Run("UpdateSameKeyLinearity", func(t *testing.T) { |
| 82 | + g := NewWithT(t) |
| 83 | + |
| 84 | + // Add a large number of entries, two times and |
| 85 | + // it should take approximately same amout of time |
| 86 | + numAddEntries := 1000 |
| 87 | + |
| 88 | + startFirst := time.Now() |
| 89 | + addSameEntries(ctx, g, client, numAddEntries, true) |
| 90 | + durationFirstBatch := time.Since(startFirst) |
| 91 | + |
| 92 | + startSecond := time.Now() |
| 93 | + addSameEntries(ctx, g, client, numAddEntries, false) |
| 94 | + durationSecondBatch := time.Since(startSecond) |
| 95 | + |
| 96 | + g.Expect(durationSecondBatch <= durationFirstBatch*2).To(BeTrue()) |
| 97 | + |
| 98 | + }) |
80 | 99 |
|
81 | 100 | // Trying to update an old revision(in compare) should fail
|
82 | 101 | t.Run("UpdateOldRevisionFails", func(t *testing.T) {
|
@@ -125,6 +144,37 @@ func TestUpdate(t *testing.T) {
|
125 | 144 |
|
126 | 145 | }
|
127 | 146 |
|
| 147 | +func addSameEntries(ctx context.Context, g Gomega, client *clientv3.Client, numEntries int, create_first bool) { |
| 148 | + for i := 0; i < numEntries; i++ { |
| 149 | + key := "testkey-same" |
| 150 | + value := fmt.Sprintf("value-%d", i) |
| 151 | + |
| 152 | + if i != 0 || !create_first { |
| 153 | + updateEntry(ctx, g, client, key, value) |
| 154 | + } else { |
| 155 | + addEntry(ctx, g, client, key, value) |
| 156 | + } |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func updateEntry(ctx context.Context, g Gomega, client *clientv3.Client, key string, value string) { |
| 161 | + |
| 162 | + resp, err := client.Get(ctx, key, clientv3.WithRange("")) |
| 163 | + |
| 164 | + g.Expect(err).To(BeNil()) |
| 165 | + g.Expect(resp.Kvs).To(HaveLen(1)) |
| 166 | + |
| 167 | + resp2, err2 := client.Txn(ctx). |
| 168 | + If(clientv3.Compare(clientv3.ModRevision(key), "=", resp.Kvs[0].ModRevision)). |
| 169 | + Then(clientv3.OpPut(key, value)). |
| 170 | + Else(clientv3.OpGet(key, clientv3.WithRange(""))). |
| 171 | + Commit() |
| 172 | + |
| 173 | + g.Expect(err2).To(BeNil()) |
| 174 | + g.Expect(resp2.Succeeded).To(BeTrue()) |
| 175 | + |
| 176 | +} |
| 177 | + |
128 | 178 | // BenchmarkUpdate is a benchmark for the Update operation.
|
129 | 179 | func BenchmarkUpdate(b *testing.B) {
|
130 | 180 | ctx := context.Background()
|
|
0 commit comments