package benchmarks import ( "fmt" "testing" "time" "mengstack/internal/modules/auth/application" "golang.org/x/crypto/bcrypt" ) func BenchmarkJWT_GenerateToken(b *testing.B) { secret := "benchmark-secret-key-for-testing-only" b.ResetTimer() for i := 0; i < b.N; i++ { _, err := application.GenerateToken(1, "tenant-1", "access", 15*time.Minute, secret, "mengstack") if err != nil { b.Fatal(err) } } } func BenchmarkJWT_ParseToken(b *testing.B) { secret := "benchmark-secret-key-for-testing-only" token, err := application.GenerateToken(1, "tenant-1", "access", 15*time.Minute, secret, "mengstack") if err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { _, err := application.ParseToken(token, secret) if err != nil { b.Fatal(err) } } } func BenchmarkBcrypt_Hash(b *testing.B) { password := "benchmark-password-12345" b.ResetTimer() for i := 0; i < b.N; i++ { _, err := bcrypt.GenerateFromPassword([]byte(password), 12) if err != nil { b.Fatal(err) } } } func BenchmarkBcrypt_Compare(b *testing.B) { password := "benchmark-password-12345" hash, err := bcrypt.GenerateFromPassword([]byte(password), 12) if err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { _ = bcrypt.CompareHashAndPassword(hash, []byte(password)) } } func BenchmarkBcrypt_Compare_Wrong(b *testing.B) { password := "benchmark-password-12345" wrong := "wrong-password-67890" hash, err := bcrypt.GenerateFromPassword([]byte(password), 12) if err != nil { b.Fatal(err) } b.ResetTimer() for i := 0; i < b.N; i++ { _ = bcrypt.CompareHashAndPassword(hash, []byte(wrong)) } } func BenchmarkJWT_Throughput(b *testing.B) { secret := "benchmark-secret-key-for-testing-only" b.RunParallel(func(pb *testing.PB) { i := uint(0) for pb.Next() { token, err := application.GenerateToken(i, "tenant-1", "access", 15*time.Minute, secret, "mengstack") if err != nil { b.Fatal(err) } _, err = application.ParseToken(token, secret) if err != nil { b.Fatal(err) } i++ } }) } func BenchmarkBcrypt_CostComparison(b *testing.B) { password := "benchmark-password-12345" for _, cost := range []int{10, 12, 14} { b.Run(fmt.Sprintf("cost_%d", cost), func(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { _, err := bcrypt.GenerateFromPassword([]byte(password), cost) if err != nil { b.Fatal(err) } } }) } }