Skip to content

Commit

Permalink
stmtsummary: tiny optimize by avoiding unnecessary calculations (#58562)
Browse files Browse the repository at this point in the history
ref #56649
  • Loading branch information
crazycs520 authored Dec 27, 2024
1 parent 9c2ce65 commit fc3c894
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pkg/executor/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,7 @@ func (a *ExecStmt) SummaryStmt(succ bool) {
}

execDetail := stmtCtx.GetExecDetails()
copTaskInfo := stmtCtx.CopTasksDetails()
copTaskInfo := stmtCtx.CopTasksSummary()
memMax := sessVars.MemTracker.MaxConsumed()
diskMax := sessVars.DiskTracker.MaxConsumed()
var stmtDetail execdetails.StmtExecDetails
Expand Down
30 changes: 30 additions & 0 deletions pkg/util/execdetails/execdetails.go
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,25 @@ func (s *SyncExecDetails) CopTasksDetails() *CopTasksDetails {
return d
}

// CopTasksSummary returns some summary information of cop-tasks for statement summary.
func (s *SyncExecDetails) CopTasksSummary() *CopTasksSummary {
s.mu.Lock()
defer s.mu.Unlock()
n := s.detailsSummary.NumCopTasks
if n == 0 {
return nil
}
return &CopTasksSummary{
NumCopTasks: n,
MaxProcessAddress: s.detailsSummary.ProcessTimePercentile.GetMax().Addr,
MaxProcessTime: s.detailsSummary.ProcessTimePercentile.GetMax().D,
TotProcessTime: s.execDetails.TimeDetail.ProcessTime,
MaxWaitAddress: s.detailsSummary.WaitTimePercentile.GetMax().Addr,
MaxWaitTime: s.detailsSummary.WaitTimePercentile.GetMax().D,
TotWaitTime: s.execDetails.TimeDetail.WaitTime,
}
}

// CopTasksDetails collects some useful information of cop-tasks during execution.
type CopTasksDetails struct {
NumCopTasks int
Expand All @@ -535,6 +554,17 @@ type CopTasksDetails struct {
TotBackoffTimes map[string]int
}

// CopTasksSummary collects some summary information of cop-tasks for statement summary.
type CopTasksSummary struct {
NumCopTasks int
MaxProcessAddress string
MaxProcessTime time.Duration
TotProcessTime time.Duration
MaxWaitAddress string
MaxWaitTime time.Duration
TotWaitTime time.Duration
}

// ToZapFields wraps the CopTasksDetails as zap.Fileds.
func (d *CopTasksDetails) ToZapFields() (fields []zap.Field) {
if d == nil || d.NumCopTasks == 0 {
Expand Down
2 changes: 1 addition & 1 deletion pkg/util/stmtsummary/statement_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ type StmtExecInfo struct {
ParseLatency time.Duration
CompileLatency time.Duration
StmtCtx *stmtctx.StatementContext
CopTasks *execdetails.CopTasksDetails
CopTasks *execdetails.CopTasksSummary
ExecDetail execdetails.ExecDetails
MemMax int64
DiskMax int64
Expand Down
18 changes: 3 additions & 15 deletions pkg/util/stmtsummary/statement_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,11 @@ func TestAddStatement(t *testing.T) {
TotalLatency: 20000,
ParseLatency: 200,
CompileLatency: 2000,
CopTasks: &execdetails.CopTasksDetails{
CopTasks: &execdetails.CopTasksSummary{
NumCopTasks: 20,
AvgProcessTime: 2000,
P90ProcessTime: 20000,
MaxProcessAddress: "200",
MaxProcessTime: 25000,
TotProcessTime: 40000,
AvgWaitTime: 200,
P90WaitTime: 2000,
MaxWaitAddress: "201",
MaxWaitTime: 2500,
TotWaitTime: 40000,
Expand Down Expand Up @@ -332,15 +328,11 @@ func TestAddStatement(t *testing.T) {
TotalLatency: 1000,
ParseLatency: 50,
CompileLatency: 500,
CopTasks: &execdetails.CopTasksDetails{
CopTasks: &execdetails.CopTasksSummary{
NumCopTasks: 2,
AvgProcessTime: 100,
P90ProcessTime: 300,
MaxProcessAddress: "300",
MaxProcessTime: 350,
TotProcessTime: 200,
AvgWaitTime: 20,
P90WaitTime: 200,
MaxWaitAddress: "301",
MaxWaitTime: 250,
TotWaitTime: 40,
Expand Down Expand Up @@ -639,15 +631,11 @@ func generateAnyExecInfo() *StmtExecInfo {
TotalLatency: 10000,
ParseLatency: 100,
CompileLatency: 1000,
CopTasks: &execdetails.CopTasksDetails{
CopTasks: &execdetails.CopTasksSummary{
NumCopTasks: 10,
AvgProcessTime: 1000,
P90ProcessTime: 10000,
MaxProcessAddress: "127",
MaxProcessTime: 15000,
TotProcessTime: 10000,
AvgWaitTime: 100,
P90WaitTime: 1000,
MaxWaitAddress: "128",
MaxWaitTime: 1500,
TotWaitTime: 1000,
Expand Down
6 changes: 1 addition & 5 deletions pkg/util/stmtsummary/v2/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,14 +623,10 @@ func GenerateStmtExecInfo4Test(digest string) *stmtsummary.StmtExecInfo {
TotalLatency: 10000,
ParseLatency: 100,
CompileLatency: 1000,
CopTasks: &execdetails.CopTasksDetails{
CopTasks: &execdetails.CopTasksSummary{
NumCopTasks: 10,
AvgProcessTime: 1000,
P90ProcessTime: 10000,
MaxProcessAddress: "127",
MaxProcessTime: 15000,
AvgWaitTime: 100,
P90WaitTime: 1000,
MaxWaitAddress: "128",
MaxWaitTime: 1500,
},
Expand Down

0 comments on commit fc3c894

Please sign in to comment.