@@ -4,6 +4,8 @@ package tstate
44
55import (
66 "context"
7+ "crypto/rand"
8+ "fmt"
79 "testing"
810
911 "github.com/ava-labs/avalanchego/database"
@@ -323,3 +325,117 @@ func TestWriteChanges(t *testing.T) {
323325 require .ErrorIs (err , database .ErrNotFound , "Value not removed from db." )
324326 }
325327}
328+
329+ func BenchmarkFetchAndSetScope (b * testing.B ) {
330+ for _ , size := range []int {4 , 8 , 16 , 32 , 64 , 128 } {
331+ b .Run (fmt .Sprintf ("fetch_and_set_scope_%d_keys" , size ), func (b * testing.B ) {
332+ benchmarkFetchAndSetScope (b , size )
333+ })
334+ }
335+ }
336+
337+ func BenchmarkInsert (b * testing.B ) {
338+ for _ , size := range []int {4 , 8 , 16 , 32 , 64 , 128 } {
339+ b .Run (fmt .Sprintf ("insert_%d_keys" , size ), func (b * testing.B ) {
340+ benchmarkInsert (b , size )
341+ })
342+ }
343+ }
344+
345+
346+ func BenchmarkGetValue (b * testing.B ) {
347+ for _ , size := range []int {4 , 8 , 16 , 32 , 64 , 128 } {
348+ b .Run (fmt .Sprintf ("get_%d_keys" , size ), func (b * testing.B ) {
349+ benchmarkGetValue (b , size )
350+ })
351+ }
352+ }
353+
354+ func benchmarkFetchAndSetScope (b * testing.B , size int ) {
355+ require := require .New (b )
356+ ts := New (size )
357+ db := NewTestDB ()
358+ ctx := context .TODO ()
359+
360+ keys , vals := initializeSet (size )
361+ for i , key := range keys {
362+ err := db .Insert (ctx , key , vals [i ])
363+ require .NoError (err , "Error during insert." )
364+ }
365+
366+ b .ResetTimer ()
367+ for i := 0 ; i < b .N ; i ++ {
368+ err := ts .FetchAndSetScope (ctx , keys , db )
369+ require .NoError (err )
370+ }
371+ b .ReportAllocs ()
372+ b .StopTimer ()
373+ }
374+
375+ func benchmarkInsert (b * testing.B , size int ) {
376+ require := require .New (b )
377+ ts := New (size )
378+ ctx := context .TODO ()
379+
380+ keys , vals := initializeSet (size )
381+
382+ storage := map [string ][]byte {}
383+ for i , key := range keys {
384+ storage [string (key )] = vals [i ]
385+ }
386+
387+ ts .SetScope (ctx , keys , storage )
388+
389+ b .ResetTimer ()
390+ for i := 0 ; i < b .N ; i ++ {
391+ for i , key := range keys {
392+ err := ts .Insert (ctx , key , vals [i ])
393+ require .NoError (err , "Error during insert." )
394+ }
395+ }
396+ b .ReportAllocs ()
397+ b .StopTimer ()
398+ }
399+
400+ func benchmarkGetValue (b * testing.B , size int ) {
401+ require := require .New (b )
402+ ts := New (size )
403+ ctx := context .TODO ()
404+
405+ keys , vals := initializeSet (size )
406+
407+ storage := map [string ][]byte {}
408+ for i , key := range keys {
409+ storage [string (key )] = vals [i ]
410+ }
411+
412+ ts .SetScope (ctx , keys , storage )
413+
414+ b .ResetTimer ()
415+ for i := 0 ; i < b .N ; i ++ {
416+ for _ , key := range keys {
417+ _ , err := ts .GetValue (ctx , key )
418+ require .NoError (err , "Error during insert." )
419+ }
420+ }
421+ b .ReportAllocs ()
422+ b .StopTimer ()
423+ }
424+
425+ func initializeSet (size int ) ([][]byte , [][]byte ) {
426+ keys := [][]byte {}
427+ vals := [][]byte {}
428+
429+ for i := 0 ; i <= size ; i ++ {
430+ keys = append (keys , randomBytes (33 ))
431+ vals = append (vals , randomBytes (8 ))
432+ }
433+
434+ return keys , vals
435+ }
436+
437+ func randomBytes (size int ) []byte {
438+ bytes := make ([]byte , size )
439+ rand .Read (bytes )
440+ return bytes
441+ }
0 commit comments