Showing posts with label AJAX. Show all posts
Showing posts with label AJAX. Show all posts

Sunday, July 6, 2008

Brief FAQ of AJAX Programming Concepts

Q: What is the AJAX?

A: Well, it is the Asynchronous Javascript And Xml. The technology exists since 1995-98, however the term AJAX is used since 2005.

Q: How does the AJAX work?

A: The AJAX works as follows:

  • the javascript in a browser reacts to a client-side event. An instance of XMLHttpRequest class is created inside a web browser. It acts like a "browser" inside the browser - it can handle GET/POST request from within the javascript,
  • the XmlHttpRequest connects to the server and a server-side component handles the request,
  • the request returns to the original page and a XML response is handled by another javascript routine which updates the state of the page according to the response content,

Most of todays frameworks actually do not rely on XML as the response protocol but instead the JSON is used. It also means that today we rather use AJAJ than AJAX (Asynchronous Javascript And JSON). Client-server roundtrips are usually called callbacks instead of postbacks which occur in standard form-posting scenario. The most reduced infrastructure, where the response takes form of an HTML fragment which is used to replace part of a page in user's browser, is often referred to as AHAH (Asynchronous HTTP and HTML).

Q: So, why do we need AJAX?

A: To minimize the ugly effect of page reloading? No. It can be achieved much easier.

There are in fact two important reasons for AJAX:

  • to minimize the amount of data sent to and from the application server (we do not POST the entire HTML form and do not respond with the entire HTML page)
  • to improve the user performance (for example: to provide a dynamic context-sensitive content when the user types into a textbox)

Q: Why do we need AJAX frameworks?

A: It is easy and possible to write an AJAX-style application without any support from a high-level framework. You just learn how to use the XMLHttpRequest and are on your own.

This, however, could be very inefficient. For any action which needs to be "AJAXified" you need three core components:

  • (I) a client-side javascript code raised by a browser event, responsible for creating a request to the server
  • (S) any server-side component which handles the GET/POST of the request from previous step
  • (R) a client-side javascript which handles the response of the server

I call these three components I, S and R (Init, Server and Response) and I call the whole AJAX mayhem "the I-S-R problem".

The nice aspect of calling all these three components is to realize that they are, in fact, quite independent. You can expect frameworks which attack all three issues in one coherent way, however nothing stops you from using completely different components for each of the three.

Q: I am lost in the plethora of AJAX frameworks. Can they be somehow classified?

A: I did some research and I've found the I-S-R perspective quite handy. When I see a new framework, I immediately focus on its I-S-R components and thus I am able to compare the framework to other which focus on similar issues. I think that five different approaches to AJAX can be identified:

I Incomplete I-S-R frameworks

Examples:

Profile:

  • you manually program the client-side user interface ...
  • ... or manually program the server-side components ...
  • ... or manually consume server responses on the client-side
  • sometimes semi-automatic creation of callbacks (for example: server-side interface is "mirrored" on the client side by a set of proxy interfaces and a convenient way of calling server-side API with these proxies is provided)

None of these "incomplete" frameworks provide the complete way to handle the I-S-R problem. The interesting thing is that this is where you can mix different frameworks which operate on different levels of the problem so for example you can use Bindows as the GUI provider and the JayRock as the server-side gateway.

II Custom AJAX components

Examples:

Profile:

  • someone builds a set of ASP.NET components which, instead of providing values for standard form POSTs, handle their events in an AJAX-style. Usually these components "mimic" the ASP.NET components and you "switch" from ASP.NET library components to such AJAX-enabled components by just changing <asp:Button> to <XYZ:Button> etc.
  • it's great to see that someone knows how AJAX works, however I belive that "component switching" is just a wrong direction

III Ajax Managers

Examples:

Profile:

  • you get an AJAX Manager, an ASP.NET component which, when put on a page, automatically converts all postbacks to callbacks. Sometimes it takes 15 seconds to AJAX-enable your application,
  • more advanced frameworks are in the II and III group in the same time - you have AJAX Managers which convert existing applications into AJAX-enabled applications plus a bunch of optimized components is available for more advanced UI stuff

IV "Client-side logic with server-side tools" frameworks

Examples:

Profile:

  • client-side logic is built with server-side technology (C#/Java) and then magically converted to a bunch of javascripts
  • you develop "javascripts" using strongly-typed languages, you test "javascript" with your favourite testing frameworks
  • it sounds great until you try write a code which opens a file ...
  • ... and then you realize that, in fact, you are using a really small subset of server-side technology and "opening files" does not belong to this subset ...
  • ... so whenever you are to perform a server-side action, you are on your own (which means: use a framework which makes RPC possible, perhaps something from group I?)

V Server-side AJAX

Examples:

Profile:

  • "dumb client, almighty-server", there's no "logic" on the client-side but rather messages passed to and from the server
  • it is the server which holds the complete state of the application and the user interface and the client-side only renders it
  • all client-side events are processed on the server
  • this approach is the most resource demanding of all
  • in the same time, it's most safe, since many typical internet attacks just don't work here (for example, Cross-site Request Forgery or ViewState Tamplering attacks are impossible).
  • it's impossible to easily AJAX-enable existing ASP.NET applications with this approach

For an up-to-date list of .NET Ajax Frameworks, visit the AjaxProjects page.

Q: Any concise AJAX references?

A: If you need something really concise, "Teach Yourself Ajax in 10 Minutes" should be great. I can also recommend "Ajax Design Patterns".

Tuesday, July 3, 2007

Tiny ASP.NET Ajax Framework Contest Continued...

Few days ago we've held a tiny contest of three ASP.NET Ajax frameworks. This time, three more contestants await to be compared:

These three above were chosen because the integration of an existing application is as simple as placing a single Ajax Manager on a web form - few other frameworks (and here) were rejected because they are either dead now, force you to use specific set of components or rely on writing client-side javascript code.

The result chart is available here:

  • ComfortASP.NET has a terrible requests-per-second ratio. It served only 35 (!) requests per second, while sending 6 and receiving 83 MB of data
  • Anthem.NET was able to process 133 request per second, sending 6 and receving 121 MB of data
  • Telerik's RAD Ajax did 292 requests per second, sending 7 and receiving 172 MB (!) of data

Gathering all results together, we had 2 non-Ajax contestants: pure Web.Forms application, Javascript version of it and 6 Ajax frameworks: Microsoft's ASP.NET AJAX, Ajaxium, VisualWebGUI, ComfortASP.NET, Anthem.NET and Telerik's RAD Ajax.

Regarding requests-per-second we have following results:

 

 

requests-per-second

reference

VWG

422

198,1220657

Javascript

400

187,7934272

Telerik

292

137,0892019

ASP.NET AJAX

222

104,2253521

Web.Forms

213

100

Anthem

133

62,44131455

Ajaxium

111

52,11267606

Comfort

35

16,43192488

 

Regarding number-of-bytes-sent we have following results:

 

 

sent data

reference

Javascript

1938800

24,23560589

VWG

2066800

25,83564589

Ajaxium

5029800

62,87407185

ASP.NET AJAX

5600600

70,00925023

Comfort

5880400

73,50683767

Anthem

6161400

77,01942549

Telerik

7293200

91,16727918

Web.Forms

7999800

100

 

Regarding number-of-bytes-received we have following results:

 

 

received data

reference

VWG

23348807

29,11700146

Javascript

37121000

46,29153905

Ajaxium

47756301

59,55423272

Web.Forms

80189600

100

Comfort

83742600

104,4307491

Anthem

121547800

151,575516

ASP.NET AJAX

129531800

161,5319194

Telerik

172294800

214,8592835

 

Few comments:

  • the overall winner is VWG, however the application had to be completely rewritten in order to run under this framework
  • the javascript version did not win (even in the bytes-received test!) and though this may seem surprising, it is caused by the script size
  • the Ajaxium, Anthem and Comfort reduce the requests-per-second ratio singificantly. The latter two are able to correctly handle dynamic, non-ajax content sent to the browser
  • regarding the requests-per-second, Telerik is significantly better than ASP.NET AJAX, however it downloads huge amout of data comparing to all other frameworks

Please feel free to study these resulsts and pick up your own winner. Thanks again to Krzysiek Owczarek for contributing to this contest.

Thursday, June 28, 2007

Tiny ASP.NET Ajax Framework Contest

 

Contest context

More than two years ago we've built a small web application used to collect and print documents required to participate in secondary school final examinations in Poland. The application is used quite extensively by people from all over the country.

The application, maturzysta.vulcan.pl, is located here.

From the web developer's point of view there is one interesting aspect of the main form of the application: there are about 30 dropdown lists with complicated logic between their selections: when you change the selection in one list, few others should be cleared or repopulated with context-bound data. There is also quite a lot of validation of various dependencies between user's selections.

The first version of the application was built as a server-side application: all SelectedIndexChanged events were auto-postbacked and processed on the application server. This caused a lot of trouble since the heavy form had to be sent to and from the server.

The year later we've completely redesigned the application, moving the interface logic to the javascript. The web traffic problem has been solved but two other appear. First of all - no one likes javascript, it's clumsy and error-prone. Then - we had to disable ASP.NET page validation mechanism because otherwise the page content modified on the client-side would be rejected on the server-side as potentially dangerous.

In the meantime, few promising AJAX frameworks appeared and we thought that it would be possible to go back to the initial version of the application but instead of expensive postbacks we could take the AJAX approach and replace postbacks with callbacks. This could solve all problems: reduce the traffic and still keep the application logic on the server-side.

Contest participants

Three AJAX frameworks were chosen for the contest:

The first and the third one were chosen because it's fairly straightforward to extend existing application with AJAX capabilities. The second one is perhaps one of the most promising AJAX frameworks out there, designed to fill the gap between Windows.Forms and Web.Forms programming.

In order to make the application compatible with participants, following steps have been taken:

  • in case of ASP.NET AJAX, the ScriptManager has been put on the form page and the whole page content has been put into the UpdatePanel. The concept of several small UpdatePanels with triggers between them has been rejected because of the complexity of the application logic and dependencies between controls.
  • in case of the Ajaxium framework, the Ajaxium initialization routine has been put into the OnBeginRequest event of the global application class as well as into the Page_Load method of the form.
  • in case of the VisualWebGUI framework, the application has been completely rewritten using the VWG library and components.

Compare the layout of the WebForms/AJAX application (save images to see them full size)

with the layout of the rewritten VWG version

 The judge

Microsoft Application Center Test has been choosen as the contest judgement tool. Tests were performed on the Windows XP machine serving as the application server. Exactly 200 sessions have been simulated for 5 concurrent users. Each session has been recorded as the complete user session - forms have been filled with data and dropdown selections have been made.

Each session ended with the application showing the print-ready document.

The results

The results are available here.

Observations:

  • both the Microsoft AJAX.NET and the Ajaxium frameworks are not capable of delivering dynamically created content to the web browser from within the AJAX callback [or at least we do not know how to do that]. This means that when the application produces, let say, XML data file and sends it to the application, instead of a typical "Save - Open - Cancel" prompt, you get ... nothing. Fortunately, this was not considered as a test case.
  • the original Web.Forms server-side application, numer (1) in the result chart, has been able to process 213 requests per second and ended with almost 8 MB of data sent to the server and 80 MB of data received from the server. These results has been considered as reference results.
  • Microsoft ASP.NET AJAX application, numer (2) in the result chart, has been able to process 222 requests per second with 5 MB of data sent to the server and 129 MB (!) of data received from the server.
  • The Ajaxium version, number (3) in the result chart, has been able to process only 111 request per second. It sent 5 MB and received 47 MB of data.
  • The VWG version, numer (4), did 422 requests per second with 2 MB sent and 23 MB received (!)
  • The javascript version, numer (5), did 400 requests per second, with 2 MB sent to the server and 37 MB received.

Results:

  • despite its simplicity, Ajaxium did unexpectably bad. We belive that the framework initialization takes just too much time (look at the "time to first byte") and this downgrades the performance.
  • Microsoft ASP.NET AJAX didn't really help regarding server-side resource consumption. It also sends large amount of data to the client - in our case this could be because large form has been put into single UpdatePanel. In overall it did a nice job of ajaxing the application but nothing else.
  • The javascript version was considered as an easy winner and, to our suprise, it sends quite a lot of data to the client. Well then, scripts are over 90 kB. Multiply it by 200 clients and you have the bandwitch consumption.
  • The VWG is an undoubt winner. It did a great job of serving the highest number of requests per second, 422, and is also a clear winner regarding the amount of data sent to and from the server. The only problem with VWG is that it is still in beta version and is not reliable for the time (it still has stability and compatibility problems, let's hope it will get better every other release).

What could be interesting is that the VWG version seems to be the slowest one from the end-user's point of view. It just feels sluggish inside the web browser. We belive that this is caused by the constant callbacks to the server and as you can see it does not slow down the processing pipeline at all - it is just slow inside your web browser.

I would like to thank my friend Krzysiek Owczarek for helping me to perform the tests.

 

Update 2007-07-15

Mark Krapovek, Ajaxium Support Manager, made a comment on the testing environment:

"The evaluation edition available freely on vendor's website DOES NOT SUPPORT CONCURRENT CONNECTIONS. That's just a limitation of the evaluation edition. Before testing the component for "200 sessions simulated for 5 concurrent users" you had to ask vendor for a version with all limitations removed.
So: if Ajaxium Evaluation Edition was able to serve more than 100 requests per second, being limited for only 1 connection at a time, it is the fastest component among all compared."

Mark, thank you for the information.