Click to See Complete Forum and Search --> : PHP 5 Stream server out performs Java 6 Sockets by 1/3!!!


JasonLester
05-25-2008, 02:50 AM
I made two very basic HTTP servers, one coded in PHP and one in Java. Their function is simple, and their code is as identical as possible. They get a html page from the local filesystem and send it to the client if the page is available, or send a 404 error if not - thats it (not trying to create a full featured server, just comparing performance). They are both functional HTTP servers tested with FF and IE7.

I used PHP's stream_* functions over the higher performing socket_* family because Java can create SSL sockets. This is a significant downfall for PHP socket_*'s, but you can create SSL sockets in PHP using the higher level stream_* family.

I ran these tests on a Linux VDS running Debian. PHP 5.2.6 and Java 1.6.0_06 (aka Java "6").
The Java server was compiled and ran using Sun's java compiler and JRE.
The PHP server was ran using the CLI with a default php.ini.

I ran 10 tests on each using a script that was made to hit the server 10,000 times. Attached is a copy of all the code so that it can be viewed and tested by others (client script and both servers)

Here is a copy of the logs (and no, I didn't switch them LOL)

PHP
10000 times in 0 minutes and 12.2015838623 seconds. <-- Highest
10000 times in 0 minutes and 10.2867138386 seconds.
10000 times in 0 minutes and 9.98223209381 seconds.
10000 times in 0 minutes and 10.389275074 seconds.
10000 times in 0 minutes and 10.8108680248 seconds.
10000 times in 0 minutes and 9.90578699112 seconds.
10000 times in 0 minutes and 10.2501511574 seconds.
10000 times in 0 minutes and 10.6506991386 seconds.
10000 times in 0 minutes and 9.31919288635 seconds.
10000 times in 0 minutes and 9.02534914017 seconds. <-- Lowest

JAVA
10000 times in 0 minutes and 15.6838850975 seconds.
10000 times in 0 minutes and 13.8331971169 seconds.
10000 times in 0 minutes and 14.7244591713 seconds.
10000 times in 0 minutes and 14.584854126 seconds.
10000 times in 0 minutes and 14.2438700199 seconds.
10000 times in 0 minutes and 14.3222107887 seconds.
10000 times in 0 minutes and 18.0508570671 seconds. <-- Highest (!!WOW!!)
10000 times in 0 minutes and 14.0483140945 seconds.
10000 times in 0 minutes and 12.6151368618 seconds.
10000 times in 0 minutes and 12.2505831718 seconds. <-- Lowest

JasonLester
05-25-2008, 03:00 AM
A weird note: when testing on XP it showed between 2% - 5% of my CPU being used, but the second you clicked anything the CPU usage would skyrocket to 95% - 99%. I noticed this just by clicking the window frame of the task manager. XP never became unstable so I don't know how accurate the CPU monitor is in this case. even weirder, test results would be around 1/4 quicker after you did something and the CPU monitor jumped. Both Java and PHP behaved the same and would see similar increases. PHP still out performed Java on XP, but I'm not sure how much I can trust the results.

My VDS is hosted remotely (not a local Vmware machine or similar) and the tests were done over ssh. One ssh session for the server, another for the client.

Also, if you do a usleep(200) in the client loop it is likely better on resources, but it takes 4 times longer. PHP was returning results between 30 and 40 seconds, and java between 40 and 50 seconds

Piranha
05-25-2008, 04:39 AM
I can't help to wonder why you do this comparison. If you want to really compare two languages I think it is better to compare all languages of choise in an environment that is as optimized as possible for the language currently being tested, and not a simple copy of another solution.

Of course, if you want to to get PHP looking better than java you did a good job. But it doesn't mean that it is, not when it is set up correctly.

JasonLester
05-25-2008, 06:59 AM
I can't help to wonder why you do this comparison. If you want to really compare two languages I think it is better to compare all languages of choise in an environment that is as optimized as possible for the language currently being tested, and not a simple copy of another solution.

Of course, if you want to to get PHP looking better than java you did a good job. But it doesn't mean that it is, not when it is set up correctly.I started the tests because I need some figures on PHP vs. Java performance, rather then the common rants that you hear. Next I'm going to fork each client thread in the PHP server and thread each one in the Java server and see how drastic of a performance difference there is.

Which environments would be optimal for these two languages? How would they effect the outcome of the servers that I wrote? More importantly, what is the significance of running these languages outside of those environments?

Piranha
05-25-2008, 12:25 PM
When I have read it again I have to say that I missed the big point in my first response: Why do you build a server yourself? Take advantage of the servers and set it accordingly in them instead. As an example, set up Apache for PHP and jboss for Java, and then compare the results. Creating your own server doesn't seem like a good idea since you will not be able to make it as good as the servers already out there.

Of course, if the whole idea is to build a server it is another story. Then I have no idea at all how to do, or even how to test and/or handle it.

Please note that I don't know if Java or PHP will be faster and that I didn't mean that java is better. Also, if you want a real comparison you should take other things into account, like database access, handling script/code on the page, traffic on the server and a lot of other things. And in the end the difference in developing time will be worth more than the cost of a bigger server. Just develop in whatever language will be easiest for you.

JasonLester
05-25-2008, 09:05 PM
When I have read it again I have to say that I missed the big point in my first response: Why do you build a server yourself? Take advantage of the servers and set it accordingly in them instead. As an example, set up Apache for PHP and jboss for Java, and then compare the results. Creating your own server doesn't seem like a good idea since you will not be able to make it as good as the servers already out there.

Of course, if the whole idea is to build a server it is another story. Then I have no idea at all how to do, or even how to test and/or handle it.

Please note that I don't know if Java or PHP will be faster and that I didn't mean that java is better. Also, if you want a real comparison you should take other things into account, like database access, handling script/code on the page, traffic on the server and a lot of other things. And in the end the difference in developing time will be worth more than the cost of a bigger server. Just develop in whatever language will be easiest for you.Apache and jboss have two downfalls in common... They are both HTTP servers :) The two servers that I made are just examples for testing purposes to see how the two fair against each other in networking functions.

I'm in the middle of coding a server for concurrent remoting, but I am running into problems because PHP is not thread safe (as in the PHP core, not just userland functionality). The server is multi threaded and each connection is given its own thread in order to maintain a fairly isolated yet concurrent environment for all of the clients, but only one thread can make/receve calls/data to/from the PHP core at once because PHP can't thread with the server.

The solution to the problem is fork()'ing PHP, but that will create completely isolated environments for each client which is not what I want. The only way I will be able to keep each client synchronized if I fork them is to incorporate some sort of IPC between the child processes (PHP SAPI/ or even PHP userland) and the parent (the server [Java]). Now that I'm in a situation that I might need to set up IPC between Java and PHP im questioning whether it would be better to remove PHP from the server altogether and have it on its own process... sort of like a FastCGI. The result would be less synchrony between the Java core and the PHP core, but with the result of more synchrony between the client threads and server.

Right now Im questioning PHP's performance when using userland functions to perform the forking/IPC rather then forking within the SAPI. This would leave PHP developers more control over the application environment. I will consider it a solid option if a can find that it can be done within a reasonable level of stability and efficiency. Efficacy inst looking bad right now. PHP isn't a bad performer once the interpreter gets initiated and the code gets compiled and interpreted, and that only happens once with a PHP daemon. The finial interpreted machine code runs in memory form that point on until the daemon is shutdown.

The reason that I used Java for the comparison is because it is another interpreted language and it is respected in network services. If it can hold its own against java I will also do some testing against C or C++ (of course, PHP will be smoked, but I would like some PHP vs. C and Java vs. C numbers to compare).

Piranha
05-26-2008, 10:03 PM
Ok, I understand it a bit better now. And I am totally lost. Your goal is to advanced for me, I have no idea how to do it. Please disregard my previous posts.

But I am still a little bit worried that you are not comparing networking functions in your test, but instead are comparing file reading functions. The problem here is that in PHP you read all the file in one read, but in java you read x characters at a time, making it possible that you use more than one file read. And you do that with a depreciated method, you should use BufferedReader (http://java.sun.com/j2se/1.5.0/docs/api/java/io/BufferedReader.html) instead. When you create it set the input buffer large enough to be able to read the whole file, that way you should get only one file read, and be able to compare the network function in a better way.

JasonLester
05-26-2008, 11:20 PM
Thanks :)

dart_phpcoder
06-13-2008, 03:22 AM
I am trying to make a multi-player game where each user gets to see almost instantaneously what the other players have done in their previous turns.
I want to implement this using socket programming, but I have been suggested by some that performance of sockets in PHP is very bad and so, I should do this in Java.
But the problem is that I am not acquainted with Java.
Please suggest to me if PHP sockets will be a good option for me.

JasonLester
06-13-2008, 04:08 PM
The performance is good. Some parts of PHP code (such as control structures) need to be completely converted into machine code. Other parts (such as extensions) are precompiled libraries usually written in C. They don't perform as well as the original libs because of the variable/function mapping that needs to be done to make them accessible to PHP, but the loss is usually small. Once a PHP script is compiled and running in memory they usually perform well, and often quicker then Java. The big question is - how is the data going to be processed once received by the server?

I'm assuming that it will basically be a type of echo server, such as one client sends the position or state of an object and the server relays the it to other clients so that they stay synchronized. PHP would be good for this. PHP also has good file and database functions, so logging is easy as well. I have heard that PHP is slower when it comes to complicated math algorithms, so if a large amount of processing needs to be done (for example, to keep track of the relationships of a large amount of objects on a map) Java might perform better. I think the difference would be minimal though.

Hopes that helps

I made a class that would take an image of the state of a phpbb users session and relay it to a different server using PHP sockets (custom load balancing ;) ). When using a UNIX socket based server PHP is able to process a 46Kb session in 0.0034 seconds. Thats receiving the request, building the image, and responding fully. If Java can do it in 0.0028 seconds then you can say it's quicker, but its still way to little of a difference for a human to notice.

JasonLester
06-13-2008, 04:24 PM
Sorry, I forgot to mention a down side... each version of PHP that I have used socket functions on have bad behavior when it comes to buffering data. Each time you call socket_read() PHP creates a buffer that isn't freed until the script stops, and with a server that should be as little as possible. With stream sockets the effect is small because it uses a shared buffer, but with the socket_read() a new buffer is allocated in memory for each read and not released. The result over time is that more and more memory is locked by old buffers, possible until the system runs out. The buffer still hangs around when using PHP stream sockets but it is reused.

swan
07-05-2008, 07:07 PM
I'm in the middle of coding a server for concurrent remoting, but I am running into problems because PHP is not thread safe
sonic server or nanoserv?