Skip to content

Commit c619bbb

Browse files
committed
benchmark create ClusterCatalog func
1 parent 81f0cfd commit c619bbb

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed

Diff for: test/e2e/benchmark_test.go

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package e2e
2+
3+
import (
4+
"context"
5+
"math/rand"
6+
"os"
7+
"sync"
8+
"testing"
9+
"time"
10+
)
11+
12+
func BenchmarkCreateClusterCatalog(b *testing.B) {
13+
catalogImageRef := os.Getenv(testCatalogRefEnvVar)
14+
if catalogImageRef == "" {
15+
b.Fatalf("environment variable %s is not set", testCatalogRefEnvVar)
16+
}
17+
ctx := context.Background()
18+
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)
35+
}
36+
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)
85+
}

Diff for: test/e2e/e2e_suite_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ func createTestCatalog(ctx context.Context, name string, imageRef string) (*cata
6565
return catalog, err
6666
}
6767

68+
func deleteTestCatalog(ctx context.Context, catalog *catalogd.ClusterCatalog) error {
69+
return c.Delete(ctx, catalog)
70+
}
71+
6872
// patchTestCatalog will patch the existing clusterCatalog on the test cluster, provided
6973
// the context, catalog name, and the image reference. It returns an error
7074
// if any errors occurred while updating the catalog.

0 commit comments

Comments
 (0)