Skip to content

Commit

Permalink
Decouple non-test and test code
Browse files Browse the repository at this point in the history
This commit moves tests to test package to improve decoupling.
  • Loading branch information
fxamacker committed Feb 7, 2025
1 parent b08becc commit ae1d976
Show file tree
Hide file tree
Showing 16 changed files with 4,060 additions and 4,041 deletions.
4 changes: 4 additions & 0 deletions array.go
Original file line number Diff line number Diff line change
Expand Up @@ -3300,6 +3300,10 @@ func (a *Array) getMutableElementIndexCount() int {
return len(a.mutableElementIndex)
}

func (a *Array) getMutableElementIndex() map[ValueID]uint64 {
return a.mutableElementIndex
}

// Storable returns array a as either:
// - SlabIDStorable, or
// - inlined data slab storable
Expand Down
30 changes: 16 additions & 14 deletions array_bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,19 @@
* limitations under the License.
*/

package atree
package atree_test

import (
"math/rand"
"testing"

"github.com/stretchr/testify/require"

"github.com/onflow/atree"
)

var noopValue Value
var noopStorable Storable
var noopValue atree.Value
var noopStorable atree.Storable

func BenchmarkArrayGet100x(b *testing.B) {
benchmarks := []struct {
Expand Down Expand Up @@ -186,13 +188,13 @@ func BenchmarkNewArrayFromBatchData(b *testing.B) {
}
}

func setupArray(b *testing.B, r *rand.Rand, storage *PersistentSlabStorage, initialArrayCount int) *Array {
func setupArray(b *testing.B, r *rand.Rand, storage *atree.PersistentSlabStorage, initialArrayCount int) *atree.Array {

address := Address{1, 2, 3, 4, 5, 6, 7, 8}
address := atree.Address{1, 2, 3, 4, 5, 6, 7, 8}

typeInfo := testTypeInfo{42}

array, err := NewArray(storage, address, typeInfo)
array, err := atree.NewArray(storage, address, typeInfo)
require.NoError(b, err)

for i := 0; i < initialArrayCount; i++ {
Expand All @@ -208,7 +210,7 @@ func setupArray(b *testing.B, r *rand.Rand, storage *PersistentSlabStorage, init

storage.DropCache()

newArray, err := NewArrayWithRootID(storage, arrayID)
newArray, err := atree.NewArrayWithRootID(storage, arrayID)
require.NoError(b, err)

return newArray
Expand All @@ -224,7 +226,7 @@ func benchmarkArrayGet(b *testing.B, initialArrayCount, numberOfOps int) {

array := setupArray(b, r, storage, initialArrayCount)

var value Value
var value atree.Value

b.StartTimer()

Expand Down Expand Up @@ -293,7 +295,7 @@ func benchmarkArrayRemoveAll(b *testing.B, initialArrayCount int) {

storage := newTestPersistentStorage(b)

var storable Storable
var storable atree.Storable

for i := 0; i < b.N; i++ {

Expand All @@ -319,7 +321,7 @@ func benchmarkArrayPopIterate(b *testing.B, initialArrayCount int) {

storage := newTestPersistentStorage(b)

var storable Storable
var storable atree.Storable

for i := 0; i < b.N; i++ {

Expand All @@ -329,7 +331,7 @@ func benchmarkArrayPopIterate(b *testing.B, initialArrayCount int) {

b.StartTimer()

err := array.PopIterate(func(s Storable) {
err := array.PopIterate(func(s atree.Storable) {
storable = s
})
if err != nil {
Expand All @@ -353,9 +355,9 @@ func benchmarkNewArrayFromAppend(b *testing.B, initialArrayCount int) {
b.StartTimer()

for i := 0; i < b.N; i++ {
copied, _ := NewArray(storage, array.Address(), array.Type())
copied, _ := atree.NewArray(storage, array.Address(), array.Type())

_ = array.IterateReadOnly(func(value Value) (bool, error) {
_ = array.IterateReadOnly(func(value atree.Value) (bool, error) {
_ = copied.Append(value)
return true, nil
})
Expand All @@ -382,7 +384,7 @@ func benchmarkNewArrayFromBatchData(b *testing.B, initialArrayCount int) {
iter, err := array.ReadOnlyIterator()
require.NoError(b, err)

copied, _ := NewArrayFromBatchData(storage, array.Address(), array.Type(), func() (Value, error) {
copied, _ := atree.NewArrayFromBatchData(storage, array.Address(), array.Type(), func() (atree.Value, error) {
return iter.Next()
})

Expand Down
38 changes: 20 additions & 18 deletions array_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@
* limitations under the License.
*/

package atree
package atree_test

import (
"math/rand"
"testing"
"time"

"github.com/stretchr/testify/require"

"github.com/onflow/atree"
)

// GENERAL COMMENT:
Expand All @@ -48,7 +50,7 @@ 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(r *rand.Rand) Value {
func RandomValue(r *rand.Rand) atree.Value {
switch r.Intn(4) {
case 0:
return Uint8Value(r.Intn(255))
Expand All @@ -70,11 +72,11 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {

storage := newTestPersistentStorage(b)

address := Address{1, 2, 3, 4, 5, 6, 7, 8}
address := atree.Address{1, 2, 3, 4, 5, 6, 7, 8}

typeInfo := testTypeInfo{42}

array, err := NewArray(storage, address, typeInfo)
array, err := atree.NewArray(storage, address, typeInfo)

require.NoError(b, err)

Expand All @@ -88,7 +90,7 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
// setup
for i := 0; i < initialArrayCount; i++ {
v := RandomValue(r)
storable, err := v.Storable(storage, array.Address(), MaxInlineArrayElementSize())
storable, err := v.Storable(storage, array.Address(), atree.MaxInlineArrayElementSize())
require.NoError(b, err)
totalRawDataSize += storable.ByteSize()
err = array.Append(v)
Expand All @@ -102,12 +104,12 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
// append
storage.DropCache()
start = time.Now()
array, err = NewArrayWithRootID(storage, arrayID)
array, err = atree.NewArrayWithRootID(storage, arrayID)
require.NoError(b, err)
for i := 0; i < numberOfElements; i++ {
v := RandomValue(r)

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

totalRawDataSize += storable.ByteSize()
Expand All @@ -121,7 +123,7 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
// remove
storage.DropCache()
start = time.Now()
array, err = NewArrayWithRootID(storage, arrayID)
array, err = atree.NewArrayWithRootID(storage, arrayID)
require.NoError(b, err)

for i := 0; i < numberOfElements; i++ {
Expand All @@ -136,14 +138,14 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
// insert
storage.DropCache()
start = time.Now()
array, err = NewArrayWithRootID(storage, arrayID)
array, err = atree.NewArrayWithRootID(storage, arrayID)
require.NoError(b, err)

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

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

totalRawDataSize += storable.ByteSize()
Expand All @@ -157,7 +159,7 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
// lookup
storage.DropCache()
start = time.Now()
array, err = NewArrayWithRootID(storage, arrayID)
array, err = atree.NewArrayWithRootID(storage, arrayID)
require.NoError(b, err)

for i := 0; i < numberOfElements; i++ {
Expand All @@ -168,12 +170,12 @@ func benchmarkArray(b *testing.B, initialArrayCount, numberOfElements int) {
require.NoError(b, storage.Commit())
totalLookupTime = time.Since(start)

baseStorage := GetBaseStorage(storage)
baseStorage := atree.GetBaseStorage(storage)

// random lookup
baseStorage.ResetReporter()
storage.DropCache()
array, err = NewArrayWithRootID(storage, arrayID)
array, err = atree.NewArrayWithRootID(storage, arrayID)
require.NoError(b, err)

ind := r.Intn(int(array.Count()))
Expand Down Expand Up @@ -203,11 +205,11 @@ func benchmarkLongTermImpactOnMemory(b *testing.B, initialArrayCount, numberOfOp

storage := newTestPersistentStorage(b)

address := Address{1, 2, 3, 4, 5, 6, 7, 8}
address := atree.Address{1, 2, 3, 4, 5, 6, 7, 8}

typeInfo := testTypeInfo{42}

array, err := NewArray(storage, address, typeInfo)
array, err := atree.NewArray(storage, address, typeInfo)

require.NoError(b, err)

Expand All @@ -217,7 +219,7 @@ func benchmarkLongTermImpactOnMemory(b *testing.B, initialArrayCount, numberOfOp
for i := 0; i < initialArrayCount; i++ {
v := RandomValue(r)

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

totalRawDataSize += storable.ByteSize()
Expand All @@ -239,7 +241,7 @@ func benchmarkLongTermImpactOnMemory(b *testing.B, initialArrayCount, numberOfOp
case 1: // insert
v := RandomValue(r)

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

totalRawDataSize += storable.ByteSize()
Expand All @@ -250,7 +252,7 @@ func benchmarkLongTermImpactOnMemory(b *testing.B, initialArrayCount, numberOfOp
}
require.NoError(b, storage.Commit())

baseStorage := GetBaseStorage(storage)
baseStorage := atree.GetBaseStorage(storage)

storageOverheadRatio := float64(baseStorage.Size()) / float64(totalRawDataSize)
b.ReportMetric(float64(baseStorage.SegmentsTouched()), "segments_touched")
Expand Down
Loading

0 comments on commit ae1d976

Please sign in to comment.