Posts by: devarni

Jetbrains, the known company behind the IntelliJ IDE for Java, will change the market with a new PHP IDE “PhpStorm;-) This time version 2 is in the beta stage but can be tested as a EAP early adopter version. Version 1 is released but Version 2 will catch up with the top of the PHP IDEs…

My first impression “wow”, this is a feature complete and performant PHP IDE ;-)

Masses of features like refactoring, code completion, Git support, Javascript, remote debugging,… XDebug and ZendDebugger are supported – important because XDebug has some problems to show all attributes of classes.

Available for Linux, MacOS and Windows. EAP works without restrictions for 45 days and this trial period will be reset with every new EAP version. Full version will be available at Q4 2010 for “0€” for Open Source projects to 88€ for a personal license (complete license matrix here)

Absolutely recommend to give it a try http://confluence.jetbrains.net

How is the performance of magic methods
(__call, __get, __set, …)?

Compared to direct method calls: bad. Here a short benchmark and the code I wrote for this benchmark (needs PHP 5.3):

Method Call: 3.01 seconds
Direct Access: 2.47 seconds
Magic Method (__call) 5.32 seconds
Magic Method (__get) 5.37 seconds
define('ITERATIONS', 2000000);
    function doBenchmark($name, Closure $closure){
        $start = microtime(true);
        for ($i=0; $i < ITERATIONS; ++$i) {
            $closure();
        }
        $stop = microtime(true);
        echo "Test $name: " . ($stop - $start) . " seconds";
    }

    class Test{
        public $data = array("view" => "view", "test2" => "test2", "test3" =>"test3");

        public function getView(){
            return $this->data['view'];
        }

        public function __call($name, $arg){
           return $name=="_getView" ? $this->data['view'] : false;
        }

        public function __get($key){
            return array_key_exists($key, $this->data) ? $this->data[$key] : null;
        }
    }

    $t = new Test();
    doBenchmark("Methode Call", function(){
        global $t;
        $v = $t->getView();
    });

    doBenchmark("Direct Access", function(){
        global $t;
        $v = $t->data['view'];
    });

    doBenchmark("Magic Methode (__call)", function(){
        global $t;
        $v = $t->_getView();
    });

    doBenchmark("Magic Methode (__get)", function(){
        global $t;
        $v = $t->view;
    });

So, you get the picure ;)

Don’t use magic methods as a replacement for a regular method if a direct access to a field or variable is possible. Magic methods are the sugar for dynamic created attributes (eg. for database objects) or if you don’t know the method/attribute while writing the code.

Use magic methods only rarely.

The only special magic method you should use often: __autoload ;-) It helps to load your needed classes on demand only when they are needed. If you use namespaces, here a short trick to load the name-mangled classes (the $path must of course be changed to your needs):

 

function __autoload($class) {
    $parts = explode('\\', $class);
    $class = end($parts);
    $path = __DIR__ . "/includes/"); //path for the includes

     require ($path . $class . ".php");
}

Happy coding ;-)

VirtualBox vs VMware: part 2010 ;)

Because the free Vmware Player now supports also the creating of virtual machines, it’s
time to make another short benchmark.

Host
Ubuntu GNU/Linux 10.10 64Bit (“Maverick”), 120GB SSD, 4GB RAM, Intel Core 2 Duo 6600

Guest
Windows 7 64Bit Home, 2xCPU, 2GB RAM, 20GB virtual HD on 1TB Samsung Spinpoint HD

Virtual Machines (64Bit)
VirtualBox 3.2.1, VMware Player 3.1.2


Windows 7 Performance Index

VirtualBox VMware
CPU 5,5 5,5
RAM 5,5 5,5
Graphic 1,0 4,2
Graphic (Games) 1,0 3,4
Harddisk 5,9 6,0
Performance Index: 1,0 3,4


Crystal Disk Mark 3.0

VirtualBox VmWare Player
Read Write Read Write
Seq 115.8 34.61 122.1 54.81
512k 60.31 30.80 43.69 53.45
4k 0.858 0.718 1.004 11.78
4k QD32 0.950 0.911 1.129 19.13


Summary

There is no difference for the computing performance. VirtualBox and VMware playing at the same level.
For graphic performance/support VMware wins. VirtualBox 3D support is only rudimentary and does not work for Windows Airo (so the bad performance index for graphics).
The disk/caching performance of VMware is better.

The overall performance of VMware Player is better then VirtualBox.
If you need 3D support eg. for Windows 7 Airo as a guest, VMware is the only solution.
If you need snapshots VirtualBox wins, for such features you need VMware Workstation.

I will give you a idea about the easy implementation of error page handling with Nginx.

1. Setting error handler

Error handler can be set with the error_page directive. More about this directive you can find here.

Do this in your server block but better use a generic file and include this in every virtual host server block. I use for this a file “vhost” with all such settings.

The handler or html files are placed in a separate folder outside the webs. You can use a folder inside your Nginx settings (I use here for example a “html” folder).

The location block is needed to pass the correct root and file to php fastcgi. In this example I use a file “fastcgi”  with this mostly used lines:

fastcgi_pass   unix:/tmp/fastcgi.socket;
fastcgi_index  index.php;
fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
include fastcgi_params;

Here the part in the server block (or in your generic vhost file for including):


# error handler, codes can also be combined
error_page 401 /error.php?c=401;
error_page 403 /error.php?c=403;
error_page 404 /error.php?c=404;
error_page 500 /error.php?c=500;
error_page 502 503 504 /error.php?c=50x;

# serve error.php from /etc/nginx/html folder
location =/error.php {
   root /etc/nginx/html;
   include conf/fastcgi;
}

2. PHP file

The error.php file will be called with the URL encoded error-code, so its easy to grab this code in the PHP file from the superglobal $_GET array.

To map this error code to a message string, use a associative array and a function to return the string.

This is the part in the error.php file:


 "Autorisierung erforderlich! - Authorization Required",
    "403" => "Zugriff ist nicht erlaubt! - Access forbidden",
    "404" => "Seite wurde nicht gefunden - Page not found",
    "500" => "Interner Serverfehler - Internal Server Error",
    "50x" => "Seite ist momentan nicht verfügbar. Bitte Anfrage später wiederholen.
-
The page is temporarily unavailable. Try again later."); // get the error string function errorString($error) { global $_err; foreach ($_err as $key => $val) { if ($key == $error) return $val; } return "Unknown Error"; } isset($_GET['c']) or die("Unknown Error"); $error = $_GET['c']; $str = errorString($error); ?> Error <?=$error?>: <?=$str?>

You can format the error page as you like. But remember: the web root url will be the actual virtual host! Easiest way: use inline CSS. If this is not your preferred way, place the css file in a virtual host (eg. your default virtual host)

This configuration is used to access phpmyadmin as a “subfolder” of the domain like:

http://mysite.com/phpmyadmin

The location will be append to the root! For a similar behaviour like the Apache configuration, you could use the “alias” directive

My phpmyadmin installation is under /usr/share/phpmyadmin, thats why I use /usr/share as the root.

server{
...
    location /phpmyadmin{
        root    /usr/share;
        index   index.php;
    }

    location ~ \.php$ {
        set $php_root   $document_root;
            if ($request_uri ~* /phpmyadmin) {
                set $php_root /usr/share;
           }

        fastcgi_pass   unix:/tmp/fastcgi.socket;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $php_root$fastcgi_script_name;
        include /etc/nginx/fastcgi_params;
    }
}

Quick Benchmark of Apache, Nginx, Cherokee, Lighttpd

A short benchmark of this 4 server with dynamic and static content.
All server provides the same functionality like URL rewriting, password protected folders/files etc. but all have their own way to do that ;)
For shared hosting where the user has no access to the server configuration, Apache with the .htaccess file support is a good choice.

I used the benchmark tool from Apache ab -n 50000 -c 20
php-cgi goes over a Unix socket

Dynamic Content (PHP)

Server Requests (sec) Transferrate (KB/s)
Apache 2.2.14 2125 11684
Nginx 0.7.65 1734
1861
9436 (php-cgi)
10115 (php-fpm)
Cherokee 1.0.0 2119
2103
11562 (php-cgi)
11454 (php-fpm)

Static content (14785 Bytes HTML file)

Server Requests (sec) Transferrate (KB/s)
Apache 2.2.14 6768 99593
Lighttpd 2.4.26 15782 213866
Cherokee 1.0.0 7602 111285
Nginx 0.7.65 10912 159828

More “real live” test with PHP and eAccelerator enabled

Cherokee/NGinx/Lighttp are using the same php-fpm socket

Server Requests/s Ø
Apache 2.2.14 2713|2744|2766 2741
Nginx 0.7.65 2416|2433|2418 2422
Cherokee 1.0.0 2702|2638|2694 2678
Lighttp 1.4.26 2492|2363|2540 2465

Results

There is a small difference between fast-cgi and php-fpm (alternative implementation of fast-cgi).
You should prefer php-fpm (will be included in PHP 5.4 or you must build it manually from source).

Apache and Cherokee are both excellent for dynamic content.

For static content Lighttp is the fastest server.

There is not _the_ best server but a good combination: Lighttpd or Nginx as a proxy for serving static content, Apache or Cherokee for the dynamic content. Or a another proxy server like Varnish for static content and caching and Apache/Cherokee for serving dynamic content

For a PHP site with only 1 server running: Apache.

For a smaller memory footprint and if .htaccess is not needed: Cherokee (also the only server with a very good graphical admin interface)

Benchmark VirtualBox 2.1.4 vs VMware Workstation 6.5.1

Host: Ubuntu64, 4GB RAM, Core2Duo E6600, Samsung HD502IJ

Guest: Windows XP Home, 1GB RAM, 8GB virtual disk

Benchmark Software: SiSoft Sandra Light, HD Tune

Short benchmark of this both VM

VirtualBox VMware Workstation
HDTune
Min (MB/s) 26 5,7
Max (MB/s) 250 803
Average (MB/s) 177 392
Access time (ms) 0,4 0,2
Burst rate (MB/s) 129 488
CPU Usage 56% 17%
Sisoft Sandra
Dhrystone ALU (GIPS) 22,70 7,35 (*7,34)
Whetstone iSSE3 (GFlops) 18,64 6,26 (*5,92)
Multimedia: Integer (MPixel/s) 77,21 15,76 (*16,90)
Multimedia: Floating Point (MPixel/s) 45,77 10,04 (*10,19)
Multimedia: Double (MPixel/s) 23,96 5,14 (*4,28)
Physical Drives (MB/s) 109 357
Combined Performance Index 671 802

*) Dual CPU Mode

Resumee

VirtualBox shows better computing performance, which benefits for desktop applications and multmedia based operations. Interesting: the dual CPU mode in Vwmare has no advantage over the single CPU mode.

Certainly we can say: Virtualbox for desktop applications, Vmware for server but for the combined performance VMware ist the winner.

The difference

Don’t forget: Flash != Flex. Flex uses elements from Flash but is built on own classes. If Flash and Flex should play together we need a interface.
MovieClip is a display object in Flash and UIComponent is a display object in Flex. But we can wrap a MovieClip into a UIComponent, so the Flex UIComponent becomes a container for our Flash MovieClip.
Here both files, at first the MXML file:





And the Actionscript file with the class:


package {
	import mx.core.Application;
	import mx.core.UIComponent;
	import flash.display.MovieClip;

	/*****************************************************************
	 * File: FlexApp.as
	 *****************************************************************/

	public class FlexApp {
		private var _app:Application;

		public function FlexApp(app:Application) {
			_app=app;

			var btn:MovieClip=new BtnStop();
			var flexui:UIComponent=new UIComponent;
			flexui.addChild(btn);
			app.addChild(flexui);
		}

		static public function initApp():FlexApp {
			return new FlexApp(Application(Application.application));
		}

		public function get app():Application {
			return _app;
		}
	}
}

How it works

applicationComplete in the MXML file is the eventhandler called from the application. Here we provide our handler, which must be a static member function of a class so it can be called later.

initApp() creates our class and passes a application object to the constructor.

Inside the constructor we create the MovieClip from the imported Flash symbol “BtnStop” (this symbol comes from a swc lib, created with Flash). Then we create a Flex UIComponent container and add our MovieClip as a child. The UIComponent can now be added as child of the Flex application.

Overview of development tools for Flash/Flex

Tool Price Link
FlashDevelop 3 rc1
Flex, Actionscript IDE (Windows) free http://flashdevelop.org

Adobe® Flex SDK3
Flex Software Development Kit (Compiler, Framework…) free Flex download

Adobe® Flash® CS4
Design and programming of Flash animations $699/831€ 30-day trial

Adobe® FlexBuilder3® Professional
Flex programming and visual design of MXML, based on Eclipse Standard: $249/213€
Professional: $699/593€
30-day trial

Ensemble Tofino
Flex plugin for Microsoft Visual Studio® free http://www.ensemble.com