mirror of
https://github.com/octoleo/syncthing.git
synced 2024-12-23 03:18:59 +00:00
198028d627
rand.secureSource.Uint64 no longer allocates. rand.String uses a strings.Builder. Benchmark results on linux/amd64: name old time/op new time/op delta SecureSource-8 69.1ns ± 3% 51.7ns ± 3% -25.21% (p=0.000 n=20+10) String-8 2.66µs ± 2% 1.95µs ± 1% -26.61% (p=0.000 n=10+10) name old alloc/op new alloc/op delta SecureSource-8 8.00B ± 0% 0.00B -100.00% (p=0.000 n=20+10) String-8 288B ± 0% 32B ± 0% -88.89% (p=0.000 n=10+10) name old allocs/op new allocs/op delta SecureSource-8 1.00 ± 0% 0.00 -100.00% (p=0.000 n=20+10) String-8 33.0 ± 0% 1.0 ± 0% -96.97% (p=0.000 n=10+10)
54 lines
1.0 KiB
Go
54 lines
1.0 KiB
Go
// Copyright (C) 2014 The Syncthing Authors.
|
|
//
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
// You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
package rand
|
|
|
|
import "testing"
|
|
|
|
func TestRandomString(t *testing.T) {
|
|
for _, l := range []int{0, 1, 2, 3, 4, 8, 42} {
|
|
s := String(l)
|
|
if len(s) != l {
|
|
t.Errorf("Incorrect length %d != %d", len(s), l)
|
|
}
|
|
}
|
|
|
|
strings := make([]string, 1000)
|
|
for i := range strings {
|
|
strings[i] = String(8)
|
|
for j := range strings {
|
|
if i == j {
|
|
continue
|
|
}
|
|
if strings[i] == strings[j] {
|
|
t.Errorf("Repeated random string %q", strings[i])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRandomUint64(t *testing.T) {
|
|
ints := make([]uint64, 1000)
|
|
for i := range ints {
|
|
ints[i] = Uint64()
|
|
for j := range ints {
|
|
if i == j {
|
|
continue
|
|
}
|
|
if ints[i] == ints[j] {
|
|
t.Errorf("Repeated random int64 %d", ints[i])
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func BenchmarkString(b *testing.B) {
|
|
b.ReportAllocs()
|
|
for i := 0; i < b.N; i++ {
|
|
String(32)
|
|
}
|
|
}
|