Upload speed test - php

I'm developing an app with Laravel and VueJs, which should make upload speed tests.
To do this, I already had in my Vue component a script recording a few minutes of webCam, and upload it to the server via a Form included in axios post request.
To calculate the transfer speed, in my php Controller, I get the current timestamp, also timestamp of sending the request and the content length of my http request.
The speed should be speed = length / time
But unfortunately, the results are so far from the real speed when i did tests in some websites testSpeed because of the request time that is not correct.
In Chrome inspection through the network, i see several times, as "request sent", "waiting ttfb", "content download" and more... I think that the time corresponding to the request time sent is the "request sent", but i have not idea how to got it in my php script.
here are screenshots of my script and the result:
public function analyse(Request $request)
{
$currentTimestamp = round(microtime(true) * 1000);
$requestSentAt = round($request->server->get('REQUEST_TIME_FLOAT') * 1000);
$deliveryTime = ($currentTimestamp - $requestSentAt) / 1000;
$contentLength = $request->server->get('CONTENT_LENGTH') / 1000000;
$speed = $contentLength / $deliveryTime;
dd($speed." Mo/S");
}
Chrome debugger
result
Do you think that my logic doing this calculus is right ?
Do you have an idea or suggestion ?
I'm really blocked in this step.
I really appreciate your help in advance.
Thank you very much and have a good day .

Related

PHP execution time: How to measure using a symfony system

I am trying to send out a bigger amount of emails using PHP in a symfony 3.4 system. Now the script stops due to php time execution limit, which of course I need to work around.
My idea is to put all emails to sent in a table email_queue and then send them after they are saved in the queue.
So I am saving all these emails, displaying a page with a progress bar and making an ajax call which should send emails out up to under the time limit, report back how many it sent out (to update the progress bar) and if something is left in the queue, the scipt is called again till everything is sent.
So far so good. Now I try to measure the execution time of the ajax script part to stop sending before I reach the 30 seconds time limit.
Now for some reason the measurement is always much smaller then the 30 seconds, but before the measured execution time even reaches something like 2 seconds, the script stops due to reaching the time limit. Why is that so?
This is how I do my measurement:
$executionTimes = [];
$executionPoints = [];
$timeStart = $_SERVER['REQUEST_TIME_FLOAT'];
foreach ($mailQueue as $mail) {
// do some mail configuration
// send the email
$now = microtime(true);
$executionTime = $now - $timeStart;
$executionTimes[] = $executionTime;
$executionPoints[] = $now;
if ($executionTime >= 10) {
break;
}
}
// return data
Using $_SERVER['REQUEST_TIME_FLOAT'] I thought I'd get the real execution time including all the symfony overhead. Is that not correct?
Here is the result of one run:
{
"timeStart": 1525702394.248,
"executionPoints": [
1525702394.863065,
1525702394.866609,
1525702394.870812,
1525702394.874702,
1525702394.878718,
1525702394.882434,
1525702394.886418,
1525702394.890428,
1525702394.894365,
1525702394.899119
],
"executionTimes": [
0.6150650978088379,
0.6186091899871826,
0.622812032699585,
0.626702070236206,
0.6307179927825928,
0.6344339847564697,
0.6384181976318359,
0.6424281597137451,
0.6463651657104492,
0.6511189937591553
]
}
So the last execution measurement was under 0.7 seconds, but there the script execution stoped.
I did a lot of research already but I just can't figure out why my script is doing things it shouldn't. Any ideas would be highly appreciated.

The bwshare module and PHP scraping

I wrote a script downloading a list of pages from a website. From time to time I receive the following error (the number of seconds is variable):
The bwshare module will refuse your requests for the next 7 seconds.
You have downloaded data too rapidly.
I found when using sleep(2) in the loop, it works much better, however the time delay is too expensive.
What's the best way how to deal with this module? Should I scrape it without any delay and if the response will be similar to the above message simply use sleep for the requested number of seconds?
It all depends on how many pages you can get before the error message.
Try and measure how many pages in average you can get.
4 pages before the bwshare message is the minimum.
If you are getting the error message before reaching 4 page downloads, then il would be faster to sleep(2) after each download.
try this way... it might help u.
$requestTime = 0.1; // s/connection
foreach(/* blah */) {
$start = microtime(true);
// Do your stuff to here.. get_file_content($url) and other processing .........
if($timeTaken = microtime(true)-$start < $requestTime) {
usleep(($requestTime-$timeTaken)*1000000);
}
}
if your problem is solved then try to post your answer so that other people may also be benefited

Page Speed - Test Script response speed

I made a website recent and it's loading very slow. My Firebug Page Speed Score is 82/100. I think this is good. My website has 2 images and they have 100KB and some other small images for bullets, arrows and stuff that have not more that 50KB.
Anyway, my point is that the html is quite fast, but I have this html as a Wordpress theme and a new host (cheap one).
My question: How can I find the time for my Wordpress (or any PHP script) to echo out the page requested.
Can I know for sure if the hosting or the script that is making my page work slow?
Thank you!
time your script?
<?php
$start = microtime(true);
//your script here
$end = microtime(true);
$time = $end - $start;
echo('script took ' . $time . ' seconds to execute.');
?>
I use Fiddler for this. It is a handy tool that can accurately time the entire request and response, to and from the server.
Just open Fiddler, go to your site, select all of the requests related, and click the statistics tab.

PHP Speed Test for user connection speed without echo in current page

I am looking for a possibility to check the user connection speed. It is supposed to be saved as a cookie and javascript files as well as css files will be adapted if the speed is slow.
The possibility for testing speed i have at the moment ist the following
$kb = 512;
flush();
//
echo "<!-";
$time = explode(" ",microtime());
for($x=0;$x<$kb;$x++){
echo str_pad('', 512, '.');
flush();
}
$time_end = explode(" ",microtime());
echo "->";
$start = $time[0] + $time[1];
$finish = $time_end[0] + $time_end[1];
$deltat = $finish - $start;
return round($kb / $deltat, 3);
While it works, I do not like it to put so many characters into my code also if I echo all this I can not save the result in a cookie because there has already been an output.
Could one do something like this in a different file wor something? Do you have any solution?
Thanks in advance.
Do you have any solution?
My solution is to not bother with the speed test at all. Here's why:
You stated that the reason for the test is to determine which JS/CSS files to send. You have to keep in mind that browsers will cache these files after the first download (so long as they haven't been modified). So in effect, you are sending 256K of test data to determine if you should send, say, an additional 512K?
Just send the data and it will be cached. Unless you have MBs of JS/CSS (in which case you need a site redesign, not a speed test) the download time will be doable. Speed tests should be reserved for things such as streaming video and the like.
The only idea what i can come up is a redirect.
Measure users' speed
Redirect to index
While this isn't a nice solution it only need to measure users' speed only once so i think it's excusable.
How about using javascript to time how long it takes to load a page. Then use javascript to set the cookie.
microtime in javascript http://phpjs.org/functions/microtime:472
Using jQuery
<head>
<!-- include jquery & other html snipped -->
<script>
function microtime (get_as_float) {
// http://kevin.vanzonneveld.net
// + original by: Paulo Freitas
// * example 1: timeStamp = microtime(true);
// * results 1: timeStamp > 1000000000 && timeStamp < 2000000000
var now = new Date().getTime() / 1000;
var s = parseInt(now, 10);
return (get_as_float) ? now : (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}
function setCookie(c_name, value, expiredays) {
var exdate=new Date();
exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
}
start = microtime(true);
$(window).load(function () {
// everything finished loading
end = microtime(true);
diff = end - start;
// save in a cookie for the next 30 days
setCookie('my_speed_test_cookie', diff, 30);
});
</script>
</head>
<body>
<p>some page to test how long it loads</p>
<img src="some_image_file.png">
</body>
Some pitfalls:
- The page would need to start loading first. JQuery would need to be loaded (or you can rework the above code to avoid jQuery)
testing speed on ASCII / Latin data may not give the best result, because the characters may get compressed. Besides the high level gzip compression, Some modems / lines (if not all) have basic compression that is able to detect repeating characters and tell the other end that the next 500 are repeat of ' '. I guess it would be best to use binary data that has been compressed
The problem here is that you can't really solve this nicely, and probably not in pure PHP. The approach you've taken will make the user download (512x512) = 262 144 bytes of useless data, which is much bigger than most complete pages. If the user is on a slow connection, they may assume your site is down before the speed test is over (with 10 kB/sec, it'd take half a minute before anything interesting shows up on screen!).
You could make an AJAX request for a file of a known size and time how long that takes. The problem here is that the page needs to be already loaded for that to work, so it'd only work for subsequent pages.
You could make a "loading" page (like you see on GMail when accessing it from a slow connection) that preloads the data, with a link to the low-bandwidth version (or maybe a redirect if the loading is taking too long).
Or you could save the "start" time in the cookie and make an AJAX request when the page is done loading - that would give you the actual loading time of your page; if that's, say, over 10 seconds, you may want to switch to the low-bandwidth version.
None of these, however, will get you the speed on the very first access; and sending a big empty page up front is not a very good first impression either.
you visit the first page(maybe 100kB with all external files), a session is immeadeatly started with
$_SESSION["start_time"] = time();
when page finished loading(jQuery window load or smth:) u send a request again with time,
u compute the speed (jQueryRequestTime - $_SESSION["start_time"] / PageSize) and set another session variable, the next link he clicks then can include custom css/js approved for that
ofc this is not perfect:)
After you've determined the user's speed, send javascript to the browser to set the cookie and then do a refresh or redirect in cases where the speed is below what you'd like.
The only thing I can think of would be to subscribe to a service which offers an IP to net speed lookup. These services work by building a database of IP addresses and cataloging their registered intended use. They're not always accurate, but they do provide a starting point. Look up the user's IP address against one of these and see what it returns.
Ip2Location.com provides such a database, beginning with their DB13 product.
Of course, if your goal is a mobile version of the site, user agent sniffing is a better solution.

GUI Application becomes unresponsive while http request is being done

I have just made my first proper little desktop GUI application that basically wraps a GUI (php-gtk) interface around a SimpleTest Web Test case to make it act as a remote testing client.
Each time the local The Web Test case runs, it sends an HTTP request to another SimpleTest case (that has an XHTML interface) sitting on my server.
The application, allows me to run one local test that collates information from multiple remote tests. It just has a 'Start Test' button, 'Stop Test' button and a setting to increase/decrease the number of remote tests conducted in each HTTP Request. Each test-run takes about an hour to complete.
The trouble is, most of the time the application is making http requests. Furthermore, whenever an HTTP Request is being made, the application's GUI is unresponsive.
I have taken to making the application wait a few seconds (iterating through the Gtk::main_iteration ) between requests in order to give the user time to re-size the window, press the Stop button, etc. But, this makes the whole test run take a lot a longer than is necessary.
<?php
require_once('simpletest/web_tester.php');
class TestRemoteTestingClient extends WebTestCase
{
function testRunIterations()
{
...
$this->assertTrue($this->get($nextUrl), 'getting from pointer:'. $this->_remoteMementoPointer);
$this->assertResponse(200, "checking response for " . $nextUrl );
$this->assertText('RemoteNodeGreen');
$this->doGtkIterationsForMinNSeconds($secs);
...
}
public function doGtkIterationsForMinNSeconds($secs)
{
$this->appendStatusMessage("Waiting " . $secs);
$start = time();
$end = $start + $secs;
while( (time() < $end) )
{
$this->appendStatusMessage("Waiting " . ($end - time()));
while(gtk::events_pending()) Gtk::main_iteration();
}
}
}
Is there a way to keep the application responsive whilst, at the same time making an HTTP request?
I am considering splitting the application into two, where:
Test Controller Application - Acts as a settings-writer / report-reader and this writes to settings file and reads a report file.
Test Runner Application - Acts as a settings-reader / report-writer and, for each iteration reads the settings file, Runs the test, then write a report.
So to tell it to close down - I'd:
Press the Stop Button on the 'Test Controller Application',
which writes to the settings file,
which is read by the 'Test Runner Application'
which stops, then
writes to the report file to say it stopped
the 'Test Controller Application' reads the report and updates the status
and so on...
However, before I go ahead and split the application in two - I am wondering if there is any other obvious way to deal with, this issue. I suspect it is probably quite common and a well-trodden path.
Also is there an easier way to send messages between two applications that sit on the same machine?
You will have to implement some threading in order to manage multiple processes simultanously. That way, your app wont get unresponsive while you execute some other tasks in the background.

Categories