diff --git a/commands/server.go b/commands/server.go index c80440c5c..ee30b9275 100644 --- a/commands/server.go +++ b/commands/server.go @@ -1,4 +1,4 @@ -// Copyright © 2013 Steve Francia . +// Copyright © 2013-14 Steve Francia . // // Licensed under the Simple Public License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. @@ -19,8 +19,10 @@ import ( "net/http" "net/url" "os" + "runtime" "strconv" "strings" + "time" "github.com/spf13/cobra" "github.com/spf13/hugo/helpers" @@ -49,6 +51,8 @@ func init() { serverCmd.Flags().BoolVarP(&serverWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed") serverCmd.Flags().BoolVarP(&serverAppend, "appendPort", "", true, "append port to baseurl") serverCmd.Flags().BoolVar(&disableLiveReload, "disableLiveReload", false, "watch without enabling live browser reload on rebuild") + serverCmd.Flags().String("memstats", "", "log memory usage to this file") + serverCmd.Flags().Int("meminterval", 100, "interval to poll memory usage (requires --memstats)") serverCmd.Run = server } @@ -84,6 +88,10 @@ func server(cmd *cobra.Command, args []string) { } viper.Set("BaseUrl", BaseUrl) + if err := memStats(); err != nil { + jww.ERROR.Println("memstats error:", err) + } + build(serverWatch) // Watch runs its own server as part of the routine @@ -157,3 +165,38 @@ func fixUrl(s string) (string, error) { } return u.String(), nil } + +func memStats() error { + memstats := serverCmd.Flags().Lookup("memstats").Value.String() + if memstats != "" { + interval, err := time.ParseDuration(serverCmd.Flags().Lookup("meminterval").Value.String()) + if err != nil { + interval, _ = time.ParseDuration("100ms") + } + + fileMemStats, err := os.Create(memstats) + if err != nil { + return err + } + + fileMemStats.WriteString("# Time\tHeapSys\tHeapAlloc\tHeapIdle\tHeapReleased\n") + + go func() { + var stats runtime.MemStats + + start := time.Now().UnixNano() + + for { + runtime.ReadMemStats(&stats) + if fileMemStats != nil { + fileMemStats.WriteString(fmt.Sprintf("%d\t%d\t%d\t%d\t%d\n", + (time.Now().UnixNano()-start)/1000000, stats.HeapSys, stats.HeapAlloc, stats.HeapIdle, stats.HeapReleased)) + time.Sleep(interval) + } else { + break + } + } + }() + } + return nil +}