adding memstat option to server

This commit is contained in:
spf13 2014-09-22 09:45:05 -04:00
parent 5d565c34e5
commit 26e44506e9

View file

@ -1,4 +1,4 @@
// Copyright © 2013 Steve Francia <spf@spf13.com>. // Copyright © 2013-14 Steve Francia <spf@spf13.com>.
// //
// Licensed under the Simple Public License, Version 2.0 (the "License"); // Licensed under the Simple Public License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License. // you may not use this file except in compliance with the License.
@ -19,8 +19,10 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"runtime"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/hugo/helpers" "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(&serverWatch, "watch", "w", false, "watch filesystem for changes and recreate as needed")
serverCmd.Flags().BoolVarP(&serverAppend, "appendPort", "", true, "append port to baseurl") 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().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 serverCmd.Run = server
} }
@ -84,6 +88,10 @@ func server(cmd *cobra.Command, args []string) {
} }
viper.Set("BaseUrl", BaseUrl) viper.Set("BaseUrl", BaseUrl)
if err := memStats(); err != nil {
jww.ERROR.Println("memstats error:", err)
}
build(serverWatch) build(serverWatch)
// Watch runs its own server as part of the routine // Watch runs its own server as part of the routine
@ -157,3 +165,38 @@ func fixUrl(s string) (string, error) {
} }
return u.String(), nil 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
}