Solved

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

Posted on 2008-10-24
17
497 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 3
  • +1
17 Comments
 

Author Comment

by:hamlin11
ID: 22800175
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
ID: 22802276
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:Marcus Bointon
Marcus Bointon earned 150 total points
ID: 22802741
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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 

Author Comment

by:hamlin11
ID: 22804158
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
ID: 22804485
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
ID: 22804505
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:Marcus Bointon
Marcus Bointon earned 150 total points
ID: 22804712
cxr, have you seen microtime(true)? Saves all that string wrangling.
0
 

Author Comment

by:hamlin11
ID: 22804804
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
 
LVL 13

Assisted Solution

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

Author Comment

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

Accepted Solution

by:
Roger Baklund earned 200 total points
ID: 22805076
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
ID: 22805271
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:Marcus Bointon
Marcus Bointon earned 150 total points
ID: 22807803
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
ID: 22809581
How do I do that?
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 22809678
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
ID: 22809710
I've spent hours reading docs.

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

Author Comment

by:hamlin11
ID: 22817959
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick step-by-step overview of installing and configuring Carbonite Server Backup.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

717 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