github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/prolly/tree/blob_bench_test.go (about)

     1  // Copyright 2022 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package tree
    16  
    17  import (
    18  	"bytes"
    19  	"context"
    20  	"fmt"
    21  	"math"
    22  	"testing"
    23  
    24  	"github.com/dolthub/dolt/go/store/hash"
    25  )
    26  
    27  var result hash.Hash
    28  
    29  func BenchmarkBlobBuilder(b *testing.B) {
    30  	var r hash.Hash
    31  	var err error
    32  	dataSizes := []int{1e3, 1e4, 1e5, 1e6}
    33  	for _, d := range dataSizes {
    34  		b.Run(fmt.Sprintf("datasize: %.0f", math.Log10(float64(d))), func(b *testing.B) {
    35  			ns := NewTestNodeStore()
    36  			bb := mustNewBlobBuilder(DefaultFixedChunkLength)
    37  			bb.SetNodeStore(ns)
    38  			buf := make([]byte, d)
    39  			for i := 0; i < d; i++ {
    40  				buf[i] = uint8(i)
    41  			}
    42  
    43  			b.ResetTimer()
    44  			for n := 0; n < b.N; n++ {
    45  				// always record the result to prevent
    46  				// the compiler eliminating the function call.
    47  				bb.Init(d)
    48  				_, r, err = bb.Chunk(context.Background(), bytes.NewReader(buf))
    49  				if err != nil {
    50  					b.Fatal(err)
    51  				}
    52  				bb.Reset()
    53  			}
    54  			// always store the result to a package level variable
    55  			// so the compiler cannot eliminate the Benchmark itself.
    56  			result = r
    57  		})
    58  	}
    59  }