1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-23 06:12:24 +01:00

Add --profile

This commit is contained in:
Oliver Steele 2017-07-02 09:48:30 -04:00
parent e1d7653d3f
commit 365928dc74
2 changed files with 25 additions and 19 deletions

View File

@ -4,7 +4,6 @@ import (
"fmt" "fmt"
"os" "os"
"path/filepath" "path/filepath"
"runtime/pprof"
"sort" "sort"
"strings" "strings"
"time" "time"
@ -37,16 +36,7 @@ func cleanCommand(site *sites.Site) error {
return site.Clean(buildOptions) return site.Clean(buildOptions)
} }
func profileCommand(_ *sites.Site) error { func benchmarkCommand(_ *sites.Site) error {
printSetting("Profiling...", "")
var profilePath = "gojekyll.prof"
f, err := os.Create(profilePath)
if err != nil {
return err
}
if err = pprof.StartCPUProfile(f); err != nil {
return err
}
t0 := time.Now() t0 := time.Now()
for i := 0; time.Since(t0) < 10*time.Second; i++ { for i := 0; time.Since(t0) < 10*time.Second; i++ {
site, err := loadSite(*source, configFlags) site, err := loadSite(*source, configFlags)
@ -59,11 +49,6 @@ func profileCommand(_ *sites.Site) error {
} }
printSetting("", fmt.Sprintf("Run #%d; %.1fs elapsed", i+1, time.Since(t0).Seconds())) printSetting("", fmt.Sprintf("Run #%d; %.1fs elapsed", i+1, time.Since(t0).Seconds()))
} }
pprof.StopCPUProfile()
if err := f.Close(); err != nil {
return err
}
fmt.Println("Wrote", profilePath)
return nil return nil
} }

View File

@ -1,8 +1,10 @@
package main package main
import ( import (
"fmt"
"os" "os"
"path/filepath" "path/filepath"
"runtime/pprof"
"github.com/osteele/gojekyll/config" "github.com/osteele/gojekyll/config"
"github.com/osteele/gojekyll/sites" "github.com/osteele/gojekyll/sites"
@ -13,6 +15,7 @@ import (
var ( var (
buildOptions sites.BuildOptions buildOptions sites.BuildOptions
configFlags = config.Flags{} configFlags = config.Flags{}
profile = false
quiet = false quiet = false
) )
@ -30,7 +33,7 @@ var (
serve = app.Command("serve", "Serve your site locally").Alias("server").Alias("s") serve = app.Command("serve", "Serve your site locally").Alias("server").Alias("s")
open = serve.Flag("open-url", "Launch your site in a browser").Short('o').Bool() open = serve.Flag("open-url", "Launch your site in a browser").Short('o').Bool()
profile = app.Command("profile", "Build several times, and write a profile file") benchmark = app.Command("profile", "Repeat build for ten seconds. Implies --profile.")
variables = app.Command("variables", "Display a file or URL path's variables").Alias("v").Alias("var").Alias("vars") variables = app.Command("variables", "Display a file or URL path's variables").Alias("v").Alias("var").Alias("vars")
dataVariable = variables.Flag("data", "Display site.data").Bool() dataVariable = variables.Flag("data", "Display site.data").Bool()
@ -45,6 +48,7 @@ var (
) )
func init() { func init() {
app.Flag("profile", "Create a Go pprof CPU profile").BoolVar(&profile)
app.Flag("quiet", "Silence (some) output.").Short('q').BoolVar(&quiet) app.Flag("quiet", "Silence (some) output.").Short('q').BoolVar(&quiet)
build.Flag("dry-run", "Dry run").Short('n').BoolVar(&buildOptions.DryRun) build.Flag("dry-run", "Dry run").Short('n').BoolVar(&buildOptions.DryRun)
} }
@ -60,6 +64,9 @@ func main() {
if buildOptions.DryRun { if buildOptions.DryRun {
buildOptions.Verbose = true buildOptions.Verbose = true
} }
if cmd == benchmark.FullCommand() {
profile = true
}
app.FatalIfError(run(cmd), "") app.FatalIfError(run(cmd), "")
} }
@ -68,14 +75,28 @@ func run(cmd string) error {
if err != nil { if err != nil {
return err return err
} }
if profile {
profilePath := "gojekyll.prof"
printSetting("Profiling...", "")
f, err := os.Create(profilePath)
app.FatalIfError(err, "")
err = pprof.StartCPUProfile(f)
app.FatalIfError(err, "")
defer func() {
pprof.StopCPUProfile()
err = f.Close()
app.FatalIfError(err, "")
fmt.Println("Wrote", profilePath)
}()
}
switch cmd { switch cmd {
case benchmark.FullCommand():
return benchmarkCommand(site)
case build.FullCommand(): case build.FullCommand():
return buildCommand(site) return buildCommand(site)
case clean.FullCommand(): case clean.FullCommand():
return cleanCommand(site) return cleanCommand(site)
case profile.FullCommand():
return profileCommand(site)
case render.FullCommand(): case render.FullCommand():
return renderCommand(site) return renderCommand(site)
case routes.FullCommand(): case routes.FullCommand():