Saturday, September 10, 2016

Issue while doing Unit Testing for Code Performance

Leave a Comment

I was testing my code to check how it will behave if we think that 100 users submitted their registration all at once !!!

My code is in PHP Laravel 5.2 and JQuery Ajax is below.

for (i = 0; i < 100; i++) {     var       data={'Role' : "Role"+i},      request = $.ajax({         url:                'http://localhost:1234/Practise/public/api/SaveRoleApi',         type:               "POST",         data:               JSON.stringify(data),         contentType:        "application/json; charset=utf-8",         async:  true,         success: function(d){ console.log(d); }      }); } 

Out of 100, I am not successful in submitting more then 88 records.

I am using MySQL Database.

if my above code will add records sequentially...id there any way to test 1000 concurrent requests from one computer?

4 Answers

Answers 1

Attempting multiple requests from one browser using JavaScript to create all the connections is not a good idea, you are indeed not testing concurrency very well.

Consider using an actual load testing tool like JMeter (I definitely recommend this), or at least parallel curl requests in a batch script.

for n in {1..1000}; do     for i in `eval echo {$n..$((n+999))}`; do             echo "club $i..."             curl -X POST -H "Content-Type: application/json" -d '{"param1":"xyz","param2":"xyz"}' -s "http://localhost:1234/Practise/public/api/SaveRoleApi" >> log.txt     done &     wait done 

Answers 2

I would propose to use a tool specific for this purpose like loader. Keep in mind that your web app should be accessible from the outer world.

Answers 3

You might want to look into using PHP curl's multi functionality. http://php.net/manual/en/function.curl-multi-init.php

$mh = curl_multi_init(); $ch = []; for ($i = 1; $i < 100; $i++) {     $data = "Role=Role$i";     $ch[$i] = curl_init();     curl_setopt($ch[$i], CURLOPT_URL, 'http://localhost:1234/Practise/public/api/SaveRoleApi');     curl_setopt($ch[$i], CURLOPT_POST, 1); // Number of post fields, in this case just one.     curl_steopt($ch[$i], CURLOPT_POSTFIELDS, $data);     curl_multi_add_handle($mh, $ch[$i]); } $active = null; do {     $mrc = curl_multi_exec($mh, $active); } while ($mrc = CURLM_CALL_MULTI_PERFORM); while ($active and $mrc == CURLM_OK) {     if(curl_multi_select($mh) !== -1) {         do {             $mrc = curl_multi_exec($mh, $active);         } while ($mrc == CURLM_CALL_MULTI_PERFORM);     } } for($i = 0; $i < 100; $i++){     curl_multi_remove_handle($mh, $ch[$i]); } curl_multi_close($mh); 

Answers 4

Maybe this is not your answer but i recommend don't test it by java. check your performance by php. insert this line at start:

$mt1 = microtime(true); 

and insert this at the end of the codes:

$mt2 = microtime(true); echo 'running time: '.($mt2-$mt1).' - memory usage: '.memory_get_usage(); 

now you have running time and memory usage.

you don't need to change your entire code just for testing. at start of your page just set whatever you want to post. like

$_POST['something'] = 'someting'; 

if $_POST needs to set something new every time, generate it by rand() function.

If you are concern about code performance, I highly recommend try to minimize running time and memory usage of your codes in single time request.

If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment