Created
December 10, 2023 13:35
-
-
Save crispy-computing-machine/5fc8563172a1a6fece3338992755e225 to your computer and use it in GitHub Desktop.
PHP Curl Load Test
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
<?php | |
// Define the URL of the website you want to test. | |
$targetUrl = 'https://example.com'; | |
// Define the number of concurrent requests you want to make. | |
$concurrentRequests = 10; | |
// Create an array to store cURL handles. | |
$handles = array(); | |
// Initialize cURL sessions. | |
for ($i = 0; $i < $concurrentRequests; $i++) { | |
$ch = curl_init(); | |
curl_setopt($ch, CURLOPT_URL, $targetUrl); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$handles[$i] = $ch; | |
} | |
// Create a cURL multi handle to execute multiple requests concurrently. | |
$multiHandle = curl_multi_init(); | |
// Add the cURL handles to the multi handle. | |
foreach ($handles as $ch) { | |
curl_multi_add_handle($multiHandle, $ch); | |
} | |
// Execute the multi handle to perform the requests concurrently. | |
do { | |
curl_multi_exec($multiHandle, $running); | |
} while ($running > 0); | |
// Process the responses and close the handles. | |
foreach ($handles as $ch) { | |
$response = curl_multi_getcontent($ch); | |
// You can process the response as needed or check for errors. | |
// For a basic load test, you can log the response or measure response times. | |
// For example: echo "Response: " . $response; | |
curl_multi_remove_handle($multiHandle, $ch); | |
curl_close($ch); | |
} | |
// Close the multi handle. | |
curl_multi_close($multiHandle); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment