Skip to content

Commit f6c93ff

Browse files
committed
Do not create net.IP in each bechmark loop
1 parent 697da80 commit f6c93ff

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

reader_test.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,8 +509,9 @@ func BenchmarkMaxMindDB(b *testing.B) {
509509
r := rand.New(rand.NewSource(time.Now().UnixNano()))
510510
var result interface{}
511511

512+
ip := make(net.IP, 4, 4)
512513
for i := 0; i < b.N; i++ {
513-
ip := randomIPv4Address(b, r)
514+
randomIPv4Address(b, r, ip)
514515
err = db.Lookup(ip, &result)
515516
assert.Nil(b, err)
516517
}
@@ -530,16 +531,19 @@ func BenchmarkCountryCode(b *testing.B) {
530531
r := rand.New(rand.NewSource(0))
531532
var result MinCountry
532533

534+
ip := make(net.IP, 4, 4)
533535
for i := 0; i < b.N; i++ {
534-
ip := randomIPv4Address(b, r)
536+
randomIPv4Address(b, r, ip)
535537
err = db.Lookup(ip, &result)
536538
assert.Nil(b, err)
537539
}
538540
assert.Nil(b, db.Close(), "error on close")
539541
}
540542

541-
func randomIPv4Address(b *testing.B, r *rand.Rand) net.IP {
543+
func randomIPv4Address(b *testing.B, r *rand.Rand, ip []byte) {
542544
num := r.Uint32()
543-
return []byte{byte(num >> 24), byte(num >> 16), byte(num >> 8),
544-
byte(num)}
545+
ip[0] = byte(num >> 24)
546+
ip[1] = byte(num >> 16)
547+
ip[2] = byte(num >> 8)
548+
ip[3] = byte(num)
545549
}

0 commit comments

Comments
 (0)