Jul
12

PHP in enterprise applications. Comparison of PHP5 Frameworks.

By rzelazko  //  Apache, Lamp, Linux, PHP  //  No Comments

PHP5 Enterprise is common name for large, heavily loaded applications, visited by so huge amount of users who can not by handled by a single server. This large applications usually use open source libraries aims to alleviate the overhead associated with common activities performed in Web development. In PHP5, there are several sets of closely related libraries and frameworks.

This post describe speed and load tests of three popular PHP frameworks: Symfony Framework, Zend Framework and cakePHP. Benchmarking Web applications is not easy task and can be done in several ways. Below is a summary index of performed benchmark:

  1. Introduction
  2. Testing environment presentation
  3. Controversies and inconsistencies
  4. Description of the compared applications – engine performance – simple website.
  5. Description of the compared applications – feature rich website
  6. To be continued…

Introduction

PHP is open source programming language, very popular in WWW environment. Due to this popularity is it offered by a large number hosting providers on virtual accounts. Many people, therefore, believes that this language can be applied only for small, simple websites.

Although PHP in most cases is used for small websites it works well as technology, which supports enterprise-class applications. Example enterprise applications using PHP: Facebook, Wikipedia, Zend.

Enterprise environments characterized by the fact that the final functionality can be achieved by combining the functionality of stand-alone applications. These independent applications can be written by other developers working within a single organization but can also be written by an external supplier. The technology this applications can be different from the base system and communicate via SOAP or REST API.

Key role of enterprise applications plays performance. This applications often have to deal with handling heavy traffic and server load. Performance of engine – application framework has a significant impact on the amount needed to purchase and maintain servers.

The purpose of this post is to test the most popular frameworks for PHP5. Frameworks will be tested for speed of response to the browser request at high and low server load. I’ll check the speed of code generation depending on the complexity of applications and the amount of requests to heavily loaded server.

Testing environment presentation

There are several frameworks in PHP. I think most popular are Zend, Symfony and cakePHP. I will check speed and server load on this three frameworks:

  • Symfony Framework 1.2.1
  • CakePHP 1.2.0.7962
  • Zend Framework 1.7

I will check all selected frameworks on two different application types:

  • small application (one static html generated) – checking framework engine
  • large application (connection to database, search full text engine – Sphinx, pagination, etc)

Controversies and inconsistencies associated with the benchmarking process

Framework comparison is very difficult. Its because they are elastic and we can solve problem with several ways. Each tested library we can also compare:

  • to itself – we can apply different variants of one problem. For example with Symfony we can use three drivers connecting to MySQL database: PDO_MYSQL, MySQLi and PHP MySQL Native. Each driver is wrapped with two ways of DAO (Database Abstraction Object): Propel and Doctrine. We could compare application to itself using different database access methods.
  • Different versions of the same framework. Each described framework is available from few years. For example Zend Framework released publicly with version 0.2b at beginning of 2005. From this time Zend released several versions of its framework up to current (while writing this article) version 1.7. Each new version adds new features and improvements or delete old unnecessary things. We can compare each framework to its other version.
  • Cache. It is a key role in the final stage of each project, enterprise-class. There are a lot of systems and methods for caching and acceleration of the code for example: Advanced PHP Cache, Memcache, Zend Optimizer and many others. All framework implements at least some of this caching systems.
  • Routing – is one of the main components of each framework. Good frameworks provide several types of routing rules. Because it is a component used in the execution of each request it also has a key influence on the performance of the entire application.
  • Views – are an integral part of each project. Their code – usually HTML or XHTML can be obtained by using different templates systems like Smarty, PHPtal or an internal system of each the frameworks. These systems differ in the way of code generation, sometimes using an pre-compiled code and the quantity, quality, performance and helper functions.

Additional difficulties in testing the various frameworks together causing inability to pass an identical behavior between them. An example could be used in Symfony the Component. Transfer it to the CakePHP framework requires a combination of an Element helper with RequestAction method.

Another element which has a considerable impact on application performance is the number of queries performed by the framework in order to extract specific information from it. In this area, each Framework has its own (sometimes more) the methodology associated
element model of the MVC pattern. And so to retrieve a random record from the same database using the same technique may be associated with two, three or four queries a database (eg, part of which will be only information about the structure of tables used by the library using the database).

Depending on the chosen solution, the method of its verification as well as random loads associated with intermittent network server or test machine, which carried out tests obtained results may vary.

Description of the compared applications – engine performance – simple website.

Framework engine performance - simple application

The first type of application is very simple website. It does not contain subpages, the database connection or automatically activated sessions. This page contains the XHTML code that displays a few paragraphs of text, the CSS style sheet that defines their appearance, and four pictures. Screenshot of the first type of application is shown in the picture.

The aim of the first type of application is to study framework engine performance. Applications implemented on each of the framework as a result return to browser XHTML code. This code is identical in each framework (same md5 check sum).

Description of the compared applications – feature rich website – My Books

The second type of application, which is tested performance is a complex site that uses a large database of books called “My Books”. Its purpose is to test the most commonly used libraries and helpers of each of the frameworks. This goal is achievable only through the execution of large applications, which after a few improvements could successfully operate as big commercial website.

My Books – database description

My Books Database structure

Key element of service – database, comes from http://www.buy.com and contains around 1 300 000 books, along with full information about them. This database was provided in a SQL file that contains all data in single table.

For efficient operation of the database was converted to the form of four tables as shown in picture. In the new structure can be found a large amount of redundant data. Redundancy is necessary to the efficient operation of applications My Books.

For example, the category names are duplicated and are available both from the table “book” and “category”. With this solution on subpage categories will not have to perform a very time-consuming query: SELECT DISTINCT category FROM book (or its equivalent from the GROUP BY clause). However, the category name in the table book will be used when displaying information about the book.

My books – full text search

One of the key modules of each application like My Books is a search engine. My Books search engine supports simple regular expressions (ie, an asterisk means amount of arbitrary characters). Search phrase is checked in the title of the book, its description, author and category name. Necessary in this case is to use the full-text search.

Full text search, as its name suggests, allows you to search entire documents. It should perform this task during the nearly constant, regardless of the number of documents in the database.

The most popular methods to perform full-text search are:

  • Using MySQL clause LIKE %title%
  • Using MySQL and MyISAM engine using query MATCH AGAINST instead LIKE
  • Searching using Lucene written in Java
  • Searching using Sphinx written in C++

Sphinx search system is a full text search engine. It is able to index the data from the databases MySQL, PostgreSQL, or data in a special XML format. It is characterized by great speed of action and low consumption of computer resources.
Sphinx advantages include:

  • Very high speed indexing (to 10MB/sek.)
  • Very high speed of the search (usually several times faster than Lucene)
  • High scalability (up to 100 GB of text, 100 million documents on a single processor)
  • Native MySQL
  • Opportunity to sort and group the results by multiple categories
  • Supports English and Russian word formation
  • Any number of fields of indexed documents
  • Able to adjust the weights for your search “on the fly
  • Support for automatically locking the words
  • Full support for PHP without having to build additional modules, etc.

I am using in “My Books” search engine Sphinx. This solution was chosen because of its high efficiency and easy integration of search engines with the tested frameworks. In CakePHP I have to install the appropriate helper, in the Symfony it was necessary to correct an existing plugin and on the Zend Framework I wrote helper based on the appropriate plugin for Symfony.

The Sphinx server indexed query:

SELECT book_id, title, category, category_id, description, author_name FROM book

Using this query it is possible to query the Sphinx server and search by title, category names, description of books and names of the authors.

My Books – the structure and content

My Books -  News

The main task of My Books website is displaying information about available books in the database, search them and sell (via a link redirecting to the store page). Service consists of five main modules:

  • News – new positions, the latest additions to the database. News module displays the two newest entries from a books database. You can see there a book description (text limited to serveral characters) and thumbnail cached by ImageServer. Pagination in this module is inactive. In this case, I’ll check the speed of the model without pagination classes. After selecting the desired book and you will be redirected to a page with detailed information about the book including title, full description, cover, binding, book price or availability.
  • Categories – browse the books by predefined categories. Categories module displays all available categories (10 per each page). Paging the results was carried out based on the Paginator pattern. It displays current page and several previous and subsequent pages. Example of paging is on the picture below.
    My Books - paginator
    On the list you can see thumbnail of the cover (if it is available) and truncated description. Verified in this case is the speed of the model using the pagination of results.
  • Author – reviewing books by their authors. Author module is designed to display all authors from the database and then display a list of books for the chosen author. The results are presented in a form similar to categories module. To retrieve all the necessary data I need to made at least one tables linking. Checked in this case is the convenience of use and speed of implemented ORM pattern.
  • Search – book search. Module “Search” is made on the basis described in the previous chapter Sphinx server. In this section you can search for books by entering a phrase or keywords in the search box. Supported there is symbol of ambiguity – an asterisk denoting the occurrence of any number of arbitrary characters. After the query returned by the Sphinx results are processed to the database using query WHERE book_id IN (shinx_search_results). Another element is the list of results highlighting asked questions.
    My Books - Search
  • About – information about the service

Each page on My Books website contains several dynamic elements. First of them, available on every page, is a random book module. As the name suggests, this module displays a random book that has a description and picture. Random selection of such a large database is not a simple thing. An additional difficulty is that the primary keys are assigned at random, and not in order.
My Books - random book
Another feature available in many places is the recently added category and recently added authors. In addition to the name of the author or category there are also few characters available in that category or book titles written by the author.

My Books – releated services

My Books for proper operation requires a system dependent services, some of which have been marked schematically in the figure.

My Books - Flowchart

One of the first elements that support the request of the browser is a router. It is a component of all tested frameworks. My Books (in all frameworks) use the default router. The router decides which controller and action will handle the request. The controller communicate with a model (database server) or Sphinx search server, controller may need to communicate with media server (ImageServer). In controller also each piece of code can be cached and loaded from the cache. Controller after collecting results from the queries sends its results to the views layer. In views layer all associated arrays and objects are converted to XHTML code. Next step is decoration process – merging separate layout parts.

Very important element is cache. Performance whole application depends on it. Its easy and fast implementation and high flexibility is one of the key elements for the professional websites.

Time to implement of each My Books application was the same for each framework. So the results obtained – produced code – is relatively fair. If the implementing time wouldn’t be constant I improve a lot of elements. Constant working time allows comparing applications. Testing the framework performance wouldn’t be meaningful if implementation of the same function takes several more time time in one of the framework.

My Books – Symfony Framework 1.2.1

Application “My Books” in the Symfony framework could be done with many ways. The final version is based on the Doctrine plugin and PDO_MYSQL driver shipped with PHP. It is used Symfony native template system. Used for routing is the standard file routing.yml. Whole application uses three components: for the category menu, random books and authors. Book titles are set in each case in templates. Communicating with Image Serwer is performed using helper: ImgServHelper. Communication with the Sphinx is mediated by a modified plugin sfSphinxPlugin. Pagination is made using the Symfony sfDoctrinePager and written for the Sphinx service class sfSphinxPager.

My Books – CakePHP 1.2.0.7962

My Books on the CakePHP framework makes use MySQLi database driver. The model was written in such a way that does not include a dependent relationship to the tables (which significantly sped up the entire application). Support for Sphinx service is realized by using available SphinxBehaviour. For communication with Image Server I wrote a plugin. The whole application on the production server is set to not display any information for debuggers. Pagination is made using available in CakePHP helpers.

My Books – Zend Framework 1.7

Zend Framework uses a PDO_MYSQL driver. Sphinx Server communication is via modified sfSphinxPlugin available for Symfony Framework. Image Server use ImgServ helper. I had to writhe large parts of the code which functionality is available in the engines of the other two frameworks (eg, highlighting of search results). To debug the application it was necessary to write some classes, which allowed communication with the Firefox extension called FirePHP. These types of items were also available in the other two frameworkach without writing extra lines of code. For the routing process was used the default router available for FrontController.


To be continued…

Leave a comment