|
| 1 | +// Copyright (c) 2026 Keymaster Team |
| 2 | +// Keymaster - SSH key management system |
| 3 | +// This source code is licensed under the MIT license found in the LICENSE file. |
| 4 | +package slicest |
| 5 | + |
| 6 | +import "testing" |
| 7 | + |
| 8 | +func benchmarkFlattenInput(groups, groupSize int) [][]int { |
| 9 | + in := make([][]int, groups) |
| 10 | + for i := 0; i < groups; i++ { |
| 11 | + row := make([]int, groupSize) |
| 12 | + for j := 0; j < groupSize; j++ { |
| 13 | + row[j] = i*groupSize + j |
| 14 | + } |
| 15 | + in[i] = row |
| 16 | + } |
| 17 | + return in |
| 18 | +} |
| 19 | + |
| 20 | +func BenchmarkFlatten(b *testing.B) { |
| 21 | + cases := []struct { |
| 22 | + name string |
| 23 | + groups int |
| 24 | + groupSize int |
| 25 | + }{ |
| 26 | + {name: "small", groups: 8, groupSize: 8}, |
| 27 | + {name: "medium", groups: 64, groupSize: 16}, |
| 28 | + {name: "large", groups: 256, groupSize: 32}, |
| 29 | + } |
| 30 | + |
| 31 | + for _, tc := range cases { |
| 32 | + b.Run(tc.name, func(b *testing.B) { |
| 33 | + input := benchmarkFlattenInput(tc.groups, tc.groupSize) |
| 34 | + b.ResetTimer() |
| 35 | + for i := 0; i < b.N; i++ { |
| 36 | + _ = Flatten(input) |
| 37 | + } |
| 38 | + }) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +func BenchmarkFlatten2(b *testing.B) { |
| 43 | + cases := []struct { |
| 44 | + name string |
| 45 | + groups int |
| 46 | + groupSize int |
| 47 | + }{ |
| 48 | + {name: "small", groups: 8, groupSize: 8}, |
| 49 | + {name: "medium", groups: 64, groupSize: 16}, |
| 50 | + {name: "large", groups: 256, groupSize: 32}, |
| 51 | + } |
| 52 | + |
| 53 | + for _, tc := range cases { |
| 54 | + b.Run(tc.name, func(b *testing.B) { |
| 55 | + input := benchmarkFlattenInput(tc.groups, tc.groupSize) |
| 56 | + b.ResetTimer() |
| 57 | + for i := 0; i < b.N; i++ { |
| 58 | + _ = Flatten2(input) |
| 59 | + } |
| 60 | + }) |
| 61 | + } |
| 62 | +} |
0 commit comments