-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the profiler package (#3129)
- Loading branch information
1 parent
0b17fcf
commit 625b6f2
Showing
11 changed files
with
449 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
package execute | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/influxdata/flux" | ||
"github.com/influxdata/flux/memory" | ||
"github.com/influxdata/flux/values" | ||
) | ||
|
||
type Profiler interface { | ||
Name() string | ||
GetResult(q flux.Query, alloc *memory.Allocator) (flux.Table, error) | ||
} | ||
|
||
var AllProfilers map[string]Profiler = make(map[string]Profiler) | ||
|
||
func RegisterProfilers(ps ...Profiler) { | ||
for _, p := range ps { | ||
AllProfilers[p.Name()] = p | ||
} | ||
} | ||
|
||
type FluxStatisticsProfiler struct{} | ||
|
||
func init() { | ||
RegisterProfilers(FluxStatisticsProfiler{}) | ||
} | ||
|
||
func (s FluxStatisticsProfiler) Name() string { | ||
return "FluxStatistics" | ||
} | ||
|
||
func (s FluxStatisticsProfiler) GetResult(q flux.Query, alloc *memory.Allocator) (flux.Table, error) { | ||
groupKey := NewGroupKey( | ||
[]flux.ColMeta{ | ||
{ | ||
Label: "_measurement", | ||
Type: flux.TString, | ||
}, | ||
}, | ||
[]values.Value{ | ||
values.NewString("profiler/FluxStatistics"), | ||
}, | ||
) | ||
b := NewColListTableBuilder(groupKey, alloc) | ||
stats := q.Statistics() | ||
colMeta := []flux.ColMeta{ | ||
{ | ||
Label: "_measurement", | ||
Type: flux.TString, | ||
}, | ||
{ | ||
Label: "TotalDuration", | ||
Type: flux.TInt, | ||
}, | ||
{ | ||
Label: "CompileDuration", | ||
Type: flux.TInt, | ||
}, | ||
{ | ||
Label: "QueueDuration", | ||
Type: flux.TInt, | ||
}, | ||
{ | ||
Label: "PlanDuration", | ||
Type: flux.TInt, | ||
}, | ||
{ | ||
Label: "RequeueDuration", | ||
Type: flux.TInt, | ||
}, | ||
{ | ||
Label: "ExecuteDuration", | ||
Type: flux.TInt, | ||
}, | ||
{ | ||
Label: "Concurrency", | ||
Type: flux.TInt, | ||
}, | ||
{ | ||
Label: "MaxAllocated", | ||
Type: flux.TInt, | ||
}, | ||
{ | ||
Label: "TotalAllocated", | ||
Type: flux.TInt, | ||
}, | ||
{ | ||
Label: "RuntimeErrors", | ||
Type: flux.TString, | ||
}, | ||
} | ||
colData := []interface{}{ | ||
"profiler/FluxStatistics", | ||
stats.TotalDuration.Nanoseconds(), | ||
stats.CompileDuration.Nanoseconds(), | ||
stats.QueueDuration.Nanoseconds(), | ||
stats.PlanDuration.Nanoseconds(), | ||
stats.RequeueDuration.Nanoseconds(), | ||
stats.ExecuteDuration.Nanoseconds(), | ||
int64(stats.Concurrency), | ||
stats.MaxAllocated, | ||
stats.TotalAllocated, | ||
strings.Join(stats.RuntimeErrors, "\n"), | ||
} | ||
stats.Metadata.Range(func(key string, value interface{}) bool { | ||
var ty flux.ColType | ||
if intValue, ok := value.(int); ok { | ||
ty = flux.TInt | ||
colData = append(colData, int64(intValue)) | ||
} else { | ||
ty = flux.TString | ||
colData = append(colData, fmt.Sprintf("%v", value)) | ||
} | ||
colMeta = append(colMeta, flux.ColMeta{ | ||
Label: key, | ||
Type: ty, | ||
}) | ||
return true | ||
}) | ||
for _, col := range colMeta { | ||
if _, err := b.AddCol(col); err != nil { | ||
return nil, err | ||
} | ||
} | ||
for i := 0; i < len(colData); i++ { | ||
if intValue, ok := colData[i].(int64); ok { | ||
b.AppendInt(i, intValue) | ||
} else { | ||
b.AppendString(i, colData[i].(string)) | ||
} | ||
} | ||
tbl, err := b.Table() | ||
if err != nil { | ||
return nil, err | ||
} | ||
return tbl, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package execute_test | ||
|
||
import ( | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/influxdata/flux" | ||
"github.com/influxdata/flux/csv" | ||
"github.com/influxdata/flux/execute" | ||
"github.com/influxdata/flux/execute/executetest" | ||
"github.com/influxdata/flux/execute/table" | ||
"github.com/influxdata/flux/memory" | ||
"github.com/influxdata/flux/metadata" | ||
"github.com/influxdata/flux/mock" | ||
) | ||
|
||
func TestFluxStatisticsProfiler_GetResult(t *testing.T) { | ||
p := &execute.FluxStatisticsProfiler{} | ||
q := &mock.Query{} | ||
q.SetStatistics(flux.Statistics{ | ||
TotalDuration: 1, | ||
CompileDuration: 2, | ||
QueueDuration: 3, | ||
PlanDuration: 4, | ||
RequeueDuration: 5, | ||
ExecuteDuration: 6, | ||
Concurrency: 7, | ||
MaxAllocated: 8, | ||
TotalAllocated: 9, | ||
RuntimeErrors: []string{"1", "2"}, | ||
Metadata: metadata.Metadata{ | ||
"influxdb/scanned-bytes": []interface{}{10}, | ||
"influxdb/scanned-values": []interface{}{11}, | ||
"flux/query-plan": []interface{}{"query plan"}, | ||
}, | ||
}) | ||
wantStr := ` | ||
#datatype,string,long,string,long,long,long,long,long,long,long,long,long,string,string,long,long | ||
#group,false,false,true,false,false,false,false,false,false,false,false,false,false,false,false,false | ||
#default,_profiler,,,,,,,,,,,,,,, | ||
,result,table,_measurement,TotalDuration,CompileDuration,QueueDuration,PlanDuration,RequeueDuration,ExecuteDuration,Concurrency,MaxAllocated,TotalAllocated,RuntimeErrors,flux/query-plan,influxdb/scanned-bytes,influxdb/scanned-values | ||
,,0,profiler/FluxStatistics,1,2,3,4,5,6,7,8,9,"1 | ||
2","query plan",10,11 | ||
` | ||
q.Done() | ||
tbl, err := p.GetResult(q, &memory.Allocator{}) | ||
if err != nil { | ||
t.Error(err) | ||
} | ||
result := table.NewProfilerResult(tbl) | ||
got := flux.NewSliceResultIterator([]flux.Result{&result}) | ||
dec := csv.NewMultiResultDecoder(csv.ResultDecoderConfig{}) | ||
want, e := dec.Decode(ioutil.NopCloser(strings.NewReader(wantStr))) | ||
if e != nil { | ||
t.Error(err) | ||
} | ||
defer want.Release() | ||
|
||
if err := executetest.EqualResultIterators(want, got); err != nil { | ||
t.Fatal(err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package table | ||
|
||
import ( | ||
"github.com/influxdata/flux" | ||
) | ||
|
||
type ProfilerResult struct { | ||
tables Iterator | ||
} | ||
|
||
func NewProfilerResult(tables ...flux.Table) ProfilerResult { | ||
return ProfilerResult{tables} | ||
} | ||
|
||
func (r *ProfilerResult) Name() string { | ||
return "_profiler" | ||
} | ||
|
||
func (r *ProfilerResult) Tables() flux.TableIterator { | ||
return r.tables | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.