Mike Potter

July 12, 2006

PHP and Flex - JSON, XML or AMFPHP?

I've created a small performance study that shows time to load three different types of data from a PHP backend... Data transfered as PHP objects from AMFPHP, data transfered as XML and data transfered in JSON notation.

The two most popular ways of getting information from a PHP backend to a Flex front end are through the use of XML and AMFPHP. The use of XML requires you to serialize your data into XML formats, while the use of AMFPHP allows you to transfer objects directly from PHP to ActionScript, the Flex scripting language that is similar to JavaScript. I'm not too familiar with JSON, but it seems to be getting a lot of attention for web based applications.

In the tests, I use the AMFPHP library, which you'll need for the AMFPHP test. I use the Zend Framework for the JSON library, converting my PHP Array to JSON. JSON is handled in Flex with the use of the JSON library. The XML test is the simplest of all, simply printing out the XML without having to load any other library or files.

Here's the data (times are in ms):

# of data
XMLAMFPHPJSON
500019032033
2814
1000036253495
4747
1500043264867
6830
2000059396550
9094
2500065707942
11838
30000847310335
14512
35000959412389
17716
400001088514651
20480

As you can see, under 20000 items returned, AMFPHP and XML give you about the same performance, with JSON lagging behind considerably. Above 20000 items returned, XML appears to be a bit better than AMFPHP in terms of performance, with JSON really struggling. (Of course, once the data was in the Flex application, the data could be sorted and filtered with the same performance).

There's lots of things to consider when building out your PHP application with a Flex front end. First of all, you need to consider the fact that with XML, you're respondible for serializing all your data. That may or may not be difficult, depending on what your data type is. In this example, it was relatively easy. AMFPHP may provide some time advantages there for your development, and may be a reason to choose that over XML. As well, you should look at the amount of data that is being transferred. I did all my tests over a local connection. Over the Internet, lattency may be an issue for your application (AMF is a binary protocol, which should make things slightly faster than straight XML encoding).

My next tasks are to see what happens when I do this remotely, and to see what effects any of the PHP accelerators have on the performance

If you'd like to do the tests on your own, here is a .zip file that contains all the useful files: the .mxml application file, the PHP files for printing out JSON and XML data and the file to put in your AMFPHP services/ directory.

technorati tags:, ,

Blogged with Flock

Posted by at 9:15 AM on July 12, 2006

Comments

Mike Potter — 11:52 AM on July 12, 2006

I've posted the actual Flex application up at wickeddemo.com/flex_php_performance/

Mike

Matt — 10:52 PM on July 13, 2006

Very cool. I was under the impression that XML was much slower than AMFPHP. I may consider it as a strong alternative for larger apps. Your blog has quickly become one of my favorite blogs on Flex. I just can not find PHP content anywhere else. Thanks.

Leonardo França — 10:19 AM on July 14, 2006

in my tests, the AMFPHP was faster of what the XML
the test remotely it made the difference =)

Best Regards
Leonardo França
[This matches what Adobe consulting told me as well... I suggest that people do their own tests on their own applications. - Mike]

David F — 4:33 PM on July 15, 2006

Interesting article comparing performance of transferring data beteween Flex & PHP. Please keep the PHP/Flex articles coming.

Luis — 3:01 PM on July 17, 2006

there is a json php extension. why not use that instead of the zend implementation? your numbers would be very different.
[I used the Zend Framework instead of the json php extension because I really think that the Zend Framework is the easiest to setup and use, and I have other examples of using the ZF that I'm going to publish - Mike]

MySchizoBuddy — 12:36 AM on July 22, 2006

Since Flex was designed for designers to easily create web applications. Does Flex have build in commands to connect to a databse without requiring us designers to learn php and MySql.

Something easy and secure for me to connect to a database, store names and adresses for eg., and easily retrive them as well.

Blaine McDonnell — 12:21 PM on July 26, 2006

I noticed that you are using localhost for your comparison. The main advantages of JSON and AMF are the network efficiencies, XML is typically 2+ times more the size. In this case you aren't using an optimized JSON structure, which makes the resource efficiency only about 8K better, an optimized structure gets you about 1.5M better.

My references to DIRECT in the following mean printing out code, not converting structures.

It's also not fair that you are generating an array in PHP then through a function to convert it vs. XML being DIRECT output. To even the playing field you need to either generate the XML from an array (which is more common for a WS) or print JSON (which is easy enough). By doing DIRECT output my results processed on average ~10sec faster than XML, optimized JSON is ~15sec faster (Numbers from my laptop). The improvement should be even greater over WAN or LAN.

Now onto memory optimization:
Initially I found that the JSON and XML used about the same PHY/VM memory 107M/112M (Increase over SWF/Firefox footprint) and AMF was dramatically lower at 36M/35M. I noticed that the transmission of DIRECT RAW JSON took ~6sec vs. XML taking ~23sec. So based on this information, I checked out how you were handling the return results for JSON and you were creating multiple variations of the JSON array without cleaning up. I modified the code to not make extra copies(variations) of the data and was able to reduce the footprint to 67M/80M (unoptimized structure), this is still quite larger than AMF but more acceptable than XML. The XML doesn't appear that it could be handled any more efficient.

For non DIRECT output the JSON API is very fast, easy to install and use. I don't think that is a boundary for your audience.

Steven — 7:43 PM on November 28, 2006

MySchizoBuddy,

The reason there are so many middleware solutions on the market, PHP, Java, Ruby, etc., is because there needs to be something between your database and the front end. One of the primary reasons for this is security.

If you were able to directly modify your database, I could decompile your swf (very easy) and see exactly how your database was set up and gain access to it and possibly more depending on whether or not you have the latest patches, etc.

With middleware, the communication with the database is abstracted.

If you're looking for an easier solution, ColdFusion is probably the easiest one out there. It does for middleware what Dreamweaver did for HTML, which is to say it gave non-coders a set of tools to set up a back-end.

mysticav — 9:04 PM on April 2, 2007

Mike Potter:
Please do the test using native php_json module coming with PHP 5.2. Those statidistics will change dramatically. Don't be surprised if JSON becomes be the winnner !

mandy — 11:29 PM on January 16, 2009

Hey Mike, I'm very new to Flex and PHP and get to know your blog while Im doin research on both platform. I use PHP to create some XML object and I can display them in a vbox using canvas. However, I dunno how to display them into List. For your reminder, I use a for loop to load data. i think i shud use a dynamic array to capture data however it cant work. Hope you can help me

mandy — 11:29 PM on January 16, 2009

Hey Mike, I'm very new to Flex and PHP and get to know your blog while Im doin research on both platform. I use PHP to create some XML object and I can display them in a vbox using canvas. However, I dunno how to display them into List. For your reminder, I use a for loop to load data. i think i shud use a dynamic array to capture data however it cant work. Hope you can help me

mandy — 11:30 PM on January 16, 2009

Hey Mike, I'm very new to Flex and PHP and get to know your blog while Im doin research on both platform. I use PHP to create some XML object and I can display them in a vbox using canvas. However, I dunno how to display them into List. For your reminder, I use a for loop to load data. i think i shud use a dynamic array to capture data however it cant work. Hope you can help me

mandy — 11:40 PM on January 16, 2009

Hey Mike, I'm very new to Flex and PHP and get to know your blog while Im doin research on both platform. I use PHP to create some XML object and I can display them in a vbox using canvas. However, I dunno how to display them into List. For your reminder, I use a for loop to load data. i think i shud use a dynamic array to capture data however it cant work. Hope you can help me

mandy — 11:48 PM on January 16, 2009

Hey Mike, I'm very new to Flex and PHP and get to know your blog while Im doin research on both platform. I use PHP to create some XML object and I can display them in a vbox using canvas. However, I dunno how to display them into List. For your reminder, I use a for loop to load data. i think i shud use a dynamic array to capture data however it cant work. Hope you can help me

Thank you for signing in, You may now comment. (Sign Out)

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Add your comments

Remember Me?

(You may use HTML tags for style.)

Copyright © 2009 Adobe Systems Incorporated. All rights reserved.
Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy (updated 07-14-2009).