1
0
mirror of https://github.com/MontFerret/ferret.git synced 2025-03-17 21:18:37 +02:00

Updated profiler (#686)

This commit is contained in:
Tim Voronov 2021-10-20 11:13:56 -04:00 committed by GitHub
parent a049f30214
commit 982a7da618
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -57,7 +57,7 @@ func (p *Profiler) StartTimer(label string) {
} }
p.timers[label] = timer p.timers[label] = timer
p.labels = append(p.labels, label) p.addLabel(label)
} }
func (p *Profiler) StopTimer(label string) { func (p *Profiler) StopTimer(label string) {
@ -80,7 +80,7 @@ func (p *Profiler) HeapSnapshot(label string) {
} }
p.heaps[label] = heap p.heaps[label] = heap
p.labels = append(p.labels, label) p.addLabel(label)
} }
func (p *Profiler) Allocations(label string) { func (p *Profiler) Allocations(label string) {
@ -89,7 +89,7 @@ func (p *Profiler) Allocations(label string) {
rt.ReadMemStats(stats) rt.ReadMemStats(stats)
p.allocs[label] = stats p.allocs[label] = stats
p.labels = append(p.labels, label) p.addLabel(label)
} }
func (p *Profiler) StartCPU(label string) { func (p *Profiler) StartCPU(label string) {
@ -100,7 +100,7 @@ func (p *Profiler) StartCPU(label string) {
} }
p.cpus[label] = b p.cpus[label] = b
p.labels = append(p.labels, label) p.addLabel(label)
} }
func (p *Profiler) StopCPU() { func (p *Profiler) StopCPU() {
@ -130,11 +130,11 @@ func (p *Profiler) Print(label string) {
fmt.Fprintln(writer, fmt.Sprintf("Heap Objects: %d", stats.HeapObjects)) fmt.Fprintln(writer, fmt.Sprintf("Heap Objects: %d", stats.HeapObjects))
} }
cpu, found := p.cpus[label] //cpu, found := p.cpus[label]
//
if found { //if found {
fmt.Fprintln(writer, cpu.String()) // fmt.Fprintln(writer, cpu.String())
} //}
if writer.Len() > 0 { if writer.Len() > 0 {
fmt.Println(fmt.Sprintf("%s:", label)) fmt.Println(fmt.Sprintf("%s:", label))
@ -149,6 +149,21 @@ func (p *Profiler) PrintAll() {
} }
} }
func (p *Profiler) addLabel(label string) {
var found bool
for _, l := range p.labels {
if l == label {
found = true
break
}
}
if !found {
p.labels = append(p.labels, label)
}
}
type Params []string type Params []string
func (p *Params) String() string { func (p *Params) String() string {
@ -199,6 +214,12 @@ var (
"compiles a given query, but does not execute", "compiles a given query, but does not execute",
) )
profiler = flag.Bool(
"profiler",
false,
"enables CPU and Memory profiler",
)
logLevel = flag.String( logLevel = flag.String(
"log-level", "log-level",
logging.ErrorLevel.String(), logging.ErrorLevel.String(),
@ -392,14 +413,40 @@ func runQuery(ctx context.Context, engine *ferret.Instance, opts []runtime.Optio
return execQuery(ctx, engine, opts, query) return execQuery(ctx, engine, opts, query)
} }
return analyzeQuery(ctx, engine, opts, query) return analyzeQuery(engine, query)
} }
func execQuery(ctx context.Context, engine *ferret.Instance, opts []runtime.Option, query string) error { func execQuery(ctx context.Context, engine *ferret.Instance, opts []runtime.Option, query string) error {
beforeExec := "Before Execution"
exec := "Execution"
afterExec := "After Execution"
prof := NewProfiler()
if *profiler {
prof.Allocations(beforeExec)
prof.StartCPU(exec)
prof.StartTimer(exec)
}
out, err := engine.Exec(ctx, query, opts...) out, err := engine.Exec(ctx, query, opts...)
if *profiler {
prof.Allocations(afterExec)
prof.StopTimer(exec)
prof.StopCPU()
prof.PrintAll()
if out != nil {
fmt.Println(fmt.Sprintf("Output size: %s", byteCountDecimal(uint64(len(out)))))
fmt.Println("")
}
}
if err != nil { if err != nil {
return err fmt.Println(err)
os.Exit(1)
} }
fmt.Println(string(out)) fmt.Println(string(out))
@ -407,30 +454,27 @@ func execQuery(ctx context.Context, engine *ferret.Instance, opts []runtime.Opti
return nil return nil
} }
func analyzeQuery(ctx context.Context, engine *ferret.Instance, opts []runtime.Option, query string) error { func analyzeQuery(engine *ferret.Instance, query string) error {
beforeCompilation := "Before Compilation"
compilation := "Compilation" compilation := "Compilation"
beforeCompilation := "Before compilation" afterCompilation := "After Compilation"
afterCompilation := "After compilation"
prof := NewProfiler() prof := NewProfiler()
prof.Allocations(beforeCompilation) fullProf := *profiler
if fullProf {
prof.Allocations(beforeCompilation)
}
prof.StartTimer(compilation) prof.StartTimer(compilation)
program := engine.MustCompile(query)
engine.MustCompile(query)
prof.StopTimer(compilation) prof.StopTimer(compilation)
prof.Allocations(afterCompilation)
exec := "Execution" if fullProf {
beforeExec := "Before execution" prof.Allocations(afterCompilation)
afterExec := "After execution" }
prof.Allocations(beforeExec)
prof.StartTimer(exec)
engine.MustRun(ctx, program, opts...)
prof.StopTimer(exec)
prof.Allocations(afterExec)
prof.PrintAll() prof.PrintAll()