Skip to content

Commit

Permalink
Return valid JSON format for profiling data (#236)
Browse files Browse the repository at this point in the history
  • Loading branch information
yixinglu authored Nov 18, 2022
1 parent 91bc339 commit 2ed450c
Showing 1 changed file with 28 additions and 15 deletions.
43 changes: 28 additions & 15 deletions result_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"regexp"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -1270,24 +1271,36 @@ func (res ResultSet) MakePlanByRow() [][]interface{} {
}

if planNodeDesc.IsSetProfiles() {
var strArr []string
var profileArr []string
for i, profile := range planNodeDesc.GetProfiles() {
otherStats := profile.GetOtherStats()
if otherStats != nil {
strArr = append(strArr, "{")
}
s := fmt.Sprintf("ver: %d, rows: %d, execTime: %dus, totalTime: %dus",
i, profile.GetRows(), profile.GetExecDurationInUs(), profile.GetTotalDurationInUs())
strArr = append(strArr, s)

for k, v := range otherStats {
strArr = append(strArr, fmt.Sprintf("%s: %s", k, v))
}
if otherStats != nil {
strArr = append(strArr, "}")
var statArr []string
statArr = append(statArr, fmt.Sprintf("\"version\":%d", i))
statArr = append(statArr, fmt.Sprintf("\"rows\":%d", profile.GetRows()))
statArr = append(statArr, fmt.Sprintf("\"execTime\":\"%d(us)\"", profile.GetExecDurationInUs()))
statArr = append(statArr, fmt.Sprintf("\"totalTime\":\"%d(us)\"", profile.GetTotalDurationInUs()))
for k, v := range profile.GetOtherStats() {
s := string(v)
if matched, err := regexp.Match(`^[^{(\[]\w+`, v); err == nil && matched {
if !strings.HasPrefix(s, "\"") {
s = fmt.Sprintf("\"%s", s)
}
if !strings.HasSuffix(s, "\"") {
s = fmt.Sprintf("%s\"", s)
}
}
statArr = append(statArr, fmt.Sprintf("\"%s\": %s", k, s))
}
sort.Strings(statArr)
statStr := fmt.Sprintf("{%s}", strings.Join(statArr, ",\n"))
profileArr = append(profileArr, statStr)
}
allProfiles := strings.Join(profileArr, ",\n")
if len(profileArr) > 1 {
allProfiles = fmt.Sprintf("[%s]", allProfiles)
}
row = append(row, strings.Join(strArr, "\n"))
var buffer bytes.Buffer
json.Indent(&buffer, []byte(allProfiles), "", " ")
row = append(row, string(buffer.Bytes()))
} else {
row = append(row, "")
}
Expand Down

0 comments on commit 2ed450c

Please sign in to comment.