1
1
mirror of https://github.com/wader/fq.git synced 2024-11-22 15:45:45 +03:00

cli: Make profile build optional and move it to cli

This commit is contained in:
Mattias Wadman 2021-08-31 14:14:12 +02:00
parent b849895970
commit b33f2cd678
6 changed files with 34 additions and 17 deletions

View File

@ -64,10 +64,12 @@ depgraph.svg:
formats.svg: formats.svg:
dev/formats_dot.jq | dot -Tsvg -o formats.svg dev/formats_dot.jq | dot -Tsvg -o formats.svg
# make memprof ARGS=". test.mp3"
# make cpuprof ARGS=". test.mp3"
.PHONY: prof .PHONY: prof
prof: prof:
go build -o fq.prof main.go go build -tags profile -o fq.prof main.go
CPUPROFILE=fq.cpu.prof MEMPROFILE=fq.mem.prof ./fq.prof "${ARGS}" CPUPROFILE=fq.cpu.prof MEMPROFILE=fq.mem.prof ./fq.prof ${ARGS}
.PHONY: memprof .PHONY: memprof
memprof: prof memprof: prof
go tool pprof -http :5555 fq.prof fq.mem.prof go tool pprof -http :5555 fq.prof fq.mem.prof

View File

@ -26,13 +26,8 @@ func Start(cpuProfilePath string, memProfilePath string) func() {
}) })
} }
} }
if memProfilePath != "" {
return func() { deferFns = append(deferFns, func() {
for _, fn := range deferFns {
fn()
}
if memProfilePath != "" {
f, err := os.Create(memProfilePath) f, err := os.Create(memProfilePath)
if err != nil { if err != nil {
log.Fatal("could not create memory profile: ", err) log.Fatal("could not create memory profile: ", err)
@ -45,6 +40,12 @@ func Start(cpuProfilePath string, memProfilePath string) func() {
log.Fatal("could not close memory profile: ", err) log.Fatal("could not close memory profile: ", err)
} }
} }
})
}
return func() {
for _, fn := range deferFns {
fn()
} }
} }
} }

View File

@ -9,7 +9,5 @@ import (
var version = "dev" var version = "dev"
func main() { func main() {
defer cli.MaybeProfile()()
cli.MaybeLogFile()
cli.Main(registry.Default, version) cli.Main(registry.Default, version)
} }

View File

@ -13,17 +13,12 @@ import (
"path/filepath" "path/filepath"
"github.com/wader/fq/format/registry" "github.com/wader/fq/format/registry"
"github.com/wader/fq/internal/profile"
"github.com/wader/fq/pkg/interp" "github.com/wader/fq/pkg/interp"
"github.com/wader/readline" "github.com/wader/readline"
) )
func MaybeProfile() func() { func maybeLogFile() {
return profile.Start(os.Getenv("CPUPROFILE"), os.Getenv("MEMPROFILE"))
}
func MaybeLogFile() {
// used during dev to redirect log to file, useful when debugging repl etc // used during dev to redirect log to file, useful when debugging repl etc
if lf := os.Getenv("LOGFILE"); lf != "" { if lf := os.Getenv("LOGFILE"); lf != "" {
if f, err := os.Create(lf); err == nil { if f, err := os.Create(lf); err == nil {
@ -194,6 +189,9 @@ func (o *standardOS) Close() error {
func Main(r *registry.Registry, version string) { func Main(r *registry.Registry, version string) {
os.Exit(func() int { os.Exit(func() int {
defer maybeProfile()()
maybeLogFile()
sos := newStandardOS() sos := newStandardOS()
defer sos.Close() defer sos.Close()
i, err := interp.New(sos, r) i, err := interp.New(sos, r)

5
pkg/cli/noprofile.go Normal file
View File

@ -0,0 +1,5 @@
//go:build !profile
package cli
func maybeProfile() func() { return func() {} }

13
pkg/cli/profile.go Normal file
View File

@ -0,0 +1,13 @@
//go:build profile
package cli
import (
"os"
"github.com/wader/fq/internal/profile"
)
func maybeProfile() func() {
return profile.Start(os.Getenv("CPUPROFILE"), os.Getenv("MEMPROFILE"))
}