Skip to content

Commit 7524b4f

Browse files
committed
use rand.String
1 parent 63ea7ea commit 7524b4f

File tree

1 file changed

+22
-67
lines changed

1 file changed

+22
-67
lines changed

test/e2e/benchmark_test.go

+22-67
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ package e2e
22

33
import (
44
"context"
5-
"math/rand"
65
"os"
7-
"sync"
86
"testing"
9-
"time"
7+
8+
"k8s.io/apimachinery/pkg/util/rand"
109
)
1110

1211
func BenchmarkCreateClusterCatalog(b *testing.B) {
@@ -16,70 +15,26 @@ func BenchmarkCreateClusterCatalog(b *testing.B) {
1615
}
1716
ctx := context.Background()
1817
b.ResetTimer()
19-
// b.RunParallel(func(pb *testing.PB) {
20-
// for pb.Next() {
21-
// catalogObj, err := createTestCatalog(ctx, getRandomStringParallel(6), catalogImageRef)
22-
// if err != nil {
23-
// b.Logf("failed to create ClusterCatalog: %v", err)
24-
// }
25-
26-
// if err := deleteTestCatalog(ctx, catalogObj); err != nil {
27-
// b.Logf("failed to remove ClusterCatalog: %v", err)
28-
// }
29-
// }
30-
// })
31-
for i := 0; i < b.N; i++ {
32-
catalogObj, err := createTestCatalog(ctx, getRandomString(8), catalogImageRef)
33-
if err != nil {
34-
b.Logf("failed to create ClusterCatalog: %v", err)
18+
b.RunParallel(func(pb *testing.PB) {
19+
for pb.Next() {
20+
catalogObj, err := createTestCatalog(ctx, rand.String(6), catalogImageRef)
21+
if err != nil {
22+
b.Logf("failed to create ClusterCatalog: %v", err)
23+
}
24+
25+
if err := deleteTestCatalog(ctx, catalogObj); err != nil {
26+
b.Logf("failed to remove ClusterCatalog: %v", err)
27+
}
3528
}
29+
})
30+
// for i := 0; i < b.N; i++ {
31+
// catalogObj, err := createTestCatalog(ctx, rand.String(8), catalogImageRef)
32+
// if err != nil {
33+
// b.Logf("failed to create ClusterCatalog: %v", err)
34+
// }
3635

37-
if err := deleteTestCatalog(ctx, catalogObj); err != nil {
38-
b.Logf("failed to remove ClusterCatalog: %v", err)
39-
}
40-
}
41-
}
42-
43-
var (
44-
mu sync.Mutex
45-
usedChars = make(map[string]struct{})
46-
alphabet = "abcdefghijklmnopqrstuvwxyz"
47-
)
48-
49-
func getRandomStringParallel(length int) string {
50-
// Ensure we seed the random number generator only once
51-
rand.Seed(time.Now().UnixNano())
52-
53-
// Lock to ensure no concurrent access to shared resources (e.g., usedChars)
54-
mu.Lock()
55-
defer mu.Unlock()
56-
57-
// Try generating a random string and ensure it's unique
58-
for {
59-
var result []rune
60-
for i := 0; i < length; i++ {
61-
result = append(result, rune(alphabet[rand.Intn(len(alphabet))]))
62-
}
63-
// Convert result to string
64-
randomStr := string(result)
65-
66-
// Check if the generated string is unique
67-
if _, exists := usedChars[randomStr]; !exists {
68-
// If it's unique, add it to the map and return it
69-
usedChars[randomStr] = struct{}{}
70-
return randomStr
71-
}
72-
}
73-
}
74-
75-
// GetRandomString generates a random string of the given length
76-
func getRandomString(length int) string {
77-
const charset = "abcdefghijklmnopqrstuvwxyz"
78-
seededRand := rand.New(rand.NewSource(time.Now().UnixNano()))
79-
80-
b := make([]byte, length)
81-
for i := range b {
82-
b[i] = charset[seededRand.Intn(len(charset))]
83-
}
84-
return string(b)
36+
// if err := deleteTestCatalog(ctx, catalogObj); err != nil {
37+
// b.Logf("failed to remove ClusterCatalog: %v", err)
38+
// }
39+
// }
8540
}

0 commit comments

Comments
 (0)