I ran a quick benchmark to test them out. As always, a benchmark is simply a benchmark, you will need to look at your own situation and your own use case.
The Setup
I took a very simple database which has one table with one row, defined as the following:
create table test_table(id int, mytext varchar(50));
create index indexTest on test_table (id);
I populated the table with 10,000 entries. We will test 2 things. First, we will test how quickly we can connect to the database and retrieve one value value. Second, we will test how long it takes to iterate over a larger result set.
Tomcat was used as the java application server, and apache2 with mod_php prefork was used for PHP. Linux was used and the database was on the same server as the app servers. I did not measure the time for the http request, but the only the time it took to perform the database operations. The database was warmed up for each test.
1) Connection Speed
In this first test I tested the connection speed, closing the connection after each iteration. Here are the averaged results.
Java (no connection pool) = 6 ms to connect to MySQL
PHP = .46 ms to connect to MySQL
PHP is the clear winner here. Compared to PHP, Java is just embarrassingly slow at creating a fresh database connection.
While Java is slow at creating a fresh connection every time, one rarely does this with Java. Instead, Java typically uses connection pools. Connection pools allow database connections to be managed by a pool and shared among requests. This prevents having to frequently open and destroy connections. Using the Tomcat JDBC connection pool, Java is about 4 times faster than PHP.
Java Connection Pool = .14 ms per connect
2) Iterating over a Result Set
In this next test, we want to see how Java and PHP compare while iterating over a result set. 1000 entries were selected, with each row extracted from the result set, like in the following:
Java
String sql = "select * from test_table where id>0 AND id <1000";
ResultSet rs = stmt.executeQuery(sql);
while (rs.next()) {
String s = rs.getString("mytext");
}
PHP
$sql ="select * from test_table where id>0 AND id <1000";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result)) {
$s=$row["mytext"];
}
Here are the averaged results:
Java = 2.3 ms to iterate over 1000 entries
PHP = 4.9 ms to iterate over 1000 entries
So, Java is almost twice as fast at this test: faster but not too much faster.
Note about Persistent Connections and Thread Pools
While I did run the above test with a Java connection pool, I did not re-run the PHP test with PHP's persistence connection. A persistent connection would likely have matched Java's speed, however I chose not to do this because it is often not recommended to use persistent connections in PHP. Persistent connections are often not the standard way of connecting in PHP, however connection pools are standard way of connecting in Java. (for a larger discussion on persistent connections see this and this). So, I wanted to compare the standard approaches in each respective language.
Moreover, while persistent connections are similar to Java's connection pools, they are not the same thing. In PHP, a persistent connection is created for the apache process and re-used for each subsequent request on that process. In this case, each apache process would have its own persistance connection. So, if you have 50 apache processes, then you have 50 open connections. That connection will stay open even if that process is not even using the database.
However, with Java connection pools, the number of open connections is not dependent on the number of request threads. Instead, the pool starts with a small number of connections and grows and shrinks according to demand. A request thread will obtain a connection from the pool only when needed. Thus, you often need far fewer database connections than request threads. Under low load, the pool will shrink to one maintained connection (if so configured).
Concluding thoughts
This test demonstrates that Java is a bit faster than PHP for establishing connections and iterating over results. So which language/platform is better for MySQL?
If the database on the same computer as the server, I would say that both languages are about equal. Java is faster: 4 times faster creating connections and about 2 times faster iterating over a result set. But, that is not drastically faster and will only occupy a small fraction of the entire database call and and even smaller fraction of and entire page request.
If the database is on a different computer then the server, then you will likely want to keep the connections alive between requests to avoid the overhead of establishing a remote connection. Here Java's connection pooling has some real advantages over PHP's persistent connections, because the thread pool allows the connection pool to grow and shrink according to demand. However, this concept was not benchmarked in this post, so I will leave this for a future discussion.
No comments:
Post a Comment