Skip to content

Commit

Permalink
*: add more daily benchmark test cases (pingcap#26773)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Sep 10, 2021
1 parent 8e25f8e commit 1cd95a8
Show file tree
Hide file tree
Showing 11 changed files with 294 additions and 84 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ vendor
/_tools/
.DS_Store
.vscode
bench_daily.json
12 changes: 10 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,16 @@ endif
# Usage:
# make bench-daily TO=/path/to/file.json
bench-daily:
cd ./session && \
go test -run TestBenchDaily --date `git log -n1 --date=unix --pretty=format:%cd` --commit `git log -n1 --pretty=format:%h` --outfile $(TO)
go test github.com/pingcap/tidb/session -run TestBenchDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/executor -run TestBenchDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/tablecodec -run TestBenchDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/expression -run TestBenchDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/util/rowcodec -run TestBenchDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/util/codec -run TestBenchDaily --outfile bench_daily.json
go test github.com/pingcap/tidb/util/benchdaily -run TestBenchDaily \
-date `git log -n1 --date=unix --pretty=format:%cd` \
-commit `git log -n1 --pretty=format:%h` \
-outfile $(TO)

build_tools: build_br build_lightning build_lightning-ctl

Expand Down
7 changes: 7 additions & 0 deletions executor/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import (
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/benchdaily"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/disk"
"github.com/pingcap/tidb/util/memory"
Expand Down Expand Up @@ -2100,3 +2101,9 @@ func BenchmarkPipelinedRowNumberWindowFunctionExecution(b *testing.B) {
b.ReportAllocs()

}

func TestBenchDaily(t *testing.T) {
benchdaily.Run(
BenchmarkReadLastLinesOfHugeLine,
)
}
10 changes: 10 additions & 0 deletions expression/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import (
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
"github.com/pingcap/tidb/util/benchdaily"
"github.com/pingcap/tidb/util/chunk"
"github.com/pingcap/tidb/util/math"
"github.com/pingcap/tidb/util/mock"
Expand Down Expand Up @@ -2022,3 +2023,12 @@ func (s *testVectorizeSuite2) TestVectorizedFilterConsiderNull(c *C) {
}
ctx.GetSessionVars().EnableVectorizedExpression = dafaultEnableVectorizedExpressionVar
}

func TestBenchDaily(t *testing.T) {
benchdaily.Run(
BenchmarkCastIntAsIntRow,
BenchmarkCastIntAsIntVec,
BenchmarkVectorizedExecute,
BenchmarkScalarFunctionClone,
)
}
85 changes: 3 additions & 82 deletions session/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,8 @@ package session

import (
"context"
"encoding/json"
"flag"
"fmt"
"math/rand"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
Expand All @@ -36,6 +31,7 @@ import (
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/benchdaily"
"github.com/pingcap/tidb/util/logutil"
"github.com/pingcap/tidb/util/sqlexec"
"go.uber.org/zap"
Expand Down Expand Up @@ -1808,59 +1804,12 @@ func BenchmarkCompileExecutePreparedStmt(b *testing.B) {
b.StopTimer()
}

type BenchOutput struct {
Date string
Commit string
Result []BenchResult
}

type BenchResult struct {
Name string
NsPerOp int64
AllocsPerOp int64
BytesPerOp int64
}

func benchmarkResultToJSON(name string, r testing.BenchmarkResult) BenchResult {
return BenchResult{
Name: name,
NsPerOp: r.NsPerOp(),
AllocsPerOp: r.AllocsPerOp(),
BytesPerOp: r.AllocedBytesPerOp(),
}
}

func callerName(f func(b *testing.B)) string {
fullName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
idx := strings.LastIndexByte(fullName, '.')
if idx > 0 && idx+1 < len(fullName) {
return fullName[idx+1:]
}
return fullName
}

var (
date = flag.String("date", "", " commit date")
commitHash = flag.String("commit", "unknown", "brief git commit hash")
outfile = flag.String("outfile", "bench-daily.json", "specify the output file")
)

// TestBenchDaily collects the daily benchmark test result and generates a json output file.
// The format of the json output is described by the BenchOutput.
// Used by this command in the Makefile
// make bench-daily TO=xxx.json
func TestBenchDaily(t *testing.T) {
if !flag.Parsed() {
flag.Parse()
}

if *date == "" {
// Don't run unless 'date' is specified.
// Avoiding slow down the CI.
return
}

tests := []func(b *testing.B){
benchdaily.Run(
BenchmarkPreparedPointGet,
BenchmarkPointGet,
BenchmarkBatchPointGet,
Expand All @@ -1886,33 +1835,5 @@ func TestBenchDaily(t *testing.T) {
BenchmarkHashPartitionPruningMultiSelect,
BenchmarkInsertIntoSelect,
BenchmarkCompileExecutePreparedStmt,
}

res := make([]BenchResult, 0, len(tests))
for _, t := range tests {
name := callerName(t)
r1 := testing.Benchmark(t)
r2 := benchmarkResultToJSON(name, r1)
res = append(res, r2)
}

if *outfile == "" {
*outfile = fmt.Sprintf("%s_%s.json", *date, *commitHash)
}
out, err := os.Create(*outfile)
if err != nil {
t.Fatal(err)
}
defer out.Close()

output := BenchOutput{
Date: *date,
Commit: *commitHash,
Result: res,
}
enc := json.NewEncoder(out)
err = enc.Encode(output)
if err != nil {
t.Fatal(err)
}
)
}
13 changes: 13 additions & 0 deletions tablecodec/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/util/benchdaily"
)

func BenchmarkEncodeRowKeyWithHandle(b *testing.B) {
Expand Down Expand Up @@ -53,3 +54,15 @@ func BenchmarkDecodeRowKey(b *testing.B) {
}
}
}

func TestBenchDaily(t *testing.T) {
benchdaily.Run(
BenchmarkEncodeRowKeyWithHandle,
BenchmarkEncodeEndKey,
BenchmarkEncodeRowKeyWithPrefixNex,
BenchmarkDecodeRowKey,
BenchmarkHasTablePrefix,
BenchmarkHasTablePrefixBuiltin,
BenchmarkEncodeValue,
)
}
123 changes: 123 additions & 0 deletions util/benchdaily/bench_daily.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// Copyright 2021 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package benchdaily

import (
"encoding/json"
"flag"
"log"
"os"
"reflect"
"runtime"
"strings"
"testing"
)

// BenchOutput is the json format for the final output file.
type BenchOutput struct {
Date string
Commit string
Result []BenchResult
}

// BenchResult is one benchmark result.
type BenchResult struct {
Name string
NsPerOp int64
AllocsPerOp int64
BytesPerOp int64
}

func benchmarkResultToJSON(name string, r testing.BenchmarkResult) BenchResult {
return BenchResult{
Name: name,
NsPerOp: r.NsPerOp(),
AllocsPerOp: r.AllocsPerOp(),
BytesPerOp: r.AllocedBytesPerOp(),
}
}

func callerName(f func(b *testing.B)) string {
fullName := runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
idx := strings.LastIndexByte(fullName, '.')
if idx > 0 && idx+1 < len(fullName) {
return fullName[idx+1:]
}
return fullName
}

var outfile = flag.String("outfile", "", "specify the output file")

// Run runs some benchmark tests, write the result to a JSON file.
func Run(tests ...func(b *testing.B)) {
if !flag.Parsed() {
flag.Parse()
}

// Avoiding slow down the CI.
if *outfile == "" {
return
}

res := make([]BenchResult, 0, len(tests))
for _, t := range tests {
name := callerName(t)
r1 := testing.Benchmark(t)
r2 := benchmarkResultToJSON(name, r1)
res = append(res, r2)
}

writeBenchResultToFile(res, *outfile)
}

func readBenchResultFromFile(file string) []BenchResult {
f, err := os.Open(file)
if err != nil {
log.Panic(err)
}
defer func() {
err := f.Close()
if err != nil {
log.Fatal(err)
}
}()

res := make([]BenchResult, 0, 100)
dec := json.NewDecoder(f)
err = dec.Decode(&res)
if err != nil {
log.Panic(err)
}
return res
}

func writeBenchResultToFile(res []BenchResult, file string) {
out, err := os.Create(file)
if err != nil {
log.Fatal(err)
}
defer func() {
err := out.Close()
if err != nil {
log.Fatal(err)
}
}()

enc := json.NewEncoder(out)
err = enc.Encode(res)
if err != nil {
log.Fatal(err)
}
}
Loading

0 comments on commit 1cd95a8

Please sign in to comment.