mirror of
https://github.com/wader/fq.git
synced 2024-11-25 23:13:19 +03:00
cli: Make profile build optional and move it to cli
This commit is contained in:
parent
b849895970
commit
b33f2cd678
6
Makefile
6
Makefile
@ -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
|
||||
|
@ -26,13 +26,8 @@ func Start(cpuProfilePath string, memProfilePath string) func() {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
return func() {
|
||||
for _, fn := range deferFns {
|
||||
fn()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
2
main.go
2
main.go
@ -9,7 +9,5 @@ import (
|
||||
var version = "dev"
|
||||
|
||||
func main() {
|
||||
defer cli.MaybeProfile()()
|
||||
cli.MaybeLogFile()
|
||||
cli.Main(registry.Default, version)
|
||||
}
|
||||
|
@ -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
5
pkg/cli/noprofile.go
Normal file
@ -0,0 +1,5 @@
|
||||
//go:build !profile
|
||||
|
||||
package cli
|
||||
|
||||
func maybeProfile() func() { return func() {} }
|
13
pkg/cli/profile.go
Normal file
13
pkg/cli/profile.go
Normal 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"))
|
||||
}
|
Loading…
Reference in New Issue
Block a user