Skip to content

Commit

Permalink
Merge branch 'master' into discard-too-long-plan
Browse files Browse the repository at this point in the history
  • Loading branch information
crazycs520 authored Jul 1, 2021
2 parents 116899c + 28202b3 commit 57fb71d
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -262,3 +262,10 @@ else
$(GOTEST) -ldflags '$(TEST_LDFLAGS)' -cover github.com/pingcap/tidb/$(pkg) -check.p true -check.timeout 4s || { $(FAILPOINT_DISABLE); exit 1; }
endif
@$(FAILPOINT_DISABLE)

# Collect the daily benchmark data.
# 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)
109 changes: 109 additions & 0 deletions session/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,13 @@ package session

import (
"context"
"encoding/json"
"flag"
"fmt"
"math/rand"
"os"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
Expand Down Expand Up @@ -1584,3 +1589,107 @@ func BenchmarkHashPartitionPruningMultiSelect(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){
BenchmarkBasic,
BenchmarkTableScan,
BenchmarkTableLookup,
BenchmarkExplainTableLookup,
BenchmarkStringIndexScan,
BenchmarkExplainStringIndexScan,
BenchmarkStringIndexLookup,
BenchmarkIntegerIndexScan,
BenchmarkIntegerIndexLookup,
BenchmarkDecimalIndexScan,
BenchmarkDecimalIndexLookup,
BenchmarkInsertWithIndex,
BenchmarkInsertNoIndex,
BenchmarkSort,
BenchmarkJoin,
BenchmarkJoinLimit,
BenchmarkPartitionPruning,
BenchmarkRangeColumnPartitionPruning,
BenchmarkHashPartitionPruningPointSelect,
BenchmarkHashPartitionPruningMultiSelect,
}

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)
}
}

0 comments on commit 57fb71d

Please sign in to comment.