1
1
mirror of https://github.com/wader/fq.git synced 2024-09-11 12:05:39 +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:
dev/formats_dot.jq | dot -Tsvg -o formats.svg
# make memprof ARGS=". test.mp3"
# make cpuprof ARGS=". test.mp3"
.PHONY: prof
prof:
go build -o fq.prof main.go
CPUPROFILE=fq.cpu.prof MEMPROFILE=fq.mem.prof ./fq.prof "${ARGS}"
go build -tags profile -o fq.prof main.go
CPUPROFILE=fq.cpu.prof MEMPROFILE=fq.mem.prof ./fq.prof ${ARGS}
.PHONY: memprof
memprof: prof
go tool pprof -http :5555 fq.prof fq.mem.prof

View File

@ -26,13 +26,8 @@ func Start(cpuProfilePath string, memProfilePath string) func() {
})
}
}
return func() {
for _, fn := range deferFns {
fn()
}
if memProfilePath != "" {
if memProfilePath != "" {
deferFns = append(deferFns, func() {
f, err := os.Create(memProfilePath)
if err != nil {
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)
}
}
})
}
return func() {
for _, fn := range deferFns {
fn()
}
}
}

View File

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

View File

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