shithub: hugo

Download patch

ref: 26e44506e92172c1d692690ce5c660ccf676725b
parent: 5d565c34e5909f30f9cf7fa4c499f0297416cb83
author: spf13 <[email protected]>
date: Mon Sep 22 05:45:05 EDT 2014

adding memstat option to server

--- a/commands/server.go
+++ b/commands/server.go
@@ -1,4 +1,4 @@
-// Copyright © 2013 Steve Francia <[email protected]>.
+// Copyright © 2013-14 Steve Francia <[email protected]>.
 //
 // 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 @@
 	"net/http"
 	"net/url"
 	"os"
+	"runtime"
 	"strconv"
 	"strings"
+	"time"
 
 	"github.com/spf13/cobra"
 	"github.com/spf13/hugo/helpers"
@@ -49,6 +51,8 @@
 	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 @@
 	}
 	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
@@ -156,4 +164,39 @@
 		u.Host = "localhost"
 	}
 	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
 }