Solved

Windows Server 2003 - PHP - shell_exec runs my Application 10 times slower than manually!

Posted on 2008-10-24
17
486 Views
Last Modified: 2011-10-03
I have an EXE that I'm invoking through shell_exec() in PHP.

It takes 2.5 seconds to run whereas it only takes 0.25 seconds to run if I just double click the exe on the same server.

And this 2.5 seconds is not initialization or destruction - this is a custom timer I coded into the EXE that operates during runtime.

Note: The EXE runs as the internet guest account when invoked by PHP and as administrator when i double click
Note: The EXE contains logic to download a file from the internet (www.google.com actually for testing purposes)

Can I narrow the gap and make it run faster?

Thanks.

0
Comment
Question by:hamlin11
  • 8
  • 4
  • 3
  • +1
17 Comments
 

Author Comment

by:hamlin11
Comment Utility
I added in a for loop that goes through a billion times and performs some arithmetic...

The "manual" run is 3.5 seconds... the "php" run is 6.0


0
 
LVL 13

Assisted Solution

by:Xyptilon2
Xyptilon2 earned 150 total points
Comment Utility
I imagine running manually results in a higher priority, where as running it from PHP results in a lower priority (for the thread). (just guessing). But i dont think it is possible to modify this under windows.. if under linux you could try prefixing the command with "nice" and set a value. But the problem is, to change the priority of a thread, you need to be super user. And the user that is running scripts, probably is not.

0
 
LVL 25

Assisted Solution

by:Squinky
Squinky earned 150 total points
Comment Utility
I think cpu priority is unlikely to produce such a big discrepancy unless the server is very heavily loaded. I might suspect permissions - is it waiting for something and then timing out?

That said, 3.5s vs 6 sec is not such a big difference? Either way, that's still not very long for a billion iterations, and PHP certainly adds some overhead.
0
 

Author Comment

by:hamlin11
Comment Utility
Well, it's approaching a 50% slowdown doing pure arithmetic. I was just hoping there was some way around it on a windows 2003 server.

Anymore thoughts?
0
 
LVL 39

Assisted Solution

by:Roger Baklund
Roger Baklund earned 200 total points
Comment Utility
You say 50%... have you tried anything that runs a little longer, say 30 seconds?

From the numbers provided in this thread, it seems to be a slowdown of 2.25-2.5 seconds. (2.5-0.25=2.25, 6-3.5=2.5)

shell_exec() works by invoking a shell (cmd.exe on windows), and then executes the program from within that shell. Loading and setting up the shell will take some time, but not 2.5 sec. I did the test below, and on the first run I got 0.48 sec, but all consecutive runs was in the 0.02-0.07 range, mostly close to 0.02.
function get_microtime() {

   list($usec, $sec) = explode(" ", microtime());

   return ((float)$usec + (float)$sec);

}
 

$start = get_microtime();

shell_exec('echo hello > hello.txt');

echo 'time elapsed: '.(get_microtime()-$start);

Open in new window

0
 
LVL 13

Assisted Solution

by:Xyptilon2
Xyptilon2 earned 150 total points
Comment Utility
Just a question...is it just downloading a file / information ? why not use CURL for that and keep everything inside php ? that would be a lot faster
0
 
LVL 25

Assisted Solution

by:Squinky
Squinky earned 150 total points
Comment Utility
cxr, have you seen microtime(true)? Saves all that string wrangling.
0
 

Author Comment

by:hamlin11
Comment Utility
The timer starts right before the simple arithmetic loop starts, and the timer stops as soon as that loop completes. I did this purposely so that we could ignore any initialization or deconstruction that must occur (e.g., loading/unloading libraries).

It does not download any data at this time - although my main program does. It's complex enough that it does not make sense to move it to PHP or ASP. I might make it a windows service when it is complete though.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 13

Assisted Solution

by:Xyptilon2
Xyptilon2 earned 150 total points
Comment Utility
...do you have the same discrepancy when running it on other machines?
0
 

Author Comment

by:hamlin11
Comment Utility
I do not have another web server to test on, unfortunately
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 200 total points
Comment Utility
Squinky, I work mostly with PHP4 code, so I didn't even think about it :) but I have seen it, thx.

hamlin11, does this 50% slowdown happen also with the "real" application, or only with your simple arithmetic loop/test code? And does the PHP code include local database activity? I ask because if it's a simple loop and you have database activity, the difference between a user invoked execution and a PHP invoked execution could be relatively large. When you run it trough PHP, the web server and the database server is using a lot of cpu resources allready, when you call shell_exec. When you invoke a program trough the desktop, it is a prioritized task to execute it. I don't know the details here, I'm not a windows expert, but I think the difference would be larger on a "personal" version of windows, compared to a "windows server" version of windows. Anyways, if you have an idle dual core computer start executing a loop counting to a billion, it would be a lot faster than doing the same thing while both the web server service and the database server service on that computer is busy.
0
 

Author Comment

by:hamlin11
Comment Utility
cxr, thanks for the reply. It's a dualcore processor on the server and there is approx. 0 database or php processing activity occurring. The only processing that I can conceive of that may be occuring is whatever loop the php processing logic must be in waiting on the exe invoked by shell_exec to exit.


0
 
LVL 25

Assisted Solution

by:Squinky
Squinky earned 150 total points
Comment Utility
What about running your app via a PHP command line? That way you'd eliminate PHP itself as the culprit - it may be your web server that is at fault.
0
 

Author Comment

by:hamlin11
Comment Utility
How do I do that?
0
 
LVL 25

Expert Comment

by:Squinky
Comment Utility
Have you even tried reading some docs?

Open a command shell, type 'php myscript.php' and hit return. If PHP is set up correctly on your machine, it will run the script directly, without any involvement from the web server. You'll need to cut your test script down to only do non-web related things (e.g. anything that relates to web-dependent super globals such as $_GET).
0
 

Author Comment

by:hamlin11
Comment Utility
I've spent hours reading docs.

I'll give that a shot - thanks for the tip.
0
 

Author Comment

by:hamlin11
Comment Utility
I'm sure at this point that it's not a PHP thing. It's a windows thing. There's lots of overhead when you create processes threads under a user who is not logged in via remote desktop or anything. Doesn't matter if it's an admin or a guest.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now