0

I am trying to bombard my site using simultaneous 50-100+ curl connections.

i have made list of URLs in array [post IDs are incremental], so no same url will be accessed twice as there is caching enabled on server.

I want to do this purely in php-curl as i am familiar with php.

i have looked on stackoverflow and found this PHP: Opening URLs concurrently to simulate a DOS attack?

but there is no curl solution to do what i am trying to do. i don't want to use 3rd party tools. as some are for windows , some are for linux, some are outdated while i am more familiar with php.

Right now my code waits for current connections to close and then start new ones. while i don't want to wait for open connections to close and just keep on starting new connections.

here is my code so far.

<?php

$my_site_url = 'https://example.com/item/post_id';

$threads = 80;

$all_post_ids = array();

//get all ids in the array 
for($int = 50; $int <= 200000 ; $int++){
    $all_post_ids[] = $int;
}
//printr($all_post_ids);


//make chunks from the big array
foreach(array_chunk($all_post_ids, $threads) as $post_ids_chunk) {

$ch = curl_multi_init();

foreach ($post_ids_chunk as $i => $each_id) {

    $url = str_replace("post_id" , $each_id , $my_site_url) ;

    echo $url."\n" ;

    $conn[$i] = curl_init($url);
    curl_setopt($conn[$i], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($conn[$i], CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($conn[$i], CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($conn[$i], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36');
    curl_setopt($conn[$i], CURLOPT_HEADER, 0);
    curl_multi_add_handle($ch, $conn[$i]);
}

do {
    $status = curl_multi_exec($ch, $active);
} while ($status === CURLM_CALL_MULTI_PERFORM || $active);

foreach ($post_ids_chunk as $i => $each_id){
    //$header[$i] = curl_getinfo($conn[$i]);
    //var_dump( $header[$i]);
    //echo $header[$i]['http_code'];
    //$received_data[$i] = curl_multi_getcontent($conn[$i]);
    curl_multi_remove_handle($ch, $conn[$i]);
}

curl_multi_close($ch);

}

So how can i not wait open connections to close and start new ones lets say per second or as soon as possible.

What i am missing here ?

Thanks for your time.

  • I don't want to be that guy that tells you to try B when you want A but here goes. You might want to try other solutions specifically targeted for load testing. 2 that comes to mind is Apache Benchmark (ab), and Siege (https://github.com/JoeDog/siege). Both are command line tools that are easy to use: https://www.petefreitag.com/item/689.cfm https://drupalize.me/blog/201507/load-testing-your-site-siege – Kyle Domingo Mar 15 '18 at 10:37
  • @KyleDomingo there are other reasons for using php, this is not just one time job, its regular job and i want to automate it, i have 20 websites which i want to DOS daily and do some calculations on timings. so i want full control over app which php and my code gives me to automate things. Thanks –  Mar 15 '18 at 13:05
  • You can use the tools I recommended, pipe their output into an outfile, then parse it using PHP? – Kyle Domingo Mar 16 '18 at 03:51
  • @KyleDomingo i have caching enabled, how can i use different urls ? i mean incremental post ids , Thanks –  Mar 16 '18 at 05:13
  • @KyleDomingo okie., Siege can take urls file as input that is good. –  Mar 16 '18 at 05:22

0 Answers0