Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return valid JSON format for profiling data #236

Merged
merged 1 commit into from
Nov 18, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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