0

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'
j0k
  • 22,600
  • 28
  • 79
  • 90
Jeremy
  • 447
  • 1
  • 4
  • 12

3 Answers3

2

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

sled
  • 14,525
  • 3
  • 42
  • 70
  • The server is connected via ethernet 100mb cable. The file is small so procesing should not be an issue for google as the file is 4397 bytes. – Jeremy Feb 09 '13 at 12:25
  • Great answer. You were right. Here are my results: http://pastebin.com/h93R2VWc I cant see how it would take so long :/ edit that cant be right its like 7 seconds per each result :SS – Jeremy Feb 11 '13 at 15:28
  • hi, 2-3 secs are consumed by the shell script, the missing 4-5 seconds maybe related to the way how php's exec function works. Have a look at this [question](http://stackoverflow.com/questions/13426541/exec-runs-slow-in-php-same-command-runs-much-faster-interactively) one way to test it, is to create a shell script that does nothing and measure the time php needs to execute it using exec. By the way, the slowest part in your shell script is the way to google and back via curl ;) – sled Feb 12 '13 at 00:57
  • by the way, do you run the php script via apache? if yes, could you try to run the php script from the pure shell using `php myscript.php`? If it makes a difference (i.e it's a problem with apache and mod_php) I might have a solution for your problem ;) – sled Feb 12 '13 at 01:32
  • I dont understand why you think that question would help :S @sled. I have tried via php shell command but no different! How can I speed up the upload to google? any ideas :P? – Jeremy Feb 12 '13 at 20:29
  • most probably you can't speed up the upload to google, what you could try (although I don't think it will be much faster) is to use curl from php: [docs](http://php.net/manual/en/book.curl.php). I think it's the `exec(..)` call itself that adds the 4-5 seconds overhead to the script. The upload only takes about 2 seconds... ;) Another solution might be to use a worker script, that is constantly running and waiting for jobs to be processed. Have a look at [gearman](http://gearman.org/). – sled Feb 13 '13 at 07:29
  • Confused by gearman Not sure what it really does and how it would help :S sorry :P do you have skype? will look at worker scripts @sled – Jeremy Feb 13 '13 at 19:57
1

Your best bet is to find some tool to do speech detection locally. You probably can't speed up your connection to google or change how fast the google engine works.

alexg
  • 3,015
  • 3
  • 23
  • 36
  • The file it is uploading is 4397 bytes. It is pretty fast at sending the file to the server which then uploads it (Which is also fast I think 2-3 seconds?) – Jeremy Feb 09 '13 at 12:24
0

This depends on how big the file is and how fast your upload connection is. It probably won't go any faster.

silkfire
  • 24,585
  • 15
  • 82
  • 105