Recently I was asked to compare tests of the template engine for the
Grails framework used by us. Why do such tests are needed and what they actually say about the performance of the application - I do not know. However, it is fun and people love such tests.
Actually, Grails does not have any template engine, but there is a Groovy language that can be used within server pages (GSP - Groovy Server Pages). So, we want to measure the performance of the Groovy language. PHP is used as the base point of reference.
Because for full testing in the loaded mode, a production-server is required, this test is greatly simplified and runs without a container.
')
Equipment
The garbage server is an IBM 325, Intel® Pentium-4 2.40GHz, 1GB RAM.
Test programs
From the point of view of code, 95% of the content of a web application consists of loops, object creation, and access to the fields of an object. From these considerations, two simple programs written in the console mode are written.
(A significant part is also the output of the page content, which these tests do not take into account - this requires full testing in the container)
Test.php <?php class My { public $x; public $y; public $z; } $t1 = gettimeofday(true); $arr = array(); for($i = 0; $i < 100; $i++) { $entry = new My(); $entry->x = 'A'; $entry->y = 'B'; $entry->z = 'C'; array_push($arr, $entry); } $iterations = (int) $argv[1]; print $iterations." iterations.\n"; for($i = 0; $i < $iterations; $i++) { $x = null; foreach($arr as $entry) { $x = $entry->x.$entry->y.$entry->z; } } print "\n".(gettimeofday(true) - $t1)." sec.\n"; ?>
Test.groovy class My { def x, y, z; } def t = System.currentTimeMillis() arr = [] for(def i = 0; i < 100; i++) { def entry = new My(x:'A',y:'B',z:'C') arr << entry } def iterations = args[0] as int; println "Using ${iterations} iterations."; for(def k = 0; k < iterations; k++) { def x = null for(def entry : arr) { x = entry.x + entry.y + entry.z } } println "${(System.currentTimeMillis() - t) / 1000.0} sec.";
Soft
PHP 5.2.6 (cli) (built: May 8 2008 08:38:47)
Groovy Version: 1.6.3 JVM: Java HotSpot (TM) Server VM (build 14.0-b16, mixed mode)
results
Number of iterations | Test.php, sec. | Test.groovy, sec. |
---|
10,000 | 1.38 | 2.07 |
20.000 | 2.77 | 3.15 |
30,000 | 4.2 | 4.3 |
50.000 | 6.9 | 6.5 |
100,000 | 13.8 | 12.1 |
300.000 | 41 | 34 |
500.000 | 68 | 57 |
1,000,000 | 136 | 111 |
findings
The performance of the Groovy program improves significantly with increasing number of iterations. This is most likely due to the work of the HotSpot compiler.
Another point - you can try PHP accelerators. On this basis, I tried Zend, but it didn't really affect me. It's funny that disabling the modifiers for accessing the xyz fields sped up the PHP script.