Made it easier to do some profiling on the code.

This commit is contained in:
Maurice Makaay 2019-07-12 12:33:53 +00:00
parent a9964163c1
commit b8d99972bb
8 changed files with 84 additions and 28 deletions

View File

@ -5,4 +5,4 @@ test:
test-sushi: test-sushi:
@(cd cmd/burntsushi-tester; go build) @(cd cmd/burntsushi-tester; go build)
@echo -n "BurntSushi tests: " @echo -n "BurntSushi tests: "
@(cd cmd/burntsushi-tester && ${GOPATH}/bin/toml-test ./burntsushi-tester) @(/bin/bash -c "time ${GOPATH}/bin/toml-test ./cmd/burntsushi-tester/burntsushi-tester")

View File

@ -6,16 +6,20 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"io/ioutil"
"log" "log"
"os" "os"
"path" "path"
"time"
"git.makaay.nl/mauricem/go-toml/parse" "git.makaay.nl/mauricem/go-toml/parse"
"github.com/pkg/profile"
) )
func init() { var doProfile *int
log.SetFlags(0)
func init() {
doProfile = flag.Int("p", 0, "Perform pprof profiling (value is number of run loops)")
flag.Usage = usage flag.Usage = usage
flag.Parse() flag.Parse()
} }
@ -28,11 +32,24 @@ func usage() {
} }
func main() { func main() {
if flag.NArg() != 0 { if *doProfile > 0 {
flag.Usage() fmt.Println("Profiling ...")
inputBytes, _ := ioutil.ReadAll(os.Stdin)
inputStr := string(inputBytes)
p := profile.Start()
start := time.Now()
for i := 0; i < *doProfile; i++ {
parse.Run(inputStr)
fmt.Printf("cycle %d / %d\r", i+1, *doProfile)
}
duration := time.Since(start)
p.Stop()
fmt.Printf("\n")
fmt.Println("Duration:", duration)
return
} }
toml, err := parse.RunWithoutSanityChecks(os.Stdin) toml, err := parse.Run(os.Stdin)
if err != nil { if err != nil {
log.Fatalf("Error decoding TOML: %s", err) log.Fatalf("Error decoding TOML: %s", err)
} else { } else {

View File

@ -57,11 +57,3 @@ func Run(input interface{}) (ast.Table, error) {
err := parse.New(p.startDocument)(input) err := parse.New(p.startDocument)(input)
return p.doc.Root, err return p.doc.Root, err
} }
// RunWithoutSanityChecks runs the TOML parser against the provided input data.
// The parsekit sanity checks are disabled during the parse run.
func RunWithoutSanityChecks(input interface{}) (ast.Table, error) {
p := newParser()
err := parse.New(p.startDocument)(input)
return p.doc.Root, err
}

View File

@ -1,39 +1,57 @@
package main package main
import ( import (
"flag"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "log"
"math" "math"
"os" "os"
"path"
"time" "time"
"git.makaay.nl/mauricem/go-parsekit/tokenize" "git.makaay.nl/mauricem/go-parsekit/tokenize"
"github.com/pkg/profile" "github.com/pkg/profile"
) )
const runProfiler = true var doProfile *int
func init() {
doProfile = flag.Int("p", 0, "Perform pprof profiling (value is number of run loops)")
flag.Usage = usage
flag.Parse()
}
func usage() {
log.Printf("Usage: %s < <path to TOML-file>\n", path.Base(os.Args[0]))
flag.PrintDefaults()
os.Exit(1)
}
func main() { func main() {
bytes, _ := ioutil.ReadAll(os.Stdin)
input := string(bytes)
toml := BuildGrammar() toml := BuildGrammar()
var result *tokenize.Result var result *tokenize.Result
var err error var err error
if runProfiler { if *doProfile > 0 {
t := profile.Start() //profile.CPUProfile) fmt.Println("Profiling ...")
for i := 0; i < 20; i++ { inputBytes, _ := ioutil.ReadAll(os.Stdin)
fmt.Printf(".") inputStr := string(inputBytes)
result, err = toml.Match(input) p := profile.Start()
start := time.Now()
for i := 0; i < *doProfile; i++ {
result, err = toml.Match(inputStr)
fmt.Printf("cycle %d / %d, tokens=%d\r", i+1, *doProfile, len(result.Tokens))
} }
t.Stop() duration := time.Since(start)
} else { fmt.Printf("\n")
result, err = toml.Match(input) fmt.Println("Duration:", duration)
p.Stop()
return
} }
result, err = toml.Match(os.Stdin)
if err != nil { if err != nil {
log.Fatalf("Error in parsing TOML: %s\n", err) log.Fatalf("Error in parsing TOML: %s\n", err)
} else { } else {

Binary file not shown.

View File

@ -0,0 +1,12 @@
0.004 burntsushi tester with x
1.347 burntsushi profile cycle 1000 with x
0.005 parse2 non-profile mode with x
1.850 parse2 profile cycle 1000 with x
0.098 burntsushi with long.toml
0.950 burntsushi profile cycle 10 with long.toml
0.190 parse2 non-profile mode with long.toml
1.843 parse2 profile cycle 10 with long.toml
0.213 parse test set
0.239 burntsushi test set

17
parse2/profile-sushi.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/bash
#cd $GOPATH/src/git.makaay.nl/mauricem/go-toml/cmd/burntsushi-tester/
#go build
#go install
#cd $GOPATH/src/git.makaay.nl/mauricem/go-toml/parse2
#PPFILE=`cat long.toml | $GOPATH/bin/burntsushi-tester 2>&1 | grep "profiling enabled" | cut -d, -f2`
#echo $PPFILE
#ppfile=` | ./parse2 2>&1 | grep "cpu profiling enabled" | cut -d, -f2`
cd ../cmd/burntsushi-tester
go build
cd ../../parse2
ppfile=`cat x | ../cmd/burntsushi-tester/burntsushi-tester -p 100 2>&1 | grep "profiling enabled" | cut -d, -f2`
go tool pprof -http 0.0.0.0:8888 ../cmd/burntsushi-tester/burntsushi-tester $ppfile

View File

@ -10,6 +10,6 @@
#ppfile=` | ./parse2 2>&1 | grep "cpu profiling enabled" | cut -d, -f2` #ppfile=` | ./parse2 2>&1 | grep "cpu profiling enabled" | cut -d, -f2`
go build go build
ppfile=`cat long.toml | time ./parse2 2>&1 | grep "profiling enabled" | cut -d, -f2` ppfile=`cat long.toml | ./parse2 -p 10 2>&1 | grep "profiling enabled" | cut -d, -f2`
go tool pprof -http 0.0.0.0:8888 ./parse2 $ppfile go tool pprof -http 0.0.0.0:8888 ./parse2 $ppfile