Tuesday, September 7, 2010

Message Oriented Architecture (Performance)

Second part of the tutorial about MOA. After a night of test, I can show you the results. They are convincing. I compared the native event system with my implementation. The three main performance indices are the time for sending an event without any listener, the cost to add a listener to an empty list of listener and finally the marginal cost for adding a listener to an existing list of listeners.

Type Native Custom Performance 
without listener 2674.2 ms 1545.4 ms ~175%
first listener 2342.6 ms 1264.5 ms ~185%
add a listener 883.6 ms 145.6 ms ~605%

All tests were done with 1'000'000 send actions. The second and third experiment result are only the additional cost. So we can observe that we gain a lot of time using our custom version. Additionally, the custom version gives you more flexibility for its purpose and throw away the useless things like bubbling, etc. We can set a channel in which we speak, give the number of arguments we want and select a event type. All actions are done in a static manner, so we don't have to bother with EventDispatcher, we only use "iEventListener". As you will see.

Example of structure for the EventCenter
As you can see on the example schema, we use different level of classification. The first level contains the channels. When an event is dispatch, we first select the correct channel. Then the second level is the type of event, each channel has different type of event. You can notice the "Kill_Channel" has no type in the schema, it's because it has no listener. When a listener comes, we will add the types it wants to wait for. With this technique we don't waste neither memory space for each unused event types nor computation time when we dispatch an event that nobody are interested by.
The third level is a list of all current listeners. For the moment, we use a simple ArrayList, but we could also implement a LinkedList to store the listeners and allow us to have constant time insert/delete. But to do that, we need to add some code in each listener. Because we use only an interface to have the biggest flexibility. Using a class like EventListener that contains the next/prev links needed by the linked list, we must use composition instead of simple inheritance.
All the code you need will come with the next tutorial...

No comments:

Post a Comment