Skip to content

Commit ca8cd67

Browse files
committed
memory support
1 parent 89ed125 commit ca8cd67

File tree

4 files changed

+140
-10
lines changed

4 files changed

+140
-10
lines changed

README.md

+47-8
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ To use the `k8s-pod-cpu-stressor`, you need to have the following installed:
4646
go build -o cpu-stress .
4747
```
4848

49+
### Applying Sample Manifests
50+
51+
To quickly apply the sample Kubernetes manifests, navigate to the `k8s-manifests` folder and run the following command:
52+
53+
```shell
54+
kubectl apply -f deployment.yaml
55+
kubectl apply -f job.yaml
56+
```
57+
4958
## Running with Docker
5059

5160
Build the Docker image using the provided Dockerfile:
@@ -54,13 +63,13 @@ Build the Docker image using the provided Dockerfile:
5463
docker build -t k8s-pod-cpu-stressor .
5564
```
5665

57-
Run the Docker container, specifying the desired CPU usage, stress duration, and optionally whether to run CPU stress indefinitely:
66+
Run the Docker container, specifying the desired CPU usage, stress duration, memory usage, memory stress duration, and optionally whether to run CPU and memory stress indefinitely:
5867

5968
```shell
60-
docker run --rm k8s-pod-cpu-stressor -cpu=0.2 -duration=10s -forever
69+
docker run --rm k8s-pod-cpu-stressor -cpu=0.2 -duration=10s -forever -memory=0.2 -memduration=10s -memforever
6170
```
6271

63-
Replace `0.2` and `10s` with the desired CPU usage (fraction) and duration, respectively. Add `-forever` flag to run CPU stress indefinitely.
72+
Replace `0.2` and `10s` with the desired CPU usage (fraction), duration, memory usage (fraction), and memory stress duration, respectively. Add `-forever` and `-memforever` flags to run CPU and memory stress indefinitely.
6473

6574
## CPU Usage and Duration
6675

@@ -72,14 +81,24 @@ The `k8s-pod-cpu-stressor` allows you to specify the desired CPU usage and stres
7281

7382
- **Run Indefinitely**: To run CPU stress indefinitely, include the `-forever` flag.
7483

75-
Adjust these parameters according to your requirements to simulate different CPU load scenarios.
84+
## Memory Usage and Duration
85+
86+
The `k8s-pod-cpu-stressor` also allows you to specify the desired memory usage and stress duration using the following parameters:
87+
88+
- **Memory Usage**: The memory usage is defined as a fraction of memory resources. It is specified using the `-memory` argument. For example, `-memory=0.2` represents a memory usage of 20%.
89+
90+
- **Memory Stress Duration**: The memory stress duration defines how long the memory stress operation should run. It is specified using the `-memduration` argument, which accepts a duration value with a unit. Supported units include seconds (s), minutes (m), hours (h), and days (d). For example, `-memduration=10s` represents a memory stress duration of 10 seconds, `-memduration=5m` represents 5 minutes, `-memduration=2h` represents 2 hours, and `-memduration=1d` represents 1 day.
91+
92+
- **Run Memory Stress Indefinitely**: To run memory stress indefinitely, include the `-memforever` flag.
93+
94+
Adjust these parameters according to your requirements to simulate different CPU and memory load scenarios.
7695

7796
### Kubernetes Resource Requests and Limits
7897

79-
It is recommended to specify Kubernetes resource requests and limits to control the amount of CPU resources consumed by the pod, and to prevent overloading your cluster. For example:
98+
It is recommended to specify Kubernetes resource requests and limits to control the amount of CPU and memory resources consumed by the pod, and to prevent overloading your cluster. For example:
8099

81-
- **Requests**: This defines the minimum amount of CPU that the pod is guaranteed to have.
82-
- **Limits**: This defines the maximum amount of CPU that the pod can use.
100+
- **Requests**: This defines the minimum amount of CPU and memory that the pod is guaranteed to have.
101+
- **Limits**: This defines the maximum amount of CPU and memory that the pod can use.
83102

84103
Adding requests and limits helps Kubernetes manage resources efficiently and ensures that your cluster remains stable during stress testing.
85104

@@ -89,8 +108,10 @@ Example:
89108
resources:
90109
requests:
91110
cpu: "100m"
111+
memory: "128Mi"
92112
limits:
93113
cpu: "200m"
114+
memory: "256Mi"
94115
```
95116
96117
## Check the Public Docker Image
@@ -127,16 +148,21 @@ spec:
127148
- "-cpu=0.2"
128149
- "-duration=10s"
129150
- "-forever"
151+
- "-memory=0.2"
152+
- "-memduration=10s"
153+
- "-memforever"
130154
resources:
131155
limits:
132156
cpu: "200m"
157+
memory: "256Mi"
133158
requests:
134159
cpu: "100m"
160+
memory: "128Mi"
135161
```
136162

137163
## Sample Job Manifest
138164

139-
If you want to run the CPU stressor for a fixed duration as a one-time job, you can use the following Kubernetes Job manifest:
165+
If you want to run the CPU and memory stressor for a fixed duration as a one-time job, you can use the following Kubernetes Job manifest:
140166

141167
```yaml
142168
apiVersion: batch/v1
@@ -155,17 +181,30 @@ spec:
155181
args:
156182
- "-cpu=0.5"
157183
- "-duration=5m"
184+
- "-memory=0.5"
185+
- "-memduration=5m"
158186
resources:
159187
limits:
160188
cpu: "500m"
189+
memory: "512Mi"
161190
requests:
162191
cpu: "250m"
192+
memory: "256Mi"
163193
restartPolicy: Never
164194
backoffLimit: 3
165195
```
166196

167197
This manifest runs the `k8s-pod-cpu-stressor` as a Kubernetes Job, which will execute the stress test once for 5 minutes and then stop. The `backoffLimit` specifies the number of retries if the job fails.
168198

199+
## Sample Kubernetes Manifests
200+
201+
You can find sample Kubernetes manifests in the `k8s-manifests` folder. These manifests include deployment and job configurations for the `k8s-pod-cpu-stressor`. To quickly apply these manifests, navigate to the `k8s-manifests` folder and run the following command:
202+
203+
```shell
204+
kubectl apply -f deployment.yaml
205+
kubectl apply -f job.yaml
206+
```
207+
169208
## Contributing
170209

171210
Contributions are welcome! If you find a bug or have a suggestion, please open an issue or submit a pull request. For major changes, please discuss them first in the issue tracker.

k8s-manifests/deployment.yaml

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: cpu-stressor-deployment
5+
spec:
6+
replicas: 1
7+
selector:
8+
matchLabels:
9+
app: cpu-stressor
10+
template:
11+
metadata:
12+
labels:
13+
app: cpu-stressor
14+
spec:
15+
containers:
16+
- name: cpu-stressor
17+
image: narmidm/k8s-pod-cpu-stressor:latest
18+
args:
19+
- "-cpu=0.2"
20+
- "-duration=10s"
21+
- "-forever"
22+
- "-memory=0.2"
23+
- "-memduration=10s"
24+
- "-memforever"
25+
resources:
26+
limits:
27+
cpu: "200m"
28+
memory: "256Mi"
29+
requests:
30+
cpu: "100m"
31+
memory: "128Mi"

k8s-manifests/job.yaml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
apiVersion: batch/v1
2+
kind: Job
3+
metadata:
4+
name: cpu-stressor-job
5+
spec:
6+
template:
7+
metadata:
8+
labels:
9+
app: cpu-stressor
10+
spec:
11+
containers:
12+
- name: cpu-stressor
13+
image: narmidm/k8s-pod-cpu-stressor:latest
14+
args:
15+
- "-cpu=0.5"
16+
- "-duration=5m"
17+
- "-memory=0.5"
18+
- "-memduration=5m"
19+
resources:
20+
limits:
21+
cpu: "500m"
22+
memory: "512Mi"
23+
requests:
24+
cpu: "250m"
25+
memory: "256Mi"
26+
restartPolicy: Never
27+
backoffLimit: 3

main.go

+35-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ func main() {
1515
cpuUsagePtr := flag.Float64("cpu", 0.2, "CPU usage as a fraction (e.g., 0.2 for 20% CPU usage)")
1616
durationPtr := flag.Duration("duration", 10*time.Second, "Duration for the CPU stress (e.g., 10s)")
1717
runForeverPtr := flag.Bool("forever", false, "Run CPU stress indefinitely")
18+
memoryUsagePtr := flag.Float64("memory", 0.2, "Memory usage as a fraction (e.g., 0.2 for 20% memory usage)")
19+
memDurationPtr := flag.Duration("memduration", 10*time.Second, "Duration for the memory stress (e.g., 10s)")
20+
memForeverPtr := flag.Bool("memforever", false, "Run memory stress indefinitely")
1821
flag.Parse()
1922

2023
numCPU := runtime.NumCPU()
@@ -60,10 +63,31 @@ func main() {
6063
}()
6164
}
6265

66+
// Memory stress logic
67+
go func() {
68+
memUsage := int(float64(runtime.MemStats.Sys) * (*memoryUsagePtr))
69+
memStress := make([]byte, memUsage)
70+
for i := range memStress {
71+
memStress[i] = byte(rand.Intn(256))
72+
}
73+
74+
for {
75+
if atomic.LoadInt32(&stopFlag) == 1 {
76+
return
77+
}
78+
79+
// Simulate memory usage
80+
_ = memStress[rand.Intn(len(memStress))]
81+
82+
// Sleep for a short duration to simulate memory stress
83+
time.Sleep(10 * time.Millisecond)
84+
}
85+
}()
86+
6387
go func() {
6488
// Wait for termination signal
6589
<-quit
66-
fmt.Println("\nTermination signal received. Stopping CPU stress...")
90+
fmt.Println("\nTermination signal received. Stopping CPU and memory stress...")
6791
atomic.StoreInt32(&stopFlag, 1)
6892
close(done)
6993
}()
@@ -77,7 +101,16 @@ func main() {
77101
select {}
78102
}
79103

104+
if !*memForeverPtr {
105+
time.Sleep(*memDurationPtr)
106+
fmt.Println("\nMemory stress completed.")
107+
atomic.StoreInt32(&stopFlag, 1)
108+
close(done)
109+
// Keep the process running to prevent the pod from restarting
110+
select {}
111+
}
112+
80113
// Run stress indefinitely
81-
fmt.Println("CPU stress will run indefinitely. Press Ctrl+C to stop.")
114+
fmt.Println("CPU and memory stress will run indefinitely. Press Ctrl+C to stop.")
82115
<-done
83116
}

0 commit comments

Comments
 (0)