Skip to content

Instantly share code, notes, and snippets.

@samaita
Last active August 8, 2018 05:05
Show Gist options
  • Save samaita/89319f1b158d5a1c8e549abecc709d43 to your computer and use it in GitHub Desktop.
Save samaita/89319f1b158d5a1c8e549abecc709d43 to your computer and use it in GitHub Desktop.
Sample of Golang Benchmark for Fraud Check Function
package fraud
import (
"flag"
"os"
"strings"
"testing"
)
func TestMain(m *testing.M) {
initSampleMessage()
initRegex()
initMap()
flag.Parse()
os.Exit(m.Run())
}
func benchIsFraud(b *testing.B, value int) {
for n := 0; n < b.N; n++ {
IsFraud(strings.Repeat(sampleMessage, value))
}
}
func benchIsFraudByMap(b *testing.B, value int) {
for n := 0; n < b.N; n++ {
IsFraudByMap(strings.Repeat(sampleMessage, value))
}
}
func BenchmarkIsFraud1(b *testing.B) { benchIsFraud(b, 1) }
func BenchmarkIsFraud10(b *testing.B) { benchIsFraud(b, 10) }
func BenchmarkIsFraud100(b *testing.B) { benchIsFraud(b, 100) }
func BenchmarkIsFraud1000(b *testing.B) { benchIsFraud(b, 1000) }
func BenchmarkIsFraudByMap1(b *testing.B) { benchIsFraudByMap(b, 1) }
func BenchmarkIsFraudByMap10(b *testing.B) { benchIsFraudByMap(b, 10) }
func BenchmarkIsFraudByMap100(b *testing.B) { benchIsFraudByMap(b, 100) }
func BenchmarkIsFraudByMap1000(b *testing.B) { benchIsFraudByMap(b, 1000) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment