mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-20 05:13:05 +00:00
parent
1157fef859
commit
fdf1d94ebc
2 changed files with 45 additions and 29 deletions
|
@ -23,50 +23,66 @@ import (
|
||||||
jww "github.com/spf13/jwalterweatherman"
|
jww "github.com/spf13/jwalterweatherman"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var _ cmder = (*benchmarkCmd)(nil)
|
||||||
|
|
||||||
|
type cmder interface {
|
||||||
|
getCommand() *cobra.Command
|
||||||
|
}
|
||||||
|
|
||||||
|
type benchmarkCmd struct {
|
||||||
benchmarkTimes int
|
benchmarkTimes int
|
||||||
cpuProfileFile string
|
cpuProfileFile string
|
||||||
memProfileFile string
|
memProfileFile string
|
||||||
)
|
|
||||||
|
|
||||||
var benchmarkCmd = &cobra.Command{
|
cmd *cobra.Command
|
||||||
|
}
|
||||||
|
|
||||||
|
func (c *benchmarkCmd) getCommand() *cobra.Command {
|
||||||
|
return c.cmd
|
||||||
|
}
|
||||||
|
|
||||||
|
func newBenchmarkCmd() *benchmarkCmd {
|
||||||
|
cmd := &cobra.Command{
|
||||||
Use: "benchmark",
|
Use: "benchmark",
|
||||||
Short: "Benchmark Hugo by building a site a number of times.",
|
Short: "Benchmark Hugo by building a site a number of times.",
|
||||||
Long: `Hugo can build a site many times over and analyze the running process
|
Long: `Hugo can build a site many times over and analyze the running process
|
||||||
creating a benchmark.`,
|
creating a benchmark.`,
|
||||||
|
}
|
||||||
|
|
||||||
|
initHugoBuilderFlags(cmd)
|
||||||
|
initBenchmarkBuildingFlags(cmd)
|
||||||
|
|
||||||
|
c := &benchmarkCmd{cmd: cmd}
|
||||||
|
|
||||||
|
cmd.Flags().StringVar(&c.cpuProfileFile, "cpuprofile", "", "path/filename for the CPU profile file")
|
||||||
|
cmd.Flags().StringVar(&c.memProfileFile, "memprofile", "", "path/filename for the memory profile file")
|
||||||
|
cmd.Flags().IntVarP(&c.benchmarkTimes, "count", "n", 13, "number of times to build the site")
|
||||||
|
|
||||||
|
cmd.RunE = c.benchmark
|
||||||
|
|
||||||
|
return c
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func (c *benchmarkCmd) benchmark(cmd *cobra.Command, args []string) error {
|
||||||
initHugoBuilderFlags(benchmarkCmd)
|
|
||||||
initBenchmarkBuildingFlags(benchmarkCmd)
|
|
||||||
|
|
||||||
benchmarkCmd.Flags().StringVar(&cpuProfileFile, "cpuprofile", "", "path/filename for the CPU profile file")
|
|
||||||
benchmarkCmd.Flags().StringVar(&memProfileFile, "memprofile", "", "path/filename for the memory profile file")
|
|
||||||
benchmarkCmd.Flags().IntVarP(&benchmarkTimes, "count", "n", 13, "number of times to build the site")
|
|
||||||
|
|
||||||
benchmarkCmd.RunE = benchmark
|
|
||||||
}
|
|
||||||
|
|
||||||
func benchmark(cmd *cobra.Command, args []string) error {
|
|
||||||
cfgInit := func(c *commandeer) error {
|
cfgInit := func(c *commandeer) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
c, err := InitializeConfig(false, cfgInit, benchmarkCmd)
|
comm, err := InitializeConfig(false, cfgInit, c.cmd)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var memProf *os.File
|
var memProf *os.File
|
||||||
if memProfileFile != "" {
|
if c.memProfileFile != "" {
|
||||||
memProf, err = os.Create(memProfileFile)
|
memProf, err = os.Create(c.memProfileFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var cpuProf *os.File
|
var cpuProf *os.File
|
||||||
if cpuProfileFile != "" {
|
if c.cpuProfileFile != "" {
|
||||||
cpuProf, err = os.Create(cpuProfileFile)
|
cpuProf, err = os.Create(c.cpuProfileFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -81,8 +97,8 @@ func benchmark(cmd *cobra.Command, args []string) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
t := time.Now()
|
t := time.Now()
|
||||||
for i := 0; i < benchmarkTimes; i++ {
|
for i := 0; i < c.benchmarkTimes; i++ {
|
||||||
if err = c.resetAndBuildSites(); err != nil {
|
if err = comm.resetAndBuildSites(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -102,9 +118,9 @@ func benchmark(cmd *cobra.Command, args []string) error {
|
||||||
totalMallocs := memStats.Mallocs - mallocs
|
totalMallocs := memStats.Mallocs - mallocs
|
||||||
|
|
||||||
jww.FEEDBACK.Println()
|
jww.FEEDBACK.Println()
|
||||||
jww.FEEDBACK.Printf("Average time per operation: %vms\n", int(1000*totalTime.Seconds()/float64(benchmarkTimes)))
|
jww.FEEDBACK.Printf("Average time per operation: %vms\n", int(1000*totalTime.Seconds()/float64(c.benchmarkTimes)))
|
||||||
jww.FEEDBACK.Printf("Average memory allocated per operation: %vkB\n", totalMemAllocated/uint64(benchmarkTimes)/1024)
|
jww.FEEDBACK.Printf("Average memory allocated per operation: %vkB\n", totalMemAllocated/uint64(c.benchmarkTimes)/1024)
|
||||||
jww.FEEDBACK.Printf("Average allocations per operation: %v\n", totalMallocs/uint64(benchmarkTimes))
|
jww.FEEDBACK.Printf("Average allocations per operation: %v\n", totalMallocs/uint64(c.benchmarkTimes))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -198,7 +198,7 @@ func AddCommands() {
|
||||||
HugoCmd.AddCommand(envCmd)
|
HugoCmd.AddCommand(envCmd)
|
||||||
HugoCmd.AddCommand(configCmd)
|
HugoCmd.AddCommand(configCmd)
|
||||||
HugoCmd.AddCommand(checkCmd)
|
HugoCmd.AddCommand(checkCmd)
|
||||||
HugoCmd.AddCommand(benchmarkCmd)
|
HugoCmd.AddCommand(newBenchmarkCmd().getCommand())
|
||||||
HugoCmd.AddCommand(convertCmd)
|
HugoCmd.AddCommand(convertCmd)
|
||||||
HugoCmd.AddCommand(newCmd)
|
HugoCmd.AddCommand(newCmd)
|
||||||
HugoCmd.AddCommand(listCmd)
|
HugoCmd.AddCommand(listCmd)
|
||||||
|
|
Loading…
Add table
Reference in a new issue