Skip to content

Commit 11216fa

Browse files
committed
Add high CPU demo
1 parent 64c84f2 commit 11216fa

File tree

5 files changed

+114
-0
lines changed

5 files changed

+114
-0
lines changed

monitoring/cpu/README.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# container-monitoring-demo
2+
3+
This is a demo of how to monitor containers with simple Docker commands.
4+
5+
## To run
6+
7+
**Pre-requisites:** You need to have Docker installed and running first. See [Docker's installation instructions](https://docs.docker.com/engine/installation/) for more information.
8+
9+
Once you've cloned this repo, run this to build the demo app:
10+
11+
```bash
12+
docker build -t highcpu-go highcpu-go
13+
14+
docker run --rm --name highcpu-go -d highcpu-go
15+
```
16+
17+
### Monitoring with docker stats
18+
19+
The app is designed to oscillate between using 100% CPU and 0% CPU.
20+
21+
You can see the container's CPU usage with `docker stats`. We'll run the command with a screen refresh interval of 1 second:
22+
23+
```bash
24+
docker stats --interval 1
25+
```
26+
27+
You should see something like this:
28+
29+
```bash
30+
ID NAME CPU % MEM USAGE / LIMIT MEM % NET IO BLOCK IO PIDS CPU TIME AVG CPU %
31+
2b56db050efb highcpu-go 99.97% 528.4kB / 16.47GB 0.00% 430B / 110B 0B / 0B 4 2m20.636994s 50.09%
32+
```
33+
34+
You should see the CPU column increase and decrease over time.
35+
36+
### Monitoring with docker top
37+
38+
You can also monitor the individual process inside the container with `docker top`:
39+
40+
```bash
41+
docker top highcpu-go
42+
```
43+
44+
You should see something like this:
45+
46+
```bash
47+
USER PID PPID %CPU ELAPSED TTY TIME COMMAND
48+
root 1 0 50.130 6m26.99324902s ? 3m14s ./highcpu-go
49+
```
50+
51+
You should see the CPU column increase and decrease over time.
52+
53+
### Monitoring with docker logs
54+
55+
You can also monitor the container's logs with `docker logs`:
56+
57+
```bash
58+
docker logs -f highcpu-go
59+
```
60+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
FROM docker.io/library/golang:1.19
2+
3+
COPY highcpu.go .
4+
5+
RUN go build -o highcpu highcpu.go
6+
7+
CMD ["./highcpu"]
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package main
2+
3+
import (
4+
"log"
5+
"math"
6+
"time"
7+
)
8+
9+
func main() {
10+
log.Println("Starting program")
11+
12+
for {
13+
14+
// Use 100% CPU for 5 seconds
15+
startTime := time.Now()
16+
log.Println("Starting CPU-intensive work")
17+
for time.Since(startTime) < 5*time.Second {
18+
// Perform a computationally expensive call:
19+
// the square root of a big number!
20+
math.Sqrt(999999999)
21+
}
22+
23+
// Use less CPU for 5 seconds
24+
startTime = time.Now()
25+
log.Println("Starting CPU-light work")
26+
for time.Since(startTime) < 5*time.Second {
27+
time.Sleep(1 * time.Second)
28+
}
29+
}
30+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
FROM docker.io/library/python:3.6-alpine
2+
3+
COPY . .
4+
5+
CMD ["python", "app.py"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import time
2+
3+
while True:
4+
# Use 100% CPU for 5 seconds
5+
start_time = time.time()
6+
while time.time() - start_time < 5:
7+
pass
8+
9+
# Use 50% CPU for 5 seconds
10+
start_time = time.time()
11+
while time.time() - start_time < 5:
12+
time.sleep(0.5)

0 commit comments

Comments
 (0)