feat: added optional profiling
This commit is contained in:
2
Makefile
2
Makefile
@@ -5,7 +5,7 @@ it:
|
|||||||
@ chmod +x ${BINARY_NAME}
|
@ chmod +x ${BINARY_NAME}
|
||||||
|
|
||||||
simple: it
|
simple: it
|
||||||
@ ./lambda.exe - < ./samples/simple.txt > program.out
|
@ ./lambda.exe -p profile/simple.prof - < ./samples/simple.txt > program.out
|
||||||
|
|
||||||
thunk: it
|
thunk: it
|
||||||
@ ./lambda.exe - < ./samples/thunk.txt > program.out
|
@ ./lambda.exe - < ./samples/thunk.txt > program.out
|
||||||
|
|||||||
@@ -3,11 +3,10 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime/pprof"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"git.maximhutz.com/max/lambda/internal/cli"
|
"git.maximhutz.com/max/lambda/internal/cli"
|
||||||
"git.maximhutz.com/max/lambda/internal/config"
|
"git.maximhutz.com/max/lambda/internal/config"
|
||||||
|
"git.maximhutz.com/max/lambda/internal/executer"
|
||||||
"git.maximhutz.com/max/lambda/pkg/convert"
|
"git.maximhutz.com/max/lambda/pkg/convert"
|
||||||
"git.maximhutz.com/max/lambda/pkg/lambda"
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||||
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
"git.maximhutz.com/max/lambda/pkg/saccharine"
|
||||||
@@ -15,14 +14,6 @@ import (
|
|||||||
|
|
||||||
// Run main application.
|
// Run main application.
|
||||||
func main() {
|
func main() {
|
||||||
f, err := os.Create("profile/cpu.prof")
|
|
||||||
cli.HandleError(err)
|
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
err = pprof.StartCPUProfile(f)
|
|
||||||
cli.HandleError(err)
|
|
||||||
defer pprof.StopCPUProfile()
|
|
||||||
|
|
||||||
options, err := config.FromArgs()
|
options, err := config.FromArgs()
|
||||||
cli.HandleError(err)
|
cli.HandleError(err)
|
||||||
|
|
||||||
@@ -50,29 +41,10 @@ func main() {
|
|||||||
logger.Info("compiled lambda expression", "tree", lambda.Stringify(compiled))
|
logger.Info("compiled lambda expression", "tree", lambda.Stringify(compiled))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce expression.
|
executor := executer.New(options)
|
||||||
start := time.Now()
|
results, err := executor.Run(&compiled)
|
||||||
|
cli.HandleError(err)
|
||||||
if options.Explanation {
|
|
||||||
fmt.Println(lambda.Stringify(compiled))
|
|
||||||
}
|
|
||||||
|
|
||||||
steps := 0
|
|
||||||
|
|
||||||
for lambda.ReduceOnce(&compiled) {
|
|
||||||
if options.Verbose {
|
|
||||||
logger.Info("reduction", "tree", lambda.Stringify(compiled))
|
|
||||||
}
|
|
||||||
if options.Explanation {
|
|
||||||
fmt.Println(" =", lambda.Stringify(compiled))
|
|
||||||
}
|
|
||||||
steps++
|
|
||||||
}
|
|
||||||
|
|
||||||
elapsed := time.Since(start).Milliseconds()
|
|
||||||
|
|
||||||
fmt.Println(lambda.Stringify(compiled))
|
fmt.Println(lambda.Stringify(compiled))
|
||||||
fmt.Fprintln(os.Stderr, "Time Spent:", elapsed, "ms")
|
fmt.Fprint(os.Stderr, results.String())
|
||||||
fmt.Fprintln(os.Stderr, "Steps:", steps)
|
|
||||||
fmt.Fprintln(os.Stderr, "Speed:", float32(steps)/(float32(elapsed)/1000), "ops")
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,37 +0,0 @@
|
|||||||
package cli
|
|
||||||
|
|
||||||
import (
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Arguments given to program.
|
|
||||||
type Options struct {
|
|
||||||
// The source code given to the program.
|
|
||||||
Input string
|
|
||||||
// Whether or not to print debug logs.
|
|
||||||
Verbose bool
|
|
||||||
// Whether or not to print an explanation of the reduction.
|
|
||||||
Explanation bool
|
|
||||||
}
|
|
||||||
|
|
||||||
// Extract the program configuration from the command-line arguments.
|
|
||||||
func ParseOptions() (*Options, error) {
|
|
||||||
// Parse flags.
|
|
||||||
verbose := flag.Bool("v", false, "Verbosity. If set, the program will print logs.")
|
|
||||||
explanation := flag.Bool("x", false, "Explanation. Whether or not to show all reduction steps.")
|
|
||||||
flag.Parse()
|
|
||||||
|
|
||||||
// Parse non-flag arguments.
|
|
||||||
if flag.NArg() == 0 {
|
|
||||||
return nil, fmt.Errorf("no input given")
|
|
||||||
} else if flag.NArg() > 1 {
|
|
||||||
return nil, fmt.Errorf("more than 1 command-line argument")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Options{
|
|
||||||
Input: flag.Arg(0),
|
|
||||||
Verbose: *verbose,
|
|
||||||
Explanation: *explanation,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
@@ -2,10 +2,8 @@ package config
|
|||||||
|
|
||||||
// Arguments given to program.
|
// Arguments given to program.
|
||||||
type Config struct {
|
type Config struct {
|
||||||
// The source code given to the program.
|
Source Source // The source code given to the program.
|
||||||
Source Source
|
Verbose bool // Whether or not to print debug logs.
|
||||||
// Whether or not to print debug logs.
|
Explanation bool // Whether or not to print an explanation of the reduction.
|
||||||
Verbose bool
|
Profile string // If not nil, print a CPU profile during execution.
|
||||||
// Whether or not to print an explanation of the reduction.
|
|
||||||
Explanation bool
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ func FromArgs() (*Config, error) {
|
|||||||
// Parse flags.
|
// Parse flags.
|
||||||
verbose := flag.Bool("v", false, "Verbosity. If set, the program will print logs.")
|
verbose := flag.Bool("v", false, "Verbosity. If set, the program will print logs.")
|
||||||
explanation := flag.Bool("x", false, "Explanation. Whether or not to show all reduction steps.")
|
explanation := flag.Bool("x", false, "Explanation. Whether or not to show all reduction steps.")
|
||||||
|
profile := flag.String("p", "", "CPU profiling. If set, the program will run a performance profile during execution.")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Parse non-flag arguments.
|
// Parse non-flag arguments.
|
||||||
@@ -31,5 +32,6 @@ func FromArgs() (*Config, error) {
|
|||||||
Source: source,
|
Source: source,
|
||||||
Verbose: *verbose,
|
Verbose: *verbose,
|
||||||
Explanation: *explanation,
|
Explanation: *explanation,
|
||||||
|
Profile: *profile,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|||||||
49
internal/executer/executer.go
Normal file
49
internal/executer/executer.go
Normal file
@@ -0,0 +1,49 @@
|
|||||||
|
package executer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"git.maximhutz.com/max/lambda/internal/config"
|
||||||
|
"git.maximhutz.com/max/lambda/pkg/lambda"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Executor struct {
|
||||||
|
Config *config.Config
|
||||||
|
}
|
||||||
|
|
||||||
|
func New(config *config.Config) *Executor {
|
||||||
|
return &Executor{Config: config}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (e Executor) Run(expr *lambda.Expression) (*Results, error) {
|
||||||
|
results := &Results{}
|
||||||
|
|
||||||
|
if e.Config.Profile != "" {
|
||||||
|
profiler := Profiler{File: e.Config.Profile}
|
||||||
|
if err := profiler.Start(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer profiler.End()
|
||||||
|
}
|
||||||
|
|
||||||
|
start := time.Now()
|
||||||
|
|
||||||
|
if e.Config.Explanation {
|
||||||
|
fmt.Println(lambda.Stringify(*expr))
|
||||||
|
}
|
||||||
|
|
||||||
|
for lambda.ReduceOnce(expr) {
|
||||||
|
if e.Config.Verbose {
|
||||||
|
slog.Info("reduction", "tree", lambda.Stringify(*expr))
|
||||||
|
}
|
||||||
|
if e.Config.Explanation {
|
||||||
|
fmt.Println(" =", lambda.Stringify(*expr))
|
||||||
|
}
|
||||||
|
results.StepsTaken++
|
||||||
|
}
|
||||||
|
|
||||||
|
results.TimeElapsed = uint64(time.Since(start).Milliseconds())
|
||||||
|
return results, nil
|
||||||
|
}
|
||||||
40
internal/executer/profiler.go
Normal file
40
internal/executer/profiler.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package executer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime/pprof"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Profiler struct {
|
||||||
|
File string
|
||||||
|
filePointer *os.File
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Profiler) Start() error {
|
||||||
|
absPath, err := filepath.Abs(p.File)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = os.MkdirAll(filepath.Dir(absPath), 0777)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
p.filePointer, err = os.Create(absPath)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if err = pprof.StartCPUProfile(p.filePointer); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (p *Profiler) End() {
|
||||||
|
pprof.StopCPUProfile()
|
||||||
|
p.filePointer.Close()
|
||||||
|
}
|
||||||
23
internal/executer/results.go
Normal file
23
internal/executer/results.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
package executer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Results struct {
|
||||||
|
StepsTaken uint64 // Number of steps taken during execution.
|
||||||
|
TimeElapsed uint64 // The time (ms) taken for execution to complete.
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Results) OpsPerSecond() float32 {
|
||||||
|
return float32(r.StepsTaken) / (float32(r.TimeElapsed) / 1000)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r Results) String() string {
|
||||||
|
builder := strings.Builder{}
|
||||||
|
fmt.Fprintln(&builder, "Time Spent:", r.TimeElapsed, "ms")
|
||||||
|
fmt.Fprintln(&builder, "Steps:", r.StepsTaken)
|
||||||
|
fmt.Fprintln(&builder, "Speed:", r.OpsPerSecond(), "ops")
|
||||||
|
return builder.String()
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user