-->

Author Archive

PHP magic methods performance

by on 19 November 2010, under PHP, Programming

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 ;-)

Leave a Comment more...

Benchmark: VirtualBox vs VMware: Part2 2010

by on 29 Oktober 2010, under Benchmark, Virtualization

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.

Leave a Comment more...

NGinx: Custom error handling

by on 18 Mai 2010, under Howto

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)

Leave a Comment more...

Nginx: phpmyadmin configuration

by on 13 Mai 2010, under Howto

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;
    }
}
Leave a Comment more...

Server Benchmark: Apache, Nginx, Cherokee, Lighttp

by on 13 Mai 2010, under Networking

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)

Leave a Comment more...

Benchmark: VirtualBox vs VMware

by on 10 März 2009, under Benchmark

Benchmark VirtualBox 2.1.4 vs VMware Workstation 6.5.1

Host: Ubuntu64, 4GB RAM, Core2Duo E6600, Samsung HD502IJ
Gast: Windows XP Home, 1GB RAM, 8GB virtual disk
Benchmark Software: SiSoft Sandra Light, HD Tune

Ein kurzer Benchmark der beiden VMs

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 zeigt die bessere Rechenleistung, was sich besonders bei Desktopanwendungen und multimedialastigen Operationen positiv auswirkt.
VMware zeigt die bessere Festplattenperformance, was sich besonders bei Servern positiv bemerkbar macht. Interessant: der Dual CPU Mode in Vmware hat keinen Vorteil zum Single CPU mode).
Man kann sicherlich sagen: VirtualBox für Desktopanwendungen, Vmware für Server, wobei Vmware in der Gesamtperformance der Sieger ist.

Leave a Comment :, , , more...

Flash Symbole mit Actionscript3 in Flex

by on 23 Februar 2009, under Flash & Flex, Programming

Der Unterschied

Nicht vergessen: Flash != Flex. Flex benutzt zwar Flash Elemente basiert aber auf eigenen Klassen. Wenn Flash und Flex zusammenspielen sollen, benötigen wir eine Schnittstelle.
Ein MovieClip ist ein Anzeigeobjekt in Flash und UIComponent ist ein Anzeigeobjekt in Flex. Wir können aber ein MovieClip in ein UIComponent “verpacken”, so wird das Flex UIComponent ein Container für unseren Flash MovieClip.
Hier nun beide Dateien, zuerst die MXML Datei:





Und die Actionscriptdatei mit der Klasse:


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;
		}
	}
}

Wie es funktioniert

applicationComplete in der MXML Datei ist der Eventhandler der von der Anwendung aufgerufen wird. Hier setzen wir unseren Handler ein, der eine statische Funktion unser Klasse sein muss damit er auch später aufgerufen werden kann.

initApp() erzeugt unsere Klasse und übergibt das Objekt als Parameter an den Constructor.

Im Constructor erzeugen wir den MovieClip, basierend auf dem von Flash importierten Symbol “BtnStop” (dieses Symbol kommt von einer SWC Bibliothek die vorher mit Flash erzeugt wurde). Es wird dann ein Flex UIComponent erzeugt, wo wir unseren MovieClip als Child hinzufügen. Das UIComponent kann nun zur Flex-Anwendung hinzugefügt werden.

Leave a Comment :, more...

Die Werkzeuge…

by on 22 Februar 2009, under IDEs & Development Tools

Überblick über Entwicklungstools für Flash/Flash

Tool Kosten 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 und Programmierung von Flash Animationen $699/831€ 30-day trial

Adobe® FlexBuilder3® Professional
Flex Programmierung und visuelles Design von MXML, basierend auf Eclipse Standard: $249/213€
Professional: $699/593€
30-day trial

Ensemble Tofino
Flex plugin für Microsoft Visual Studio® free http://www.ensemble.com
Leave a Comment :, more...

FlexBuilder3 mit Aptana/Eclipse 3.4.1 “Ganymede”

by on 22 Februar 2009, under IDEs & Development Tools

FlexBuilder3 basiert auf Eclipse 3.3.1 und funktioniert unglücklicherweise nicht ohne Probleme mit Eclipse 3.4.1 “Ganymede”.
Aber Eclipse “Ganymede” besitzt ein Feature zum einfachen einbinden von Plugins als sogenannte “Dropins”, dazu existiert unter der Eclipse 3.4.1 Installation ein Ordner “dropin”…

Eclipse schließen und den kompletten Flexbuilder Ordner (Standalone oder Plugin) in diesen dropin Ordner kopieren. Eclipse starten und nun sollte die komplette Flexumgebung zur Verfügung stehen!
Flexbuilder findet man beim Standardinstallationspfad unter C :\Programme\Adobe.

3 Comments :, , , more...

FlexBuilder vs FlashDevelop

by on 22 Februar 2009, under IDEs & Development Tools

Ein kurzer Vergleich von Flexbuilder3 mit FlashDevelop 3.

FlexBuilder 3


Dafür

  • Einfaches debuggen und profilen
  • Automatisches hinzufügen von Importen und Organisierung von Importen
  • Visual Designer für MXML
  • Durch Eclipse: Unterstützung für andere Sprachen (z.B. durch Aptana, PDT…) und durch Eclipse Plugins stark erweiterbar. Damit gehen dann auch Features wie “Code-Schnipsel” (CFEclipse), automatische Codeerzeugung z.B. für “Getter/Setter” (Monkey Script) usw.
  • Class Wizard
  • Unterstützung für ASDoc Kommentare
  • Unterstützung für virtuelle Ordner/Dateien (Links zu externen Dateien/Ordnern sind ein Feature von Eclipse, funktioniert so auch unter Windows)
  • Refactoring (Umbenennung von Klassen, Funktionen, Eigenschaften mit automatischer Anpassung der Abhängigkeiten)

Dagegen

  • Kommerzielle Lizenz
  • Plugin funktioniert nicht mit allen Eclipse Versionen (z.B. nicht mit Eclipse Ganymede 3.4.1 oder 64bit Versionen)
  • Suchen, hinzufügen und konfigurieren der fehlenden Features durch Eclipse Plugins ist oft aufwendig
  • Code Assistent eher durchschnittlich und etwas fehlerhaft z.B. wenn richtige Importe fehlen (“java.lang.NullPointerException”)
  • Kein Code Formattierer oder automatische getter/setter (nur mit Eclipse plugins wie Monkey Scripts, CFeclipse, Flexformatter “FlexPrettyPrintCommand”...)
  • Keine Anzeige der Paketinhalte von SWC Dateien

FlashDevelop

Dafür

  • Frei und OpenSource (entwickelt mit C#)
  • “Leichgewichtig”
  • Bester verfügbarer Code Assistent für Actionscript
  • Contextabhängige Code Vervollverständigung (getter/setter, Eventhandler…)
  • Code Snippets
  • Automatisches hinzufügen der Importe
  • Erweiterbar durch Plugins
  • Unterstützung für ASDoc Kommentare
  • Paket Ansicht (zeigt Klassen und Symbole in SWC Dateien)

Dagegen

  • Nur für Windows
  • Plugins funktionieren nicht immer in neueren Versionen und viele Plugin werden eher selten geupdatet
  • Debugging nur mit Plugin und noch nicht ganz ausgereift
  • Kein visueller MXML Designer
  • Kein Klassenassistent
  • Kein Code Formattierer
  • Keine Unterstützung von virtuellen Ordnern/Dateien (“Links”) im Projekt
  • Kein Refactoring


Meine Empfehlung

Für Debuggen, visuelles Design von MXML: FlexBuilder
Für überwiegend Programmierung von Actionscript unter Windows: FlashDevelop.

Leave a Comment :, more...

Suche

Benutze das Eingabefeld für die Suche:

Archive

Alle Einträge, chronologisch...

© 2009 by Frank Arnold | Impressum