Legal
The views expressed in this blog are my own and do not necessarily reflect the views of Adobe Systems Incorporated.
Search
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 | XML | AMFPHP | JSON |
| 5000 | 1903 | 2033 | 2814 |
| 10000 | 3625 | 3495 | 4747 |
| 15000 | 4326 | 4867 | 6830 |
| 20000 | 5939 | 6550 | 9094 |
| 25000 | 6570 | 7942 | 11838 |
| 30000 | 8473 | 10335 | 14512 |
| 35000 | 9594 | 12389 | 17716 |
| 40000 | 10885 | 14651 | 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:amfphp, json, flex
Blogged with Flock
Use of this website signifies your agreement to the Terms of Use and Online Privacy Policy (updated 07-14-2009).
Comments
I've posted the actual Flex application up at wickeddemo.com/flex_php_performance/
Mike
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.
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]
Interesting article comparing performance of transferring data beteween Flex & PHP. Please keep the PHP/Flex articles coming.
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]
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.
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.
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.
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 !
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
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
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
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
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
For those who advocate the vey best of JSON or something else.
First whenever you have a problem you have to put it in context of what you are trying to achieve. That is going to determine your choices. Anyone can advocate all day long pro and against AMF, JSON, or XML. But does it make sense? All of them have their applicability and or course limitations. Moreover, when you make choices as such, professionals need to have the full SDLC view, and that includes, code maintenability, documentation, flow of changes, tools to be employed, etc. It's very easy to say that choosing JSON is very light over the wire, but in the context of a complex solution, which also requires interoperability, and you account for all costs in SDLC, JSON is not the right choice, and old XML is probably a more more cost effective approach.
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.)