Skip to content

Commit e0b26cb

Browse files
tests: add standalone benchmark environment module
Auxiliary module with utils and benchmark_test for local environment validation. Made-with: Cursor
1 parent e9f6761 commit e0b26cb

File tree

3 files changed

+184
-0
lines changed

3 files changed

+184
-0
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
"test-environment/utils"
6+
)
7+
8+
func BenchmarkStringProcessor(b *testing.B) {
9+
processor := utils.NewStringProcessor()
10+
generator := utils.NewDataGenerator()
11+
12+
// Generate test data
13+
strings := generator.GenerateStrings(1000)
14+
for _, s := range strings {
15+
processor.AddString(s)
16+
}
17+
18+
b.ResetTimer()
19+
for i := 0; i < b.N; i++ {
20+
result := processor.ProcessStrings()
21+
_ = result
22+
}
23+
}
24+
25+
func BenchmarkFibonacci(b *testing.B) {
26+
calc := utils.NewCalculator()
27+
28+
b.ResetTimer()
29+
for i := 0; i < b.N; i++ {
30+
result := calc.Fibonacci(25)
31+
_ = result
32+
}
33+
}
34+
35+
func BenchmarkMatrixMultiplication(b *testing.B) {
36+
calc := utils.NewCalculator()
37+
generator := utils.NewDataGenerator()
38+
39+
// Generate test matrices
40+
matrixA := generator.GenerateMatrix(50, 50)
41+
matrixB := generator.GenerateMatrix(50, 50)
42+
43+
b.ResetTimer()
44+
for i := 0; i < b.N; i++ {
45+
result := calc.MatrixMultiply(matrixA, matrixB)
46+
_ = result
47+
}
48+
}
49+
50+
func BenchmarkDataGeneration(b *testing.B) {
51+
generator := utils.NewDataGenerator()
52+
53+
b.ResetTimer()
54+
for i := 0; i < b.N; i++ {
55+
strings := generator.GenerateStrings(500)
56+
matrix := generator.GenerateMatrix(20, 20)
57+
_ = strings
58+
_ = matrix
59+
}
60+
}

tests/Enviroment testing/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module test-environment
2+
3+
go 1.25.0
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
package utils
2+
3+
import (
4+
"crypto/sha256"
5+
"fmt"
6+
"sort"
7+
"strings"
8+
)
9+
10+
// StringProcessor provides string manipulation utilities
11+
type StringProcessor struct {
12+
data []string
13+
}
14+
15+
// NewStringProcessor creates a new string processor
16+
func NewStringProcessor() *StringProcessor {
17+
return &StringProcessor{
18+
data: make([]string, 0),
19+
}
20+
}
21+
22+
// AddString adds a string to the processor
23+
func (sp *StringProcessor) AddString(s string) {
24+
sp.data = append(sp.data, s)
25+
}
26+
27+
// ProcessStrings performs various operations on stored strings
28+
func (sp *StringProcessor) ProcessStrings() map[string]interface{} {
29+
result := make(map[string]interface{})
30+
31+
// Sort strings
32+
sorted := make([]string, len(sp.data))
33+
copy(sorted, sp.data)
34+
sort.Strings(sorted)
35+
result["sorted"] = sorted
36+
37+
// Calculate total length
38+
totalLen := 0
39+
for _, s := range sp.data {
40+
totalLen += len(s)
41+
}
42+
result["total_length"] = totalLen
43+
44+
// Generate hashes
45+
hashes := make([]string, len(sp.data))
46+
for i, s := range sp.data {
47+
hash := sha256.Sum256([]byte(s))
48+
hashes[i] = fmt.Sprintf("%x", hash)
49+
}
50+
result["hashes"] = hashes
51+
52+
return result
53+
}
54+
55+
// Calculator provides mathematical operations
56+
type Calculator struct{}
57+
58+
// NewCalculator creates a new calculator
59+
func NewCalculator() *Calculator {
60+
return &Calculator{}
61+
}
62+
63+
// Fibonacci calculates fibonacci number (CPU intensive)
64+
func (c *Calculator) Fibonacci(n int) int {
65+
if n <= 1 {
66+
return n
67+
}
68+
return c.Fibonacci(n-1) + c.Fibonacci(n-2)
69+
}
70+
71+
// MatrixMultiply performs matrix multiplication
72+
func (c *Calculator) MatrixMultiply(a, b [][]int) [][]int {
73+
if len(a) == 0 || len(b) == 0 || len(a[0]) != len(b) {
74+
return nil
75+
}
76+
77+
rows, cols := len(a), len(b[0])
78+
result := make([][]int, rows)
79+
for i := range result {
80+
result[i] = make([]int, cols)
81+
}
82+
83+
for i := 0; i < rows; i++ {
84+
for j := 0; j < cols; j++ {
85+
for k := 0; k < len(b); k++ {
86+
result[i][j] += a[i][k] * b[k][j]
87+
}
88+
}
89+
}
90+
91+
return result
92+
}
93+
94+
// DataGenerator generates test data
95+
type DataGenerator struct{}
96+
97+
// NewDataGenerator creates a new data generator
98+
func NewDataGenerator() *DataGenerator {
99+
return &DataGenerator{}
100+
}
101+
102+
// GenerateStrings creates a slice of strings
103+
func (dg *DataGenerator) GenerateStrings(count int) []string {
104+
result := make([]string, count)
105+
for i := 0; i < count; i++ {
106+
result[i] = fmt.Sprintf("generated_string_%d_%s", i, strings.Repeat("x", i%100))
107+
}
108+
return result
109+
}
110+
111+
// GenerateMatrix creates a matrix of given size
112+
func (dg *DataGenerator) GenerateMatrix(rows, cols int) [][]int {
113+
matrix := make([][]int, rows)
114+
for i := range matrix {
115+
matrix[i] = make([]int, cols)
116+
for j := range matrix[i] {
117+
matrix[i][j] = (i + j) % 100
118+
}
119+
}
120+
return matrix
121+
}

0 commit comments

Comments
 (0)