github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/table/scanner/perfomance_test.go (about) 1 package scanner 2 3 import ( 4 "testing" 5 "time" 6 7 "github.com/ydb-platform/ydb-go-sdk/v3/table/result/named" 8 ) 9 10 var testSize = 10000 11 12 func BenchmarkTestScanWithColumns(b *testing.B) { 13 b.ReportAllocs() 14 res := PrepareScannerPerformanceTest(b.N) 15 var ( 16 id uint64 // for requied scan 17 title *string // for optional scan 18 date *time.Time // for optional scan with default type value 19 ) 20 res.setColumnIndexes([]string{"series_id", "title", "release_date"}) 21 b.ResetTimer() 22 for i := 0; i < b.N; i++ { 23 for res.NextRow() { 24 if err := res.Scan(&id, &title, &date); err != nil { 25 b.Fatal(err) 26 } 27 } 28 } 29 } 30 31 func BenchmarkTestScan(b *testing.B) { 32 b.ReportAllocs() 33 res := PrepareScannerPerformanceTest(b.N) 34 var ( 35 id uint64 // for requied scan 36 title *string // for optional scan 37 date *time.Time // for optional scan with default type value 38 ) 39 b.ResetTimer() 40 for i := 0; i < b.N; i++ { 41 if res.NextRow() { 42 if err := res.Scan(&id, &title, &date); err != nil { 43 b.Fatal(err) 44 } 45 } 46 } 47 } 48 49 func BenchmarkTestScanNamed(b *testing.B) { 50 b.ReportAllocs() 51 res := PrepareScannerPerformanceTest(b.N) 52 var ( 53 id uint64 // for requied scan 54 title *string // for optional scan 55 date time.Time // for optional scan with default type value 56 ) 57 b.ResetTimer() 58 for i := 0; i < b.N; i++ { 59 for res.NextRow() { 60 if err := res.ScanNamed( 61 named.Required("series_id", &id), 62 named.Optional("title", &title), 63 named.OptionalWithDefault("release_date", &date), 64 ); err != nil { 65 b.Fatal(err) 66 } 67 } 68 } 69 } 70 71 func TestOverallApproaches(t *testing.T) { 72 for k, f := range map[string]func(b *testing.B){ 73 "BenchmarkTestScan": BenchmarkTestScan, 74 "BenchmarkTestScanWithColumns": BenchmarkTestScanWithColumns, 75 "BenchmarkTestScanNamed": BenchmarkTestScanNamed, 76 } { 77 r := testing.Benchmark(f) 78 t.Log(k, r.String(), r.MemString()) 79 } 80 } 81 82 func TestOverallSliceApproaches(t *testing.T) { 83 ints := []int{2, 5, 10, 20, 50, 100} 84 for _, testSize = range ints { 85 t.Logf("Slice size: %d", testSize) 86 for _, test := range []struct { 87 name string 88 f func(b *testing.B) 89 }{ 90 { 91 "BenchmarkTestDoubleIndex", 92 BenchmarkTestDoubleIndex, 93 }, { 94 "BenchmarkTestTempValue", 95 BenchmarkTestTempValue, 96 }, 97 } { 98 r := testing.Benchmark(test.f) 99 t.Log(test.name, r.String()) 100 } 101 } 102 } 103 104 func BenchmarkTestSliceReduce(b *testing.B) { 105 slice := make([]*column, testSize) 106 for j := 0; j < testSize; j++ { 107 slice[j] = &column{} 108 } 109 b.ResetTimer() 110 var row column 111 for i := 0; i < b.N; i++ { 112 c := slice 113 for j := 0; j < testSize; j++ { 114 row = *c[0] 115 slice = c[1:] 116 } 117 } 118 _ = row 119 } 120 121 func BenchmarkTestSliceIncrement(b *testing.B) { 122 slice := make([]*column, testSize) 123 for j := 0; j < testSize; j++ { 124 slice[j] = &column{} 125 } 126 var cnt int 127 var row column 128 b.ResetTimer() 129 for i := 0; i < b.N; i++ { 130 cnt = 0 131 for i := 0; i < testSize; i++ { 132 row = *slice[cnt] 133 cnt++ 134 } 135 } 136 _ = row 137 } 138 139 func BenchmarkTestTempValue(b *testing.B) { 140 slice := make([]*column, testSize) 141 for j := 0; j < testSize; j++ { 142 slice[j] = &column{} 143 } 144 b.ResetTimer() 145 for i := 0; i < b.N; i++ { 146 for i := 0; i < testSize; i++ { 147 col := slice[i] 148 col.name = "test1" 149 col.typeID = 1 150 } 151 } 152 } 153 154 func BenchmarkTestDoubleIndex(b *testing.B) { 155 slice := make([]*column, testSize) 156 for j := 0; j < testSize; j++ { 157 slice[j] = &column{} 158 } 159 b.ResetTimer() 160 for i := 0; i < b.N; i++ { 161 for i := 0; i < testSize; i++ { 162 slice[i].name = "test2" 163 slice[i].typeID = 1 164 } 165 } 166 }