github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/bench_test.go (about)

     1  // Copyright 2019 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  package workload_test
    12  
    13  import (
    14  	"fmt"
    15  	"testing"
    16  
    17  	"github.com/cockroachdb/cockroach/pkg/col/coldata"
    18  	"github.com/cockroachdb/cockroach/pkg/sql/types"
    19  	"github.com/cockroachdb/cockroach/pkg/util/bufalloc"
    20  	"github.com/cockroachdb/cockroach/pkg/workload"
    21  	"github.com/cockroachdb/cockroach/pkg/workload/bank"
    22  	"github.com/cockroachdb/cockroach/pkg/workload/tpcc"
    23  	"github.com/cockroachdb/cockroach/pkg/workload/tpch"
    24  )
    25  
    26  func columnByteSize(col coldata.Vec) int64 {
    27  	switch t := col.Type(); col.CanonicalTypeFamily() {
    28  	case types.IntFamily:
    29  		switch t.Width() {
    30  		case 0, 64:
    31  			return int64(len(col.Int64()) * 8)
    32  		case 16:
    33  			return int64(len(col.Int16()) * 2)
    34  		default:
    35  			panic(fmt.Sprintf("unexpected int width: %d", t.Width()))
    36  		}
    37  	case types.FloatFamily:
    38  		return int64(len(col.Float64()) * 8)
    39  	case types.BytesFamily:
    40  		// We subtract the overhead to be in line with Int64 and Float64 cases.
    41  		return int64(col.Bytes().Size() - coldata.FlatBytesOverhead)
    42  	default:
    43  		panic(fmt.Sprintf(`unhandled type %s`, t))
    44  	}
    45  }
    46  
    47  func benchmarkInitialData(b *testing.B, gen workload.Generator) {
    48  	tables := gen.Tables()
    49  
    50  	var bytes int64
    51  	b.ResetTimer()
    52  	for i := 0; i < b.N; i++ {
    53  		// Share the Batch and ByteAllocator across tables but not across benchmark
    54  		// iterations.
    55  		cb := coldata.NewMemBatch(nil /* types */, coldata.StandardColumnFactory)
    56  		var a bufalloc.ByteAllocator
    57  		for _, table := range tables {
    58  			for rowIdx := 0; rowIdx < table.InitialRows.NumBatches; rowIdx++ {
    59  				a = a[:0]
    60  				table.InitialRows.FillBatch(rowIdx, cb, &a)
    61  				for _, col := range cb.ColVecs() {
    62  					bytes += columnByteSize(col)
    63  				}
    64  			}
    65  		}
    66  	}
    67  	b.StopTimer()
    68  	b.SetBytes(bytes / int64(b.N))
    69  }
    70  
    71  func BenchmarkInitialData(b *testing.B) {
    72  	b.Run(`tpcc/warehouses=1`, func(b *testing.B) {
    73  		benchmarkInitialData(b, tpcc.FromWarehouses(1))
    74  	})
    75  	b.Run(`bank/rows=1000`, func(b *testing.B) {
    76  		benchmarkInitialData(b, bank.FromRows(1000))
    77  	})
    78  	b.Run(`tpch/scaleFactor=1`, func(b *testing.B) {
    79  		if testing.Short() {
    80  			b.Skip(`tpch loads a lot of data`)
    81  		}
    82  		benchmarkInitialData(b, tpch.FromScaleFactor(1))
    83  	})
    84  }