diff --git a/.gitignore b/.gitignore index 66451b4..032c7f4 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,7 @@ php-parser *example.php -cpu.prof -mem.prof +cpu.pprof +mem.pprof php7.test php5.test diff --git a/Makefile b/Makefile index 4f0a311..14e7188 100644 --- a/Makefile +++ b/Makefile @@ -36,12 +36,12 @@ compile: ./php5/php5.go ./php7/php7.go ./scanner/scanner.go goyacc -o $@ $< cpu_pprof: - GOGC=off go test -cpuprofile cpu.prof -bench=. -benchtime=20s ./php7 - go tool pprof ./php7.test cpu.prof + GOGC=off go test -cpuprofile cpu.pprof -bench=. -benchtime=20s ./php7 + go tool pprof ./php7.test cpu.pprof mem_pprof: - GOGC=off go test -memprofile mem.prof -bench=. -benchtime=20s -benchmem ./php7 - go tool pprof -alloc_objects ./php7.test mem.prof + GOGC=off go test -memprofile mem.pprof -bench=. -benchtime=20s -benchmem ./php7 + go tool pprof -alloc_objects ./php7.test mem.pprof cpu_pprof_php5: GOGC=off go test -cpuprofile cpu.prof -bench=. -benchtime=20s ./php5 diff --git a/README.md b/README.md index e4dbd19..0d3e482 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ php-parser [flags] ... | flag | type | description | |-------|------|----------------------------------------------| | -d |string| dump format: [custom, go, json, pretty-json] | +| -prof |string| start profiler: [cpu, mem] | | -p | bool | show positions | | -c | bool | show comments | | -r | bool | resolve names | diff --git a/main.go b/main.go index ac260c7..05bceba 100644 --- a/main.go +++ b/main.go @@ -8,6 +8,7 @@ import ( "path/filepath" "sync" + "github.com/pkg/profile" "github.com/yookoala/realpath" "github.com/z7zmey/php-parser/parser" "github.com/z7zmey/php-parser/php5" @@ -18,6 +19,7 @@ import ( var wg sync.WaitGroup var usePhp5 *bool var dumpType string +var profiler string var showPositions *bool var showComments *bool var showResolvedNs *bool @@ -28,9 +30,17 @@ func main() { showComments = flag.Bool("c", false, "show comments") showResolvedNs = flag.Bool("r", false, "resolve names") flag.StringVar(&dumpType, "d", "", "dump format: [custom, go, json, pretty_json]") + flag.StringVar(&profiler, "prof", "", "start profiler: [cpu, mem]") flag.Parse() + switch profiler { + case "cpu": + defer profile.Start(profile.ProfilePath("."), profile.NoShutdownHook).Stop() + case "mem": + defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.NoShutdownHook).Stop() + } + pathCh := make(chan string) resultCh := make(chan parser.Parser)