Created
April 7, 2017 17:06
-
-
Save jordanorelli/35f8acc93509513da2b99346cd1391f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/main.go b/main.go | |
index 3f9169b..edd5f4f 100644 | |
--- a/main.go | |
+++ b/main.go | |
@@ -1,26 +1,49 @@ | |
package main | |
import ( | |
+ "flag" | |
"github.com/symondsandson/stats/scanner" | |
"github.com/symondsandson/stats/table" | |
terminal "github.com/wayneashleyberry/terminal-dimensions" | |
+ "io/ioutil" | |
+ "log" | |
"math" | |
"os" | |
"sort" | |
"sync" | |
+ "time" | |
) | |
+const defaultWidth = 88 | |
+ | |
+var debug_log = log.New(ioutil.Discard, "", 0) | |
+ | |
type tableResult struct { | |
- order int | |
+ name string | |
err error | |
table *table.Table | |
} | |
+var opts struct { | |
+ debug bool | |
+} | |
+ | |
func main() { | |
+ flag.BoolVar(&opts.debug, "debug", false, "whether or not to debug") | |
+ flag.Parse() | |
+ if opts.debug { | |
+ debug_log = log.New(os.Stdout, "", 0) | |
+ } | |
+ | |
// Get information about the terminal for display purposes | |
+ // note(jorelli): I probably wouldn't bother to introduce a new package dependency for this. | |
terminalWidth, err := terminal.Width() | |
if err != nil { | |
- terminalWidth = 88 | |
+ debug_log.Printf("unable to get terminal width: %v", err) | |
+ debug_log.Printf("using default terminal width: %v", defaultWidth) | |
+ terminalWidth = defaultWidth | |
+ } else { | |
+ debug_log.Printf("using terminal width: %v", terminalWidth) | |
} | |
columns, columnSize := terminalWidth/44, 0 | |
@@ -30,25 +53,28 @@ func main() { | |
columnSize = int(math.Floor((float64(terminalWidth) - (2 * float64(columns))) / float64(columns))) | |
} | |
+ tableFunctions := map[string]table.TableFunction{ | |
+ "0.system": scanner.SetupSystemInfo, | |
+ "1.device": scanner.SetupDeviceInfo, | |
+ "2.monit": scanner.SetupMonitInfo, | |
+ "3.consul": scanner.SetupConsulInfo, | |
+ } | |
+ | |
var wg sync.WaitGroup | |
- wg.Add(4) | |
+ wg.Add(len(tableFunctions)) | |
tableResultChannel := make(chan tableResult) | |
- tableFunctions := []table.TableFunction{ | |
- scanner.SetupSystemInfo, | |
- scanner.SetupDeviceInfo, | |
- scanner.SetupMonitInfo, | |
- scanner.SetupConsulInfo, | |
- } | |
- | |
- for i, tableFunction := range tableFunctions { | |
- go func(f table.TableFunction, o int) { | |
+ for name, tableFunction := range tableFunctions { | |
+ go func(f table.TableFunction, name string) { | |
defer wg.Done() | |
+ start := time.Now() | |
t, err := f() | |
+ elapsed := time.Since(start) | |
+ debug_log.Printf("%s took %v time to generate table data", name, elapsed) | |
- tableResultChannel <- tableResult{order: o, table: t, err: err} | |
- }(tableFunction, i) | |
+ tableResultChannel <- tableResult{name: name, table: t, err: err} | |
+ }(tableFunction, name) | |
} | |
go func() { | |
@@ -60,7 +86,7 @@ func main() { | |
for tr := range tableResultChannel { | |
tableResults = append(tableResults, tr) | |
} | |
- sort.Slice(tableResults, func(i, j int) bool { return tableResults[i].order < tableResults[j].order }) | |
+ sort.Slice(tableResults, func(i, j int) bool { return tableResults[i].name < tableResults[j].name }) | |
for _, tableResult := range tableResults { | |
if tableResult.err != nil { | |
diff --git a/table/table.go b/table/table.go | |
index fda25b3..477be13 100644 | |
--- a/table/table.go | |
+++ b/table/table.go | |
@@ -7,6 +7,7 @@ import ( | |
"io" | |
"sort" | |
"sync" | |
+ "time" | |
) | |
type TableFunction func() (*Table, error) | |
@@ -43,7 +44,14 @@ func (t *Table) Async(cellFunctions []cell.CellFunction) { | |
go func(f cell.CellFunction, o int) { | |
defer wg.Done() | |
+ start := time.Now() | |
c, err := f() | |
+ elapsed := time.Since(start) | |
+ if err == nil { | |
+ fmt.Printf("cell %d (%s) took %v to run\n", o, c.Name, elapsed) | |
+ } else { | |
+ fmt.Printf("cell %d failed after %v\n", o, elapsed) | |
+ } | |
cellResultChannel <- cellResult{order: o, cell: c, err: err} | |
}(cf, i) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment