-
-
Save gnufied/402523b0601f008beb8d627899c73ef8 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
package main | |
import ( | |
"fmt" | |
"sync" | |
"time" | |
) | |
// DockerStats contains docker stats | |
type DockerStats struct { | |
previousCPUValues map[string]CPUValues | |
age int | |
} | |
// CPUValues struct contains the last cpu-usage values in order to compute properly the current values. | |
// (see calculateCPUPercent() for more details) | |
type CPUValues struct { | |
totCPU, systemCPU uint64 | |
} | |
func (d DockerStats) getContainerStats(container string) { | |
fmt.Println("Getting container stats") | |
oldValue := d.previousCPUValues[container] | |
time.Sleep(time.Duration(80) * time.Millisecond) | |
cpuvalue := CPUValues{totCPU: 100 + oldValue.totCPU, systemCPU: 222 + oldValue.systemCPU} | |
time.Sleep(time.Duration(80) * time.Millisecond) | |
d.previousCPUValues[container] = cpuvalue | |
time.Sleep(time.Duration(80) * time.Millisecond) | |
d.age = 20 | |
} | |
func main() { | |
d := new(DockerStats) | |
d.previousCPUValues = map[string]CPUValues{} | |
cpuvalue := CPUValues{totCPU: 45, systemCPU: 60} | |
d.previousCPUValues["foobar"] = cpuvalue | |
d.age = 100 | |
var wg sync.WaitGroup | |
wg.Add(100) | |
for i := 0; i < 100; i++ { | |
container := fmt.Sprintf("foobar : %d", i) | |
d.previousCPUValues[container] = cpuvalue | |
go func() { | |
defer wg.Done() | |
d.getContainerStats(container) | |
}() | |
} | |
wg.Wait() | |
fmt.Println("totCPU", d.previousCPUValues["foobar"].totCPU) | |
fmt.Println("age", d.age) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It crashes with: