-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource.go
47 lines (41 loc) · 1.05 KB
/
resource.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package main
import (
"fmt"
"runtime"
"sync"
"time"
)
type ResourceUsage struct {
MaxSystemMemory uint64
MaxGoroutines int
Duration time.Duration
}
func MonitorResources(startTime time.Time, returnChan chan *ResourceUsage, end chan bool, wg *sync.WaitGroup) {
wg.Add(1)
defer wg.Done()
maxSystemMemory := uint64(0)
maxGoroutines := 0
for {
select {
case <-end:
returnChan <- &ResourceUsage{maxSystemMemory, maxGoroutines, time.Since(startTime)}
return
default:
maxSystemMemory = max(maxSystemMemory, GetSystemMemory())
maxGoroutines = max(maxGoroutines, runtime.NumGoroutine())
time.Sleep(100 * time.Millisecond)
}
}
}
func ReportResourceUsage(resources *ResourceUsage) {
fmt.Println()
fmt.Println("======= Resource Usage =======")
fmt.Println("Duration:", resources.Duration.String())
fmt.Println("Max system memory:", toAppropriateUnit(resources.MaxSystemMemory))
fmt.Println("Max goroutines:", resources.MaxGoroutines)
}
func GetSystemMemory() uint64 {
var m runtime.MemStats
runtime.ReadMemStats(&m)
return m.Sys
}