After checking my code I found why it is taking 7 seconds to load wish is a pain..
$target_path = "uploads/";
exec("./speechdetect.sh uploads/voice.3gp > speech.results");
$myFile = "uploads/voice.3gp";
unlink($myFile);
$myFile = "voice.flac";
unlink($myFile);
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);
My script takes a voice recording and then sends it to google via speechdetect.sh. then takes the text which googled translated say to speak and then my program matches it and executes the command accordingly such as radio on.
How can I make this faster better or more efficient I really want a fast page loading time Im also using lighttpd.
P.S without this section of code my page loads in 352ms.
Also the shell code is
#!/bin/bash
sudo rm voice.flac
# FLAC encoded example
ffmpeg -i $1 voice.flac
curl \
--data-binary @voice.flac \
--header 'Content-type: audio/x-flac; rate=8000' \
'https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&pfilter=0&lang=en-GB&maxresults=1'
I guess it's the "speechdetect.sh" script that takes a lot of time. So you should try to optimize the shell script if possible. It will be slow anyhow because you have to connect remotely to google, upload the data, google needs some time to process the data and then it takes some time to send it back to you.
The factors are: bandwidth, latency, google's performance in processing the data.
The best thing you could do is to make the waiting more pleasent. Execute the script in an iframe, or load it via AJAX if possible and show some kind of loading indicator so that the user knows something is going on.
Edit:
Ok, maybe ffmpeg is the one to blame, because ffmpeg can be very slow - it loads a lot of code when started.
Try this to benchmark your script:
Measure the time the script actually consumes.
Start it from the shell like this:
time ./speechdetect.sh uploads/voice.3gp > speech.results
this should output something like:
real 0m1.005s
user 0m0.000s
sys 0m0.008s
the real part is the actual execution time (1.005 seconds in this example). For more info check out the man page
Make a simple benchmark in your php script
$target_path = "uploads/";
$time_start = microtime(true);
exec("./speechdetect.sh uploads/voice.3gp > speech.results");
$time_end = microtime(true);
$time = $time_end - $time_start;
echo "Time elapsed: " . $time . " seconds";
// ....
Get more detailed information whether the upload to google or ffmpeg consumes the time:
Modify your shell script (added time):
#!/bin/bash
sudo rm voice.flac
# FLAC encoded example
time ffmpeg -i $1 voice.flac
time curl \
--data-binary @voice.flac \
--header 'Content-type: audio/x-flac; rate=8000' \
'https://www.google.com/speech-api/v1/recognize?xjerr=1&client=chromium&pfilter=0&lang=en-GB&maxresults=1'
Run it: ./speechdetect.sh uploads/voice.3gp
(without redirecting the output)
The first time benchmark shown is the one of ffmpeg and the second one is from the call to curl