-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu_ram_stress.php
295 lines (263 loc) · 11.1 KB
/
cpu_ram_stress.php
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php
ini_set('max_execution_time', 0);
ini_set('display_errors', 1);
error_reporting(E_ALL);
class SystemLoadTest {
private $isRunning = false;
private $memoryData = [];
private $targetMemory;
private $cpuTarget;
private $totalMemory;
public function __construct($cpuTarget = 90, $memoryPercentage = 60, $totalMemoryGB = 1) {
$this->cpuTarget = max(1, min(100, (int)$cpuTarget));
$this->totalMemory = $totalMemoryGB * 1024 * 1024 * 1024; // Convert GB to bytes
$this->targetMemory = $this->totalMemory * ($memoryPercentage / 100);
// Set PHP memory limit slightly higher to prevent crashes
ini_set('memory_limit', ceil(($this->targetMemory * 1.1) / (1024 * 1024)) . 'M');
}
public function start() {
header('Content-Type: application/json');
ob_end_flush();
$this->isRunning = true;
try {
$this->log("Test started - Target memory: " . $this->formatBytes($this->targetMemory));
$this->initialMemoryAllocation();
while ($this->isRunning) {
$startTime = microtime(true);
$this->cpuWork($startTime);
$this->maintainMemory();
$this->reportStatus();
if (connection_aborted()) {
throw new Exception("Connection aborted by client");
}
}
} catch (Exception $e) {
$this->log("Error: " . $e->getMessage());
echo json_encode([
"status" => "error",
"message" => $e->getMessage()
]) . "\n";
}
}
private function initialMemoryAllocation() {
$chunkSize = 10 * 1024 * 1024; // 10MB chunks
while (memory_get_usage(true) < $this->targetMemory) {
$this->memoryData[] = str_repeat('A', $chunkSize);
$this->reportStatus();
usleep(10000);
}
}
private function maintainMemory() {
$currentUsage = memory_get_usage(true);
$tolerance = 0.02 * $this->targetMemory; // 2% tolerance
if ($currentUsage < ($this->targetMemory - $tolerance)) {
$this->memoryData[] = str_repeat('A', 1024 * 1024); // 1MB
} elseif ($currentUsage > ($this->targetMemory + $tolerance)) {
array_pop($this->memoryData);
}
}
private function cpuWork($startTime) {
// Calculate work duration based on CPU target percentage
$workDuration = ($this->cpuTarget / 100) * 0.1; // Scale to 100ms cycle
$endWork = $startTime + $workDuration;
while (microtime(true) < $endWork) {
for ($i = 0; $i < 2000; $i++) {
$x = sin($i) * cos($i) * tan($i) * sqrt($i);
}
}
$sleepUntil = $startTime + 0.1; // 100ms total cycle
$remainingSleep = ($sleepUntil - microtime(true)) * 1000000;
if ($remainingSleep > 0) {
usleep($remainingSleep);
}
}
private function reportStatus() {
$currentMemory = memory_get_usage(true);
$status = [
"status" => "running",
"memory_usage" => $this->formatBytes($currentMemory),
"memory_target" => $this->formatBytes($this->targetMemory),
"memory_percentage" => round(($currentMemory / $this->totalMemory) * 100, 2),
"cpu_target" => $this->cpuTarget,
"timestamp" => date('Y-m-d H:i:s')
];
echo json_encode($status) . "\n";
ob_flush();
flush();
}
private function formatBytes($bytes) {
$units = ['B', 'KB', 'MB', 'GB'];
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
return round($bytes / (1024 ** $pow), 2) . ' ' . $units[$pow];
}
private function log($message) {
echo json_encode([
"status" => "log",
"message" => $message,
"timestamp" => date('Y-m-d H:i:s')
]) . "\n";
ob_flush();
flush();
}
}
if (isset($_POST['action'])) {
if ($_POST['action'] === 'start') {
$cpuTarget = isset($_POST['cpuTarget']) ? (int)$_POST['cpuTarget'] : 90;
$memoryPercentage = isset($_POST['memoryPercentage']) ? (int)$_POST['memoryPercentage'] : 60;
$totalMemoryGB = isset($_POST['totalMemoryGB']) ? (float)$_POST['totalMemoryGB'] : 1;
$test = new SystemLoadTest($cpuTarget, $memoryPercentage, $totalMemoryGB);
$test->start();
}
exit;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>System Load Test (Configurable CPU/RAM)</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 20px auto; padding: 20px; }
.button { padding: 10px 20px; margin: 10px; cursor: pointer; border: none; border-radius: 4px; }
.button:disabled { opacity: 0.5; cursor: not-allowed; }
.start { background: #4CAF50; color: white; }
.stop { background: #f44336; color: white; }
#status { margin: 20px 0; padding: 10px; border: 1px solid #ddd; }
#console {
margin: 20px 0;
padding: 10px;
border: 1px solid #333;
background: #f8f8f8;
height: 300px;
overflow-y: scroll;
font-family: monospace;
white-space: pre-wrap;
}
.error { color: #f44336; }
.info { color: #2196F3; }
.controls {
margin: 20px 0;
padding: 15px;
border: 1px solid #ddd;
border-radius: 4px;
}
.input-group {
margin: 10px 0;
}
.input-group label {
display: inline-block;
width: 150px;
margin-right: 10px;
}
.input-group input {
padding: 5px;
width: 100px;
}
</style>
</head>
<body>
<h1>System Load Test (Configurable CPU/RAM)</h1>
<div class="controls">
<div class="input-group">
<label for="cpuTarget">CPU Target (%):</label>
<input type="number" id="cpuTarget" min="1" max="100" value="90">
</div>
<div class="input-group">
<label for="memoryPercentage">Memory Target (%):</label>
<input type="number" id="memoryPercentage" min="1" max="90" value="60">
</div>
<div class="input-group">
<label for="totalMemoryGB">Total Memory (GB):</label>
<input type="number" id="totalMemoryGB" min="1" max="128" value="1" step="0.5">
</div>
</div>
<div>
<button class="button start" onclick="startTest()">Start Test</button>
<button class="button stop" onclick="stopTest()">Stop Test</button>
</div>
<div id="status">Status: Ready</div>
<div id="console">Console Output:
</div>
<script>
let abortController = null;
function logToConsole(message, isError = false) {
const console = document.getElementById('console');
const timestamp = new Date().toLocaleTimeString();
const className = isError ? 'error' : 'info';
console.innerHTML += `<div class="${className}">[${timestamp}] ${message}</div>`;
console.scrollTop = console.scrollHeight;
}
async function startTest() {
if (abortController) {
logToConsole('Test is already running');
return;
}
// Disable inputs and start button
document.querySelectorAll('.controls input').forEach(input => input.disabled = true);
document.querySelector('.button.start').disabled = true;
const cpuTarget = document.getElementById('cpuTarget').value;
const memoryPercentage = document.getElementById('memoryPercentage').value;
const totalMemoryGB = document.getElementById('totalMemoryGB').value;
document.getElementById('status').textContent = 'Status: Starting...';
logToConsole('Starting system load test...');
try {
abortController = new AbortController();
const response = await fetch('', {
method: 'POST',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
body: `action=start&cpuTarget=${cpuTarget}&memoryPercentage=${memoryPercentage}&totalMemoryGB=${totalMemoryGB}`,
signal: abortController.signal
});
const reader = response.body.getReader();
const decoder = new TextDecoder();
while (true) {
const {value, done} = await reader.read();
if (done) break;
const text = decoder.decode(value);
const lines = text.split('\n').filter(line => line.trim());
for (const line of lines) {
try {
const data = JSON.parse(line);
if (data.status === 'error') {
logToConsole(data.message, true);
document.getElementById('status').textContent = `Error: ${data.message}`;
} else if (data.status === 'log') {
logToConsole(data.message);
} else if (data.status === 'running') {
document.getElementById('status').textContent =
`Memory: ${data.memory_usage} / ${data.memory_target} (${data.memory_percentage}%) | CPU Target: ${data.cpu_target}%`;
logToConsole(`Memory Usage: ${data.memory_percentage}% | CPU Target: ${data.cpu_target}%`);
}
} catch (e) {
logToConsole(`Parse error: ${line}`, true);
}
}
}
} catch (error) {
if (error.name === 'AbortError') {
logToConsole('Test stopped by user');
} else {
logToConsole(`Error: ${error.message}`, true);
}
} finally {
// Re-enable inputs and start button when done
document.querySelectorAll('.controls input').forEach(input => input.disabled = false);
document.querySelector('.button.start').disabled = false;
abortController = null;
}
}
async function stopTest() {
if (abortController) {
abortController.abort();
abortController = null;
logToConsole('Stopping test...');
document.getElementById('status').textContent = 'Status: Stopping...';
// Re-enable inputs and start button
document.querySelectorAll('.controls input').forEach(input => input.disabled = false);
document.querySelector('.button.start').disabled = false;
}
}
</script>
</body>
</html>