From fc6885fabc245ccf408bba7518d94ad9ec3e2b2f Mon Sep 17 00:00:00 2001 From: celogeek <65178+celogeek@users.noreply.github.com> Date: Fri, 30 Dec 2022 03:32:05 +0100 Subject: [PATCH] add memory usage --- main.go | 60 ++++++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/main.go b/main.go index 956fcd9..5d596e5 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "go-comic-converter/internal/epub" "os" "path/filepath" + "runtime" "strings" ) @@ -53,14 +54,15 @@ func init() { } type Option struct { - Input string - Output string - Profile string - Author string - Title string - Quality int - NoCrop bool - LimitMb int + Input string + Output string + Profile string + Author string + Title string + Quality int + NoCrop bool + LimitMb int + PrintMem bool } func (o *Option) String() string { @@ -80,14 +82,15 @@ func (o *Option) String() string { return fmt.Sprintf(`Go Comic Converter Options: - Input : %s - Output : %s - Profile: %s - %s - %dx%d - Author : %s - Title : %s - Quality: %d - Crop : %v - LimitMb: %s + Input : %s + Output : %s + Profile : %s - %s - %dx%d + Author : %s + Title : %s + Quality : %d + Crop : %v + LimitMb : %s + PrintMem: %v `, o.Input, o.Output, @@ -100,6 +103,27 @@ Options: o.Quality, !o.NoCrop, limitmb, + o.PrintMem, + ) +} + +func PrintMemUsage() { + var m runtime.MemStats + runtime.ReadMemStats(&m) + + bToMb := func(b uint64) uint64 { return b / 1024 / 1024 } + + // For info on each, see: https://golang.org/pkg/runtime/#MemStats + fmt.Printf(`Memory Usage: + Alloc : %v MiB + TotalAlloc: %v MiB + Sys : %v MiB + NumGC : %v +`, + bToMb(m.Alloc), + bToMb(m.TotalAlloc), + bToMb(m.Sys), + m.NumGC, ) } @@ -123,6 +147,7 @@ func main() { flag.IntVar(&opt.Quality, "quality", 85, "Quality of the image") flag.BoolVar(&opt.NoCrop, "nocrop", false, "Disable cropping") flag.IntVar(&opt.LimitMb, "limitmb", 0, "Limit size of the ePub: Default nolimit (0), Minimum 20") + flag.BoolVar(&opt.PrintMem, "printmem", false, "Print memory usage") flag.Parse() if opt.Input == "" { @@ -183,5 +208,8 @@ func main() { os.Exit(1) } + if opt.PrintMem { + PrintMemUsage() + } os.Exit(0) }