Skip to content

Commit aafcee0

Browse files
sjsanckashifkhan0771coderabbitai[bot]
authored
Add basic benchmarks (#106)
* add various benchmarks (#81) add caching and rand benchmarks add fsutils benchmarks add logging, math, strings, templates and url benchmarks * fix linting errors * Update logging/logging_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com> * Update caching/caching_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com> * Update math/math_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com> * Update rand/rand_test.go Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> Signed-off-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com> * go fmt caching_test.go Signed-off-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com> * go fmt rand_test.go Signed-off-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com> --------- Signed-off-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com> Co-authored-by: Kashif Khan <70996046+kashifkhan0771@users.noreply.github.com> Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
1 parent 0a16c74 commit aafcee0

File tree

13 files changed

+683
-37
lines changed

13 files changed

+683
-37
lines changed

caching/caching_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,3 +116,58 @@ func TestSafeCacheWrapperConcurrency(t *testing.T) {
116116
}
117117
}
118118
}
119+
120+
// ================================================================================
121+
// ### BENCHMARKS
122+
// ================================================================================
123+
124+
func fib(n int) int {
125+
if n <= 1 {
126+
return n
127+
}
128+
a, b := 0, 1
129+
for i := 2; i <= n; i++ {
130+
a, b = b, a+b
131+
}
132+
return b
133+
}
134+
135+
func BenchmarkFib(b *testing.B) {
136+
b.ReportAllocs()
137+
b.ResetTimer()
138+
for i := 0; i < b.N; i++ {
139+
_ = fib(30)
140+
}
141+
}
142+
143+
func BenchmarkCachedFib(b *testing.B) {
144+
cachedFib := CacheWrapper(fib)
145+
146+
b.ReportAllocs()
147+
b.ResetTimer()
148+
for i := 0; i < b.N; i++ {
149+
_ = cachedFib(30)
150+
}
151+
}
152+
153+
func BenchmarkSafeCachedFib(b *testing.B) {
154+
cachedFib := SafeCacheWrapper(fib)
155+
156+
b.ReportAllocs()
157+
b.ResetTimer()
158+
for i := 0; i < b.N; i++ {
159+
_ = cachedFib(30)
160+
}
161+
}
162+
163+
func BenchmarkConcurrentSafeCachedFib(b *testing.B) {
164+
cachedFib := SafeCacheWrapper(fib)
165+
166+
b.ReportAllocs()
167+
b.ResetTimer()
168+
b.RunParallel(func(pb *testing.PB) {
169+
for pb.Next() {
170+
_ = cachedFib(30)
171+
}
172+
})
173+
}

ctxutils/ctxutils_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package ctxutils
22

33
import (
44
"context"
5+
"fmt"
56
"testing"
67
)
78

9+
// TODO: these can be written as a table test
10+
811
func TestSetStringValueAndGetStringValue(t *testing.T) {
912
ctx := context.Background()
1013

@@ -67,3 +70,35 @@ func TestSetIntValueWithWrongKey(t *testing.T) {
6770
t.Errorf("Expected value not to be found, but it was.")
6871
}
6972
}
73+
74+
// ================================================================================
75+
// ### BENCHMARKS
76+
// ================================================================================
77+
78+
func BenchmarkSettingAndGettingStringKey(b *testing.B) {
79+
ctx := context.Background()
80+
81+
b.ReportAllocs()
82+
b.ResetTimer()
83+
84+
key := ContextKeyString{Key: "id"}
85+
86+
for i := 0; i < b.N; i++ {
87+
ctx = SetStringValue(ctx, key, fmt.Sprintf("value-%d", i))
88+
_, _ = GetStringValue(ctx, key)
89+
}
90+
}
91+
92+
func BenchmarkSettingAndGettingIntKey(b *testing.B) {
93+
ctx := context.Background()
94+
95+
b.ReportAllocs()
96+
b.ResetTimer()
97+
98+
key := ContextKeyInt{Key: 0}
99+
100+
for i := 0; i < b.N; i++ {
101+
ctx = SetIntValue(ctx, key, i)
102+
_, _ = GetIntValue(ctx, key)
103+
}
104+
}

fake/fake_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,3 +75,31 @@ func TestRandomAddress(t *testing.T) {
7575
t.Errorf("Generated address %v does not match the expected format", address)
7676
}
7777
}
78+
79+
// ================================================================================
80+
// ### BENCHMARKS
81+
// ================================================================================
82+
83+
func BenchmarkGenerateUUID(b *testing.B) {
84+
for i := 0; i < b.N; i++ {
85+
_, _ = RandomUUID()
86+
}
87+
}
88+
89+
func BenchmarkRandomDate(b *testing.B) {
90+
for i := 0; i < b.N; i++ {
91+
_, _ = RandomDate()
92+
}
93+
}
94+
95+
func BenchmarkRandomPhoneNumber(b *testing.B) {
96+
for i := 0; i < b.N; i++ {
97+
_, _ = RandomPhoneNumber()
98+
}
99+
}
100+
101+
func BenchmarkRandomAddress(b *testing.B) {
102+
for i := 0; i < b.N; i++ {
103+
_, _ = RandomAddress()
104+
}
105+
}

fsutils/fsutils_test.go

Lines changed: 157 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package fsutils
22

33
import (
4+
"crypto/rand"
45
"fmt"
56
"os"
67
"path/filepath"
@@ -333,7 +334,11 @@ func TestDirsIdentical(t *testing.T) {
333334
})
334335

335336
t.Run("nested directories", func(t *testing.T) {
336-
dir1, dir2 := setupNestedDirs(t)
337+
dir1, dir2, err := setupNestedDirs()
338+
if err != nil {
339+
t.Fatal(err)
340+
}
341+
337342
defer os.RemoveAll(dir1)
338343
defer os.RemoveAll(dir2)
339344

@@ -347,54 +352,47 @@ func TestDirsIdentical(t *testing.T) {
347352
})
348353
}
349354

350-
func setupNestedDirs(t *testing.T) (string, string) {
351-
// Create temporary directories for testing
352-
tempDir1, err := os.MkdirTemp("", "nestedtestdir1")
355+
func setupNestedDirs() (string, string, error) {
356+
tempDir1, err := os.MkdirTemp("", "nestedtestdir1_")
353357
if err != nil {
354-
t.Fatal(err)
358+
return "", "", fmt.Errorf("failed to create tempDir1: %w", err)
355359
}
360+
defer func() {
361+
if err != nil {
362+
os.RemoveAll(tempDir1)
363+
}
364+
}()
356365

357-
tempDir2, err := os.MkdirTemp("", "nestedtestdir2")
366+
tempDir2, err := os.MkdirTemp("", "nestedtestdir2_")
358367
if err != nil {
359-
t.Fatal(err)
368+
os.RemoveAll(tempDir1)
369+
return "", "", fmt.Errorf("failed to create tempDir2: %w", err)
360370
}
361-
362-
// Create nested directory structure in both directories
363-
nestedDirs := []string{
364-
"dir1",
365-
"dir1/dir2",
366-
"dir1/dir2/dir3",
367-
}
368-
369-
for _, dir := range nestedDirs {
370-
if err := os.MkdirAll(filepath.Join(tempDir1, dir), 0755); err != nil {
371-
t.Fatal(err)
372-
}
373-
if err := os.MkdirAll(filepath.Join(tempDir2, dir), 0755); err != nil {
374-
t.Fatal(err)
371+
defer func() {
372+
if err != nil {
373+
os.RemoveAll(tempDir2)
375374
}
376-
}
375+
}()
377376

378-
// Create some test files in the nested directories
379-
files := []struct {
380-
path string
381-
contents string
382-
}{
383-
{path: filepath.Join(tempDir1, "dir1/file1.txt"), contents: "file1"},
384-
{path: filepath.Join(tempDir1, "dir1/dir2/file2.txt"), contents: "file2"},
385-
{path: filepath.Join(tempDir1, "dir1/dir2/dir3/file3.txt"), contents: "file3"},
377+
files := map[string]string{
378+
"dir1/file1.txt": "file1",
379+
"dir1/dir2/file2.txt": "file2",
380+
"dir1/dir2/dir3/file3.txt": "file3",
386381
}
387382

388-
for _, file := range files {
389-
if err := os.WriteFile(file.path, []byte(file.contents), 0644); err != nil {
390-
t.Fatal(err)
391-
}
392-
if err := os.WriteFile(filepath.Join(tempDir2, file.path[len(tempDir1):]), []byte(file.contents), 0644); err != nil {
393-
t.Fatal(err)
383+
for _, base := range []string{tempDir1, tempDir2} {
384+
for relPath, content := range files {
385+
fullPath := filepath.Join(base, relPath)
386+
if err = os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
387+
return "", "", fmt.Errorf("failed to create directory for %s: %w", fullPath, err)
388+
}
389+
if err = os.WriteFile(fullPath, []byte(content), 0644); err != nil {
390+
return "", "", fmt.Errorf("failed to write file %s: %w", fullPath, err)
391+
}
394392
}
395393
}
396394

397-
return tempDir1, tempDir2
395+
return tempDir1, tempDir2, nil
398396
}
399397

400398
func TestGetFileMetadata(t *testing.T) {
@@ -500,3 +498,125 @@ func TestGetFileMetadata(t *testing.T) {
500498
}
501499
})
502500
}
501+
502+
// ================================================================================
503+
// ### BENCMARKS
504+
// ================================================================================
505+
506+
func BenchmarkFormatFileSize(b *testing.B) {
507+
b.ReportAllocs()
508+
for i := 0; i < b.N; i++ {
509+
_ = FormatFileSize(int64(i))
510+
}
511+
}
512+
513+
func BenchmarkFindFiles(b *testing.B) {
514+
tempDir, err := os.MkdirTemp("", "testdir")
515+
if err != nil {
516+
b.Fatal(err)
517+
}
518+
defer os.RemoveAll(tempDir)
519+
520+
for i := 0; i < 100; i++ {
521+
filePath := filepath.Join(tempDir, fmt.Sprintf("%d.txt", i))
522+
if err := os.WriteFile(filePath, []byte{}, 0644); err != nil {
523+
b.Fatal(err)
524+
}
525+
}
526+
527+
b.ReportAllocs()
528+
b.ResetTimer()
529+
530+
for i := 0; i < b.N; i++ {
531+
_, _ = FindFiles(tempDir, ".txt")
532+
}
533+
}
534+
535+
func BenchmarkGetDirectorySize(b *testing.B) {
536+
tempDir, err := os.MkdirTemp("", "testdir")
537+
if err != nil {
538+
b.Fatal(err)
539+
}
540+
defer os.RemoveAll(tempDir)
541+
542+
for i := 0; i < 100; i++ {
543+
filePath := filepath.Join(tempDir, fmt.Sprintf("%d.txt", i))
544+
data := make([]byte, 1024) // 1 KB per file
545+
if _, err := rand.Read(data); err != nil {
546+
b.Fatal(err)
547+
}
548+
if err := os.WriteFile(filePath, data, 0644); err != nil {
549+
b.Fatal(err)
550+
}
551+
}
552+
b.ReportAllocs()
553+
b.ResetTimer()
554+
555+
for i := 0; i < b.N; i++ {
556+
_, _ = GetDirectorySize(tempDir)
557+
}
558+
}
559+
560+
func BenchmarkFilesIdentical(b *testing.B) {
561+
tempDir, err := os.MkdirTemp("", "testdir")
562+
if err != nil {
563+
b.Fatal(err)
564+
}
565+
defer os.RemoveAll(tempDir)
566+
567+
file1 := filepath.Join(tempDir, "file1.txt")
568+
file2 := filepath.Join(tempDir, "file2.txt")
569+
570+
data := make([]byte, 1024)
571+
if _, err := rand.Read(data); err != nil {
572+
b.Fatal(err)
573+
}
574+
575+
if err := os.WriteFile(file1, data, 0644); err != nil {
576+
b.Fatal(err)
577+
}
578+
if err := os.WriteFile(file2, data, 0644); err != nil {
579+
b.Fatal(err)
580+
}
581+
582+
b.ReportAllocs()
583+
b.ResetTimer()
584+
585+
for i := 0; i < b.N; i++ {
586+
_, _ = FilesIdentical(file1, file2)
587+
}
588+
}
589+
590+
func BenchmarkDirsIdentical(b *testing.B) {
591+
dir1, dir2, err := setupNestedDirs()
592+
if err != nil {
593+
b.Fatal(err)
594+
}
595+
596+
b.ReportAllocs()
597+
b.ResetTimer()
598+
599+
for i := 0; i < b.N; i++ {
600+
_, _ = DirsIdentical(dir1, dir2)
601+
}
602+
}
603+
604+
func BenchmarkGetFileMetadata(b *testing.B) {
605+
tempDir, err := os.MkdirTemp("", "testdir")
606+
if err != nil {
607+
b.Fatal(err)
608+
}
609+
defer os.RemoveAll(tempDir)
610+
611+
filePath := filepath.Join(tempDir, "testfile.txt")
612+
if err := os.WriteFile(filePath, []byte("test"), 0644); err != nil {
613+
b.Fatal(err)
614+
}
615+
616+
b.ReportAllocs()
617+
b.ResetTimer()
618+
619+
for i := 0; i < b.N; i++ {
620+
_, _ = GetFileMetadata(filePath)
621+
}
622+
}

logging/logging_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,16 @@ func TestLogger(t *testing.T) {
9595
})
9696
}
9797
}
98+
99+
// ================================================================================
100+
// ### BENCHMARKS
101+
// ================================================================================
102+
103+
func BenchmarkLogger(b *testing.B) {
104+
logger := logging.NewLogger("Test", logging.INFO, nil)
105+
b.ReportAllocs()
106+
b.ResetTimer()
107+
for i := 0; i < b.N; i++ {
108+
logger.Info("This is an info message")
109+
}
110+
}

0 commit comments

Comments
 (0)