Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use non-global rand.Rand and add -seed flag #204

Merged
merged 5 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
363 changes: 194 additions & 169 deletions array_bench_test.go

Large diffs are not rendered by default.

40 changes: 20 additions & 20 deletions array_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,25 +48,25 @@ func BenchmarkXXLArray(b *testing.B) { benchmarkArray(b, 10_000_000, opCount) }
func BenchmarkXXXLArray(b *testing.B) { benchmarkArray(b, 100_000_000, opCount) }

// TODO add nested arrays as class 5
func RandomValue() Value {
switch rand.Intn(4) {
func RandomValue(r *rand.Rand) Value {
switch r.Intn(4) {
case 0:
return Uint8Value(rand.Intn(255))
return Uint8Value(r.Intn(255))
case 1:
return Uint16Value(rand.Intn(6535))
return Uint16Value(r.Intn(6535))
case 2:
return Uint32Value(rand.Intn(4294967295))
return Uint32Value(r.Intn(4294967295))
case 3:
return Uint64Value(rand.Intn(1844674407370955161))
return Uint64Value(r.Intn(1844674407370955161))
default:
return Uint8Value(rand.Intn(255))
return Uint8Value(r.Intn(255))
}
}

// BenchmarkArray benchmarks the performance of the atree array
func benchmarkArray(b *testing.B, initialArraySize, numberOfElements int) {

rand.Seed(time.Now().UnixNano())
r := newRand(b)

storage := newTestPersistentStorage(b)

Expand All @@ -89,7 +89,7 @@ func benchmarkArray(b *testing.B, initialArraySize, numberOfElements int) {

// setup
for i := 0; i < initialArraySize; i++ {
v := RandomValue()
v := RandomValue(r)
storable, err := v.Storable(storage, array.Address(), MaxInlineArrayElementSize)
require.NoError(b, err)
totalRawDataSize += storable.ByteSize()
Expand All @@ -108,7 +108,7 @@ func benchmarkArray(b *testing.B, initialArraySize, numberOfElements int) {
// array, err := NewBasicArrayWithRootID(storage, arrayID)
require.NoError(b, err)
for i := 0; i < numberOfElements; i++ {
v := RandomValue()
v := RandomValue(r)

storable, err := v.Storable(storage, array.Address(), MaxInlineArrayElementSize)
require.NoError(b, err)
Expand All @@ -129,7 +129,7 @@ func benchmarkArray(b *testing.B, initialArraySize, numberOfElements int) {
require.NoError(b, err)

for i := 0; i < numberOfElements; i++ {
ind := rand.Intn(int(array.Count()))
ind := r.Intn(int(array.Count()))
storable, err := array.Remove(uint64(ind))
require.NoError(b, err)
totalRawDataSize -= storable.ByteSize()
Expand All @@ -145,8 +145,8 @@ func benchmarkArray(b *testing.B, initialArraySize, numberOfElements int) {
require.NoError(b, err)

for i := 0; i < numberOfElements; i++ {
ind := rand.Intn(int(array.Count()))
v := RandomValue()
ind := r.Intn(int(array.Count()))
v := RandomValue(r)

storable, err := v.Storable(storage, array.Address(), MaxInlineArrayElementSize)
require.NoError(b, err)
Expand All @@ -167,7 +167,7 @@ func benchmarkArray(b *testing.B, initialArraySize, numberOfElements int) {
require.NoError(b, err)

for i := 0; i < numberOfElements; i++ {
ind := rand.Intn(int(array.Count()))
ind := r.Intn(int(array.Count()))
_, err := array.Get(uint64(ind))
require.NoError(b, err)
}
Expand All @@ -181,7 +181,7 @@ func benchmarkArray(b *testing.B, initialArraySize, numberOfElements int) {
// array, err = NewBasicArrayWithRootID(storage, arrayID)
require.NoError(b, err)

ind := rand.Intn(int(array.Count()))
ind := r.Intn(int(array.Count()))
_, err = array.Get(uint64(ind))
require.NoError(b, err)
storageOverheadRatio := float64(storage.baseStorage.Size()) / float64(totalRawDataSize)
Expand All @@ -203,7 +203,7 @@ func BenchmarkLArrayMemoryImpact(b *testing.B) { benchmarkLongTermImpactOnMemory
// BenchmarkArray benchmarks the performance of the atree array
func benchmarkLongTermImpactOnMemory(b *testing.B, initialArraySize, numberOfOps int) {

rand.Seed(time.Now().UnixNano())
r := newRand(b)

storage := newTestPersistentStorage(b)

Expand All @@ -219,7 +219,7 @@ func benchmarkLongTermImpactOnMemory(b *testing.B, initialArraySize, numberOfOps

// setup
for i := 0; i < initialArraySize; i++ {
v := RandomValue()
v := RandomValue(r)

storable, err := v.Storable(storage, array.Address(), MaxInlineArrayElementSize)
require.NoError(b, err)
Expand All @@ -233,15 +233,15 @@ func benchmarkLongTermImpactOnMemory(b *testing.B, initialArraySize, numberOfOps
b.ResetTimer()

for i := 0; i < numberOfOps; i++ {
ind := rand.Intn(int(array.Count()))
ind := r.Intn(int(array.Count()))
// select opt
switch rand.Intn(2) {
switch r.Intn(2) {
case 0: // remove
storable, err := array.Remove(uint64(ind))
require.NoError(b, err)
totalRawDataSize -= storable.ByteSize()
case 1: // insert
v := RandomValue()
v := RandomValue(r)

storable, err := v.Storable(storage, array.Address(), MaxInlineArrayElementSize)
require.NoError(b, err)
Expand Down
Loading