| 1 |
package tokenstorage_test |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"strconv" |
| 6 |
"testing" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
) |
| 11 |
|
| 12 |
const ( |
| 13 |
benchmarkClientID = "bench-client" |
| 14 |
benchmarkToken = "refresh-token-with-some-size" |
| 15 |
) |
| 16 |
|
| 17 |
func BenchmarkStorageInMemory(b *testing.B) { |
| 18 |
ctx := context.Background() |
| 19 |
|
| 20 |
b.Run("set", func(b *testing.B) { |
| 21 |
storage := newInMemoryWithGC(b, time.Hour, 0) |
| 22 |
|
| 23 |
b.Cleanup(func() { |
| 24 |
require.NoError(b, storage.Close()) |
| 25 |
}) |
| 26 |
|
| 27 |
b.ReportAllocs() |
| 28 |
b.ResetTimer() |
| 29 |
|
| 30 |
for b.Loop() { |
| 31 |
if err := storage.Set(ctx, benchmarkClientID, benchmarkToken); err != nil { |
| 32 |
b.Fatal(err) |
| 33 |
} |
| 34 |
} |
| 35 |
}) |
| 36 |
|
| 37 |
b.Run("get", func(b *testing.B) { |
| 38 |
storage := newInMemoryWithGC(b, time.Hour, 0) |
| 39 |
require.NoError(b, storage.Set(ctx, benchmarkClientID, benchmarkToken)) |
| 40 |
|
| 41 |
var tokenValue string |
| 42 |
|
| 43 |
b.Cleanup(func() { |
| 44 |
require.NoError(b, storage.Close()) |
| 45 |
}) |
| 46 |
|
| 47 |
b.ReportAllocs() |
| 48 |
b.ResetTimer() |
| 49 |
|
| 50 |
for b.Loop() { |
| 51 |
token, err := storage.Get(ctx, benchmarkClientID) |
| 52 |
if err != nil { |
| 53 |
b.Fatal(err) |
| 54 |
} |
| 55 |
|
| 56 |
tokenValue = token |
| 57 |
} |
| 58 |
|
| 59 |
_ = tokenValue |
| 60 |
}) |
| 61 |
|
| 62 |
b.Run("delete", func(b *testing.B) { |
| 63 |
storage := newInMemoryWithGC(b, time.Hour, 0) |
| 64 |
clientIDs := make([]string, b.N) |
| 65 |
|
| 66 |
for idx := range b.N { |
| 67 |
clientIDs[idx] = benchmarkClientID + "-" + strconv.Itoa(idx) |
| 68 |
|
| 69 |
if err := storage.Set(ctx, clientIDs[idx], benchmarkToken); err != nil { |
| 70 |
b.Fatal(err) |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
b.Cleanup(func() { |
| 75 |
require.NoError(b, storage.Close()) |
| 76 |
}) |
| 77 |
|
| 78 |
b.ReportAllocs() |
| 79 |
b.ResetTimer() |
| 80 |
|
| 81 |
for idx := range b.N { |
| 82 |
if err := storage.Delete(ctx, clientIDs[idx]); err != nil { |
| 83 |
b.Fatal(err) |
| 84 |
} |
| 85 |
} |
| 86 |
}) |
| 87 |
|
| 88 |
b.Run("lifecycle", func(b *testing.B) { |
| 89 |
storage := newInMemoryWithGC(b, time.Hour, 0) |
| 90 |
|
| 91 |
var tokenValue string |
| 92 |
|
| 93 |
b.Cleanup(func() { |
| 94 |
require.NoError(b, storage.Close()) |
| 95 |
}) |
| 96 |
|
| 97 |
b.ReportAllocs() |
| 98 |
b.ResetTimer() |
| 99 |
|
| 100 |
for b.Loop() { |
| 101 |
if err := storage.Set(ctx, benchmarkClientID, benchmarkToken); err != nil { |
| 102 |
b.Fatal(err) |
| 103 |
} |
| 104 |
|
| 105 |
token, err := storage.Get(ctx, benchmarkClientID) |
| 106 |
if err != nil { |
| 107 |
b.Fatal(err) |
| 108 |
} |
| 109 |
|
| 110 |
if err = storage.Delete(ctx, benchmarkClientID); err != nil { |
| 111 |
b.Fatal(err) |
| 112 |
} |
| 113 |
|
| 114 |
tokenValue = token |
| 115 |
} |
| 116 |
|
| 117 |
_ = tokenValue |
| 118 |
}) |
| 119 |
} |