How to build a custom model interpreter in a model-driven way
Posted by Andriy Levytskyy in MDD, Uncategorized on July 20th, 2011
Blogs by Johan den Haan, Stefano Butti and Jordi Cabot raised interesting discussions about code generation (CG) and model interpretation (MI). One observation I took from these discussions is that MI is still little known. Previously I demonstrated operation of a custom-made model interpreter for a so-called weighbridge domain. Today I would like to share my experience of building this interpreter in a model-driven way.
MDE Approach
Two main choices underpin the process and technology used to develop the interpreter:
- Execution semantics of the interpreter is defined within the problem domain itself (weighbridge in this case), without translating it to another domain (e.g. .Net or Java) as it is the case with CG. Such definition of semantics is also known as operational semantics. The advantage is reduction of development complexity: out of at least 2 domains needed for CG, only one and the more abstract domain is sufficient.
- Operational semantics is defined within an MDE framework. This enables customization of modeling language for problem domains besides that of the weighbridge example. Moreover, transformation capabilities are used to define operational semantics.

Figure 1: Domain-specific, nested interpretation (DSNI) MDE framework
Figure 1 shows the MDA framework [1] after it has been adapted to reflect the above mentioned choices. (If you are confused between MDA and MDE, you might find this article useful.) In contrast to MDA, there is no PIM or PSM model, but single computational independent model (CIM) written in DSL. CIM is both source and target of Transformation Tool. Transformation Tool carries out transformation classified as same language, same model. Transformation Definition defines operational semantics. It is not important if Transformation Definition Language (TDL), extends the Metalanguage as in MDA or is customizable by means of meta-specification. Therefore TDL is omitted from the framework and TDL selection criteria are defined instead (see below). Finally, new concept System Context is connected to Transformation Tool. This is due to the fact that interpretation as system exhibits external behavior through communication with other systems.
This approach can be described as nested interpretation, where a domain-specific interpreter is executed (nested) by a generic interpreter. From this perspective, Transformation Tool assumes the role of a generic interpreter and execution of Transformation Definition fills in the role of the domain-specific interpreter.
TDL Selection Criteria
Selection criteria for transformation definition language are:
- declarative modeling
- support for domain-specific notation
These criteria help to reduce development complexity and improve communication with problem domain experts.
Selected MD Technology
AToM3 is a free language workbench written in Python and under development at the Modelling, Simulation and Design Lab (MSDL) in the School of Computer Science of McGill University. The workbench closely matches the DSNI framework and meets the TDL selection criteria.
In AToM3, DSLs and models are described as graphs. From a language specification written in the metalanguage (ER formalism), AToM3 generates a tool to visually manipulate (create and edit) models written in the specified DSL. Model transformations are performed by graph rewriting. The transformations themselves can thus be declaratively expressed as graph-grammar models. However, AToM3 provides no communication infrastructure as needed by the framework.
Proof of Concept
As demonstration, a language specification for the weighbridge domain was defined (see sections domain and weighbridge DSL here) and graph rewriting was used to model operational semantics (see below). Source code of AToM3 itself, being written in Python, was extended to support web services for the communication purpose.
Operational Semantics
As blueprint for operational semantics of the interpreter, we took πDemos [2], a small process-oriented discrete event simulation language. There is a number of πDemos events that change state of a weighbridge system. For each such event, [2] defined the transitions induced on system state. While the original used functional programming language, this work uses graph rewriting and a graph grammar (GG) rule is defined per event.
| Priority | GG Rule | Description |
|---|---|---|
| 50 | importProcess | Adds an external vehicle to EL |
| 25 | removeProcess | Deletes a vehicle that has completed its todos (events) |
| 40 | newR | Creates a new weighbridge |
| 40 | decP | Creates a new vehicle class |
| 40 | newP | Creates a vehicle from a vehicle class |
| 40 | getR | Acquires a non-busy weighbridge |
| 40 | blockProcess | Blocks a vehicle acquiring a busy weighbridge |
| 40 | promoteProcess | Unblocks a delayed vehicle |
| 40 | useR | Moves a vehicle on a weighbridge until service is complete |
| 30 | releaseResource | Moves a served vehicle from a weighbridge to EL |
| 41 | putR | Releases an occupied weighbridge |
Table 1: Graph grammar rules of weighbridge events
Table 1 lists the minimum set of required events and their corresponding GG rules. Execution of such rules needs to be globally orchestrated through proper sequencing. The rules, together with execution sequencing, form an operational semantics model of the interpreter.
For complete description of the model, please refer to [3]. In the following, we present a detailed description of an example rule, followed by the execution sequencing model.
Example GG Rule

a) Left-hand side (LHS)

b) Right-hand side (RHS)
Figure 2: Subgraphs of the promoteProcess rule
Rule promoteProcess releases a busy weighbridge (bluish box in Figure 2a) that delays at least one vehicle (yellow box labelled 5). In the new state, the weighbridge remains busy and the blocked vehicle (5) is removed from the head of queue Delay and inserted in waiting queue EL.
The rule is executed if:
- The left-hand side (LHS) shown in Figure 2a is matched in the host graph (the CIM model).
- Associated condition is true: the weighbridge in LHS is the one referred to in the imminent event putR (a todo) in the body (a todo list) of the first vehicle (label 21) in queue EL.
If the above holds, the matched part of the CIM model is substituted with the right-hand side (RHS) shown in Figure 2b. Note new objects are labelled 10, 11, 13. The entities and relationships in RHS are initialized as follows:
- Objects copied from LHS keep all their properties.
- Imminent event putR (a todo) of the current vehicle (21) is completed.
- All properties of blocked vehicle (5) are copied to vehicle (10).
Execution Sequencing
The execution sequencing is based on the next-event approach: Next event to execute is always the imminent event in the body of the current vehicle. Informally, the operational semantics of execution sequencing is as follows: if EL is empty, interpreter idles until at least one vehicle is inserted in EL. Such vehicle becomes current. If the body of the current vehicle is empty then it is removed from EL and EL is examined again. Otherwise, interpreter executes a GG rule corresponding to the imminent event of the current and EL is examined again. Note that whenever interpreter is idle, EL is being updated with new vehicles that meanwhile might have arrived from system context.
The execution sequencing is implemented by organizing GG rules into groups, each group having its own base priority. These groups, in the descending order of priority are: vehicle removal, weighing activities and vehicle arrival. Within a group, each rule is assigned a relative priority. If pattern matching of two and more rules within a group is deterministic on the basis of LHSs and conditions, then these rules can share the same priority level. Example rule priorities are given in Table 1.
Conclusion
The demonstrated development approach is characterized by a very high level of abstraction, direct involvement of problem domain experts and absence of software development. All these factors contribute to fast development times: The lead time of this one man project including research and development was 3 weeks. Admittedly, tests of the produced model interpreter showed noticeable performance penalty due to 1) repurposing of MD technology that was not designed for use as interpreter and 2) the overhead introduced by nested interpretation. In my opinion there is much room for performance improvement and I am wondering if MDE can prove useful again. An important message from this experience is that model interpretation does not have to be prerogative of big commercial tools and can get closer to code generation in terms of accessibility.
References
[1] Anneke G. Kleppe, Jos Warmer and Wim Bast. “MDA Explained: The Model Driven Architecture: Practice and Promise”. Addison-Wesley Longman Publishing Co., Inc., Boston, MA, USA, April 2003.
[2] Graham Birtwistle and Chris Tofts. “An operational semantics of process-oriented simulation languages: Part 1 πDemos”. ACM Transactions on Modeling and Com- puter Simulation, 10(4):299–333, December 1994.
[3] Andriy Levytskyy. “Model Driven Construction and Customization of Modeling & Simulation Web Applications”. PhD thesis, Delft University of Technology, Delft, The Netherlands, January 2009.
Misinterpretatie bij zowel gebruiker als ontwikkelaar beperken
Posted by Wout Lemmens in Interaction Design, Requirements on June 7th, 2011
Misinterpretatie bij zowel gebruiker als ontwikkelaar beperken
Requirements beschrijven waaraan een product moet voldoen, wat het biedt, welke randvoorwaarden er zijn, hoe het eruit gaat zien, voor wie het bedoeld is, hoe het gaat werken, etc. Maar het blijft een beschrijving, met als nadeel dat iedereen die het leest een eigen interpretatie heeft.
Bij de meeste projecten worden diverse scenario’s met de eindgebruiker of de klant nagespeeld om te bepalen of de specificatie aansluit bij de verwachting. Dit naspelen maakt het voor een eindgebruiker inzichtelijk, aangezien het simuleren en visualiseren van het beoogde gedrag veel meer tot de verbeelding spreekt. En dit voorkomt dat de eindgebruiker een eigen interpretatie maakt van de specificatie. Zoals beschreven in een blog van Karl O’Brien over ‘Requirements are done…‘: “It is like watching a movie versus reading the book, everyone can interpret a book in many ways.”.
Echter stopt het vaak bij de simulaties voor de eindgebruikers, terwijl dezelfde manier van visuele simulatie ook voor de ontwikkelaars en testers van groot belang is. Kans op misinterpretatie bij een eindgebruiker is nu beperkt, maar bij de ontwikkelaars en testers is de kans daarop nog steeds erg groot. Resultaat: het product wordt ontwikkeld met de misinterpretatie en sluit nog steeds niet aan bij de eindgebruiker, tot groot onvrede. Want per slot van rekening heeft de gebruiker tijd geïnvesteerd in validatie, maar ziet deze daar onvoldoende van terug.
Werkwijze
Hoe kun je dit concreet vormgeven, zodat naast gebruikers ook de ontwikkelaars en testers hierin meegenomen worden? Enkele voorbeelden van technieken:
Applicatie-flow
De applicatie-flow bevat schermschetsen van alle flow onderdelen, en deze zijn in de juiste volgorde geplaatst en met pijlen aan elkaar gekoppeld. Zo is er inzicht te krijgen in hoe de applicatie gaat werken.
Bij een van onze projecten hebben we de complete applicatie-flow uitgeprint en groot aan de muur gehangen. Wanneer er een nieuw onderdeel door de ontwikkelaars opgepakt werd, kon de werking daarvan goed uitgelegd worden aan de hand van de applicatie-flow. Ook bij onduidelijkheden waren de ontwikkelaars met regelmaat bij de applicatie-flow-muur te vinden.

Webflow aan de muur
Paper prototypen
Paper prototyping stelt ons in staat om heel snel ideeën te verifiëren met klanten en gebruikers. Zoals de term al suggereert wordt het prototype van de software gemaakt van papier, gebruikmakend van bijvoorbeeld PostIt notes, gekleurd papier, schaar en markers. Maar ook het tekenen of knippen/plakken van schermelementen kan goed werken. De verschillende schermonderdelen worden in papier uitgevoerd en op het ’scherm’ (een vel papier of het bureaublad) geplaatst. Vervolgens kan de gebruiker met de ’software’ gaan werken. Wij zijn de ‘computer’, en reageren op de acties van de gebruiker zoals de software het uiteindelijk ook zou gaan doen. Deze manier van werken is goed om te valideren bij een klant, en de uitkomst kan vervolgens via het paper prototype aan engineers getoond worden.

Paper prototype
Wire frames
Het visualiseren van concepten in schetsmatige schermen dwingt ontwikkelaars beslissingen te nemen over functionaliteiten en structuur, en brengt problemen in een vroeg stadium aan het licht. De visualisaties maken het ook mogelijk om de concepten te communiceren naar mensen buiten het team, bijvoorbeeld klanten of gebruikers.
Tenslotte vormen visualisaties zeer expliciete ontwerpdocumentatie; een goede serie (eventueel pixel-perfect) schetsen laat engineers weinig te raden over.

Wire frames als flow diagram
Simulaties
Veel van het gedrag kan via de bovenstaande manieren inzichtelijk gemaakt worden. Maar het blijft bijzonder moeilijk om interactie aspecten via statische plaatjes duidelijk te maken of tekstueel te beschrijven, laat staan dat gebruikers en ontwikkelaars het vervolgens juist kunnen interpreteren.
Door simulaties in te zetten kan met een clickable prototype, video of animatie veel beter bepaald worden of een interactie aspect werkt zoals het bedacht is. Enerzijds kan aan gebruikers getoond worden hoe bepaald gedrag zal gaan werken, en anderzijds is het ook uitermate geschikt voor engineers om het beoogde gedrag correct te implementeren.
Voordeel
Deze technieken voorkomen of beperken misinterpretatie, niet alleen bij de eindgebruikers, maar ook bij de ontwikkelaars en testers. De hele ontwikkelketen dient dit inzicht te hebben, want “de ketting is zo sterk als de zwakste schakel”.
Threading made easy in .Net 4.0 (2/2)
Posted by admin in .NET, Uncategorized, technical on February 26th, 2011
Threading made Easy (2/2)
This blog is part two of a little example I wrote to test the new threading stuff in .Net 4.0. To read part 1 follow this link.
Short recap from part 1 first. This is what I wrote:
The thing I have to do is get some data from the web in the following pseudo manner:
foreach subject from the list
search for the last 100 tweets on this subject
The part left over was to show some results from the threaded calls from part 1. It so happens to be that, somewhere near the end, I showed the following call as the most nicest and clean threading mechanism:
public static void GetParallel() { Parallel.ForEach(UrlList, url => { Get(url); } ); }
Threadsafe collections
From the example it shows we’re not doing anything useful with the result which is returned by Twitter. A byte array is returned and the thing I want to do is to store the byte array in a collection of byte arrays. Pretty simple huh? Well almost because a potential threading problem is kickin’ in. The standard collections in .Net are not threadsafe. We used to write code with a locking object and lock the piece of code which makes changes to the collection. The new .Net 4.0 has a more smooth solution to this problem: System.Collections.Concurrent.BlockingCollection. And the code above is changed in:
// use a thread safe collection for all threads BlockingCollection<byte[]> data = new BlockingCollection<byte[]>(); Parallel.ForEach(UrlList, url => { data.Add(Get(url)); } ); // return an ordinary collection (non thread safe) to the outside world return data.ToList<byte[]>();
So each thread use a thread safe collection to store its results and when we’re ready with all threads the non-threadsafe counterpart is returned because when processing the result we don’t need any multithreaded handling.
Twitter data structure
Twitter returns its data in JSON format. The search API returns the data in the following structure (see also the Twitter API documentation), here is an example:
{
"results":
[
{
"text":"@twitterapi http:\/\/tinyurl.com\/ctrefg",
"to_user_id":396524,
"to_user":"TwitterAPI",
"from_user":"jkoum",
"metadata":
{
"result_type":"popular",
"recent_retweets": 109
},
"id":1478555574,
"from_user_id":1833773,
"iso_language_code":"nl",
"source":"<a href="http:\/\/twitter.com\/">twitter<\/a>",
"profile_image_url":"http:\/\/s3.amazonaws.com\/twitter_production\/profile_images\/118412707\/2522215727_a5f07da155_b_normal.jpg",
"created_at":"Wed, 08 Apr 2009 19:22:10 +0000"
},
... truncated ...
],
"since_id":0,
"max_id":1480307926,
"refresh_url":"?since_id=1480307926&q=%40twitterapi",
"results_per_page":15,
"next_page":"?page=2&max_id=1480307926&q=%40twitterapi",
"completed_in":0.031704,
"page":1,
"query":"%40twitterapi"
}
The list of byte arrays we have is a list of bytes in JSON format. Each byte array represents the results of a query and consists of some meta data describing the query and a list with references to all tweets returned by the search query. Each of these byte arrays needs to be transformed in something more meaningful and useful. Here JSON comes to the rescue, it is not only blazingly fast it is also simple. All we have to do is just define the interesting elements in the structure and the serialization will do its work, silently and fast. The structure in a .Net understandable format is:
[DataContract] public class TwitterSearchResultList { [DataMember] public TwitterSearchResult[] results { get; set; } [DataMember] public string query { get; set; } [DataMember] public double completed_in { get; set; } } [DataContract] public class TwitterSearchResult { [DataMember] public string from_user { get; set; } [DataMember] public string text { get; set; } [DataMember] public string profile_image_url { get; set; } }
Transforming one byte array with twitter results is now as easy as:
List<TwitterSearchResultList> tweets = new List<TwitterSearchResultList>(); foreach (byte[] tweetlist in allTweetLists) { MemoryStream tweetStream = new MemoryStream(tweetlist); DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(TwitterSearchResultList)); TwitterSearchResultList tsrl = (TwitterSearchResultList)serializer.ReadObject(tweetStream); }
Rendering to html
To render the TwitterSearchReturnList to a presentable format I generated some html. This can be done much cleaner and nicer with XSLT and/or CSS but for the sake of the example I just coded the bare minimum. It looks like:
StringBuilder = new StringBuilder(); stringBuilder.Append("<TABLE>"); foreach (TwitterSearchResult tweet in list.results) { stringBuilder.Append("<TR>"); // the tweeters' avatar stringBuilder.AppendFormat("<TD><IMG src=\"{0}\" width=\"48\" heigth=\"48\" /></TD>", tweet.profile_image_url); // tweeters' name stringBuilder.AppendFormat("<TD>{0}</TD>", tweet.from_user); // tweet text; with all urls as an href stringBuilder.AppendFormat("<TD>{0}</TD>", Regex.Replace(tweet.text, "(http:/[\\S/]*)", "<a href=\"$1\">$1</A>")); stringBuilder.Append("</TR>"); } stringBuilder.Append("</TABLE>");
Resulting in the following output:

Source code
The full source code for this example can be downloaded here.
Thank you for reading, questions will be answered, suggestions are more than welcome!
have fun,
florisz
New version of TopThreads JConsole plugin
Posted by Peter Doornbosch in java on January 26th, 2011
Some time ago, I created the “TopThreads” plugin for JConsole, that helps you to determine why your Java application is causing such a high CPU load, by showing the most busy threads in your application and giving you the opportunity to inspect thread-stacktraces at the same time. It turned out to be quite usefull and from the responses I got, I can tell people find it still usefull today. A few days ago, I released a new version of this plugin, with one very usefull new feature: CPU usage.
Top thread?
If you’ve used the topthreads plugin, you probably seen this before: suddenly, a thread that is not supposed to be very busy, pops up at the top of the table with usage figures in the 90’s. You wonder WTF is going on, that this thread is taking so much CPU power, until you realize that this figure is only relative to the rest of the application threads. And if the application is hardly doing anything, threads that do a little more than anything might get alarming high figures (and red color). After i ran into this pitfall a few times, i decided i needed to know an absolute usage figures too.

If you enable this feature (settings -> show process cpu usage too), the top row of the table shows the CPU usage of the process as a whole. This is simply the sum of the CPU usage of all threads. The percentage shown in this row however, is the percentage this process is using the CPU, which should be approximately the same value a process viewer like top, Activity Monitor or the TaskManager would report. Although this is not always the case – more about that in a minute – it’s at least a good indication whether the process is busy or idle. And even though it may not always be as acurate as i would like it to be, it proved itself to be proficiant to help me avoid confusion.
The usual suspect: the garbage collector
In normal situations (whatever that me be…
), the CPU usage figure is approximately the same as the figures other tools report. However, especially when the process is very busy, the CPU usage shown is far too low. After some testing, i’m rather confident that this is mainly caused by the garbage collector. As it turns out, TopThreads does not get information for all the JVM threads, which can easily be verified by comparing a thread dump with the thread listing in JConsole. For example, threads that never appear in JConsole (not in the TopThreads tab, but neither in the JConsole thread view) are the “Low Memory Detector”, compiler threads (HotSpot), “Signal Dispatcher” and “Surrogate Locker Thread (CMS)” and the garbage collector threads (the mark-and-sweep thread and the parallel gc threads). I can image that some of these threads can put a lot of load on the CPU when the application is very busy. And one thing is for sure: the cpu cycles that are taken by these threads are not counted in the totals that the TopThreads plugin computes, simply because it doesn’t know about these.
Despite these shortcomings, i find the new feature quite usefull myself. Let me know what you think.
Other improvements in this release:
- the initial poll time is not fixed to 10 seconds, but depends on the (initial) number of threads. For small apps, the updates will be much more frequent.
- there are more preferences to set and these are moved to a separate settings dialog. Settings are stored using the Java Preferences API.
- improved stacktrace panel behavivour, including automatic scroll to the top.
- better handling of security exceptions, that might occur when connecting to a remote VM.
Please let me know what you think, feedback is always welcome!
Threading made easy in .Net 4.0 (1/2)
Posted by Floris Zwarteveen in .NET, Uncategorized on January 8th, 2011
Threading made Easy (1/2)
In my opinion it isn’t possible to have these two words in one sentence. I’ve been exposed to thread programming numerous times in the past. Despite the almost heroic nature of being a ‘thread programmer’, I’ve never had the guts to expose this in public. Brag over a few beers about success stories on how well my thread-aware and thread-safe software was running? It has never been part of my vocabulary. One simple reason: it’s way too complicated. Well, at least for me. Even with the aid of thorough design, good examples and extensive mock tests my code remained complicated and incomprehensible. And yes, I tried to refactor, redesign, recode, restructure, whatever, I never could get it right.
Well, up until .Net 4 and its component: System.Threading.Tasks. Included in the very core of the framework. Right out of the box, support for threading for the masses.
Why threading?
It is good to step back for one moment. The 1-million-dollar-question to always ask yourself is: do I really need it? The simple answer is: yes. Because why would you let anyone wait while your machine is sitting there doing nothing? Especially when it is not necessary to wait.
So: yes I need threading to make software as performant as can be to the best of my abilities as a software engineer.
Do I always have to use threading? Of course not. In its most essential form, current CPU’s can do multiple things at the same time (especially when having more cores) and are very fast and therefore often waiting for some other things (like database or web I/O stuff) to end before they can continue. So if you’re in such a situation, and I can assure you, this is most often, threads come to the rescue to really utilize the CPU to its full capacity.
The example
Let’s apply threading to a simple example to show how the new .Net threadinglayer can be utilized to increase performance 40-70% in a very easy manner.
Suppose you want to do a multiple query on Twitter (most popular demo environment) in which you need to return the first 100 hits for a couple of subjects. I have tried this with my webbrowser and you have to do some manual work to return such a list in one piece.
Beforehand I do know getting 100 tweets from Twitter isn’t that difficult, especially with their JSON based REST API (that’s a lot of cool keywords in one sentence!). So one task is easy to implement. I also know that getting something from the internet usually sets my CPU in a comfortable waiting state because network transfer is still way slower than my CPU. So the task is also suitable for multiplexing a couple of times.
Some preparation
First I have to define my testcases. I don’t want to get bogged down into testclasses (although I do agree it would have been better to use them) because hey, this is demo code anyway.
The thing I want to show you is:

In short, I want to show the performance differences between four separate scenarios
- A non threaded one
- A classic, pre 4.0, threadpool version
- A 4.0 version using the new TaskFactory
- And finally the version using the new Parallel class
These scenarios all do exactly the same thing; only the scheduling of the tasks differs. The thing they have to do is get some data from the web in the following pseudo manner:
foreach subject from the list
search for the last 100 tweets on this subject
The search API in Twitter is pretty straightword. For example, to perform the above for the subject ‘luminis’ the following url will suffice:
http://search.twitter.com/search.json?&q=luminis&rpp=100
To get data from the web I’ve defined a small routine which returns a byte array with the required data. Fortunately WCF made getting data from the web pretty straightforward in .Net, this resulted in the very simple basic routine:
private static byte[] Get(string url) { byte[] data = null; WebClient wc = new WebClient(); try { data = wc.DownloadData(url); } catch { // probable time out, for this demo just quit } finally { wc.Dispose(); } return data; }
This routine is part of a class called WebContent. This class also has a public property UrlList which can contain the list of urls for which the content must be read.
private static List _UrlList = new List(); public static List UrlList { get { return _UrlList; } }
What I have to do now is to iterate over this list and call my simple Get routine for each of the four scenarios.
#1: The non-threaded version
The simple version is the one without any threading at all. We do need this one to baseline our tests so we can do a comparison with the threaded counterparts. The only thing we need to do is read all the urls and the code pretty much resembles the pseudo code shown previously:
public static void GetSequential() { foreach (string url in UrlList) { Get(url); } }
#2: The 2.0 ThreadPool version
Actually I didn’t want to make the ThreadPool version because as said earlier I don’t get this ‘old’ thread programming very well. But as a reference I’ve hacked a version for which I can’t take any liabilities. It is just here for reference purposes. Note that I need two routines to achieve the same goal. Additionally I’ve changed the thread of the main to [MTAThreadAttribute] otherwise you will receive to message:

Anyway the code looks like:
public static void GetWithThreadPool() { _ResetEvents = new ManualResetEvent[UrlList.Count]; Random rand = new Random(); for (int i = 0; i < UrlList.Count; i++) { _ResetEvents[i] = new ManualResetEvent(false); ThreadPool.QueueUserWorkItem(new WaitCallback(DoThreadPoolWork), (object)i); } WaitHandle.WaitAll(_ResetEvents); } private static void DoThreadPoolWork(object o) { int index = (int)o; Get(UrlList[index]); _ResetEvents[index].Set(); }
Pretty impressive but incomprehensible, huh?
It doesn’t feel right that you have to add all these complex things just to get a bit of multi-threading. To me, the only easy part is the Get call somewhere near the end. All the other stuff is thread overhead. And if you would need more that 25 threads this code won’t work. But let’s not dwell on these passed issues and move over to the 4.0 versions.
#3: The 4.0 TaskFactory version
The task factory shows what the thread overhead could be if the framework is more powerful than the 2.0 version. Still it is some overhead but the code looks more like the pseudo code than the previous implementation:
public static void GetWithTaskFactory() { Task[] tasks = new Task[UrlList.Count]; int counter = 0; foreach (string url in UrlList) { var task = Task.Factory.StartNew(() => Get(url), TaskCreationOptions.LongRunning); tasks[counter] = task; counter++; } Task.WaitAll(tasks); }
The thing I need to do is to create a task for each subject and, with a lambda expression, call my good old private Get() on this task. Finally I just have to wait until all tasks are finished.
#4: The 4.0 Parallel version
The ultimate version is left to the end. The new Parallel class makes it possible to write threaded code without almost no (thread) overhead. It looks like:
public static void GetParallel() { Parallel.ForEach(UrlList, url => { Get(url); } ); }
Isn’t that just beautiful? It also resembles the pseudo code pretty well. Okay you have to understand the lambda expression with the anonymous delegate syntax and semantics but if you do it is just an ordinary for-loop. As far as I’m concerned: this is as it should be.
Performance results
So far the differences in the source code of the four implementations. To start each of the separate scenarios a little bit of timing code is needed to calculate the total of milliseconds the code runs.
private delegate void GetContentDelegate(); private static string DoTimedOperation(GetContentDelegate getForumContentDelegate) { DateTime start = DateTime.Now; // perform the actual operation getForumContentDelegate(); DateTime end = DateTime.Now; TimeSpan elapsed = end - start; return elapsed.TotalMilliseconds.ToString() + "ms"; }
And with the aid of a delegate the timing function is called four times as in:
private void StartSequential_Click(object sender, EventArgs e) { this.lblSequentialTime.Text = DoTimedOperation(new GetContentDelegate(WebContent.GetSequential)); } private void StartThreadPool_Click(object sender, EventArgs e) { this.lblThreadPoolTime.Text = DoTimedOperation(new GetContentDelegate(WebContent.GetWithThreadPool)); } private void StartTaskFactoryl_Click(object sender, EventArgs e) { this.lblTaskFactoryTime.Text = DoTimedOperation(new GetContentDelegate(WebContent.GetWithTaskFactory)); } private void StartParallel_Click(object sender, EventArgs e) { this.lblParallelTime.Text = DoTimedOperation(new GetContentDelegate(WebContent.GetParallel)); }
I ran a few tests on my machine and one of them resulted in the following numbers: (By the way my machine is a Macbook Pro running Windows 7 in a Virtual Box with 1 processor and 2048 Mb memory.)

Of course you have to try this on your own machine but it is safe to say that the performance increase is worth the overhead in thread programming.
I have analyzed the thread behavior with the Performance Monitor (PerfMon). In the analysis I was interested in:
- # of processor threads (blue)
- # of logical threads for the process (green)
- # of physical threads for the process (red)
These are the results of one test I have run:

You can see the framework too needs some threads when the non threaded version is started. When the threadpool scenario starts you clearly see the increase in threads. The threadpool is managed by .Net so this amount doesn’t decrease when the scenario is finished. This does happen with the third scenario. Here the threads are in control of the routine and you can clearly see that the threads are released (at least to the system, the framework has a different view on it). In the fourth scenario again the threads are fully controlled by the framework.
Interesting to see is: in both the threadpool and the parallel scenario only a limited amount of threads are created (approximately 5 or 6) while in the Taskfactory 12 threads are created (for each subject one). It is hard to say what exactly happens behind the scenes. Do all logical threads have a physical thread associated? And do these threads run ’simultaneously’ or not? I have to say I don’t know.
Then there is one thing I don’t understand at all. How is it possible (near the end) that the total number of physical threads of the process is higher than the system amount of physical threads?! If somebody can tell me this I would be very hapy!
Conclusion
In a next blog I will extend this little example. So far I have concentrated on threads, starting and synchronizing them to show the performance differences. I have experimental proof it is worth taking an effort to learn more on the new .Net 4.0 threading stuff.
The part that is left out is the handling of the data and the presentation of the results.
This part is described in part 2 and can be found here.
have fun, florisz
Download the zip for the full source code here.
References
MSDN on System.Threading.Tasks
Howto use MvcContrib.Pagination with a ViewModel
Posted by Erik Sanders in .NET on December 31st, 2010
Howto use MvcContrib.Pagination with a ViewModel
I was looking for a simple solution for paging in a ASP.MVC2 project. Though we already use MVCContrib.Grid this was the first place I searched.
The solution MVCContrib offers is elegant in the way that they separated the concept of paging, a ui element to show paging (next , previous, …) and a grid to show the content. This separation allows us to using paging on a custom table based page as well.
Because we are using ViewModels a little extra effort was needed to retain the paging information from the domain layer in the view layer.
Step by step:
* Get a Querable from repository using Domain Objects
IQueryable<DomainObject> domainObjects = rep.GetAll()
* Filter and Sort using Linq
domainObjects = domainObjects.Where(...).OrderBy(...)
* Get the data
IPagination<DomainObject> pageOfData domainObjects.AsPagination( page, pageSize)
* Map to the ViewModel
IPagination<DomainObjectViewModel> model = DomainObjectViewModel.Map( pageOfData )
* Mapping needs to retain the page information from the domain
var list = new List<DomainObjectViewModel>();
foreach (var domainObject in domainObjects)
{
list.Add(Map(domainObject));
}
new CustomPagination<DomainObjectViewModel>(list.AsEnumerable(),
pageOfData.PageNumber,
pageOfData.PageSize,
pageOfData.TotalItems);
* Show the information on a page in a grid
<%= Html.Grid(Model).AutoGenerateColumns() %>
* Show the pager
<%= Html.Pager(Model).First( "<<").Last(">>").Next(">").Previous("<")
.Format( "Item {0} - {1} van {2} ") %>
Heroku
Posted by Albert Oudenampsen in Cloud on December 29th, 2010
De laatste jaren houd ik me meer met requirements bezig dan met programmeren. Soms bouw ik een prototype of een proof-of-concept en dan gebruik ik meestal Ruby-on-Rails. Dat ontwikkelt zo lekker snel.
Ik zou natuurlijk ook Groovy-on-Grails kunnen gebruiken, dat gaat net zo snel maar daar zitten van die zware java en hibernate libraries onder. Rails daarintegen is een lekker lichtgewicht taaltje en de ontwikkelomgeving (ik gebruik TextMate) start zelfs op mijn bejaarde mac binnen enkele seconden. Bovendien is de syntax van ruby erg intuitief. Ideaal voor roestige programmeurs zoals ik.
Tot nog toe was het probleem alleen: hoe maak ik mijn prototype beschikbaar voor mijn klanten? Systeembeheerders worden niet echt vrolijk als ze de webserver moeten inrichten voor rails applicaties.
Maar….. dan is er Heroku, een “Platform as a Service” speciaal voor rails applicaties. Hoe bruikbaar is dat?
Met “Platform as a service” heb ik altijd de angst dat je in je code al rekening moet houden met platform. Oftewel, als je voor een bepaalde PaaS kiest, dat zit je eraan vast. Maar Heroku is een verrassend elegante implementatie. Binnen 15 minuten had ik me geregistreerd, SSH keys aangemaakt, mijn applicatie neergezet en de database geconfigureerd. En bovenal, code aanpassingen waren niet nodig.
Zo doe je dat:
Stap 1. Maak een Rails applicatie.
Stap 2. Maak een GIT repository aan. Vanzelfsprekend is het nog handiger om dat te doen tijdens het ontwikkelen van de rails applicatie, maar anders gaat dat met drie kreten in een terminal window.
git init
git add .
git commit -m initial
Verder heb je nog een GitHub account en SSH keys nodig. Als je dat nog niet hebt dan staat hier beschreven hoe dat moet: http://help.github.com/mac-key-setup/ (Windows en Linux beschijvingen zijn er natuurlijk ook).
Stap 3. Zet alles op Heroku.
Eerst als nieuwe klant registreren bij Heroku. Dan een gem installeren:
>sudo gem install heroku
De SSH key bij heroku bekend maken:
>heroku keys:add
Create een omgeving in Heroku: >
> heroku create
Creating furious-sword-84.... done
Created http://furious-sword-84.heroku.com/ | git@heroku.com:furious-sword-84.git
Git remote heroku added
Heroku bedenkt prachtige krijgshaftige namen voor de applicatie, zoals hier “furious-sword-84″, de naam kan je natuurlijk aanpassen. Ik heb de naam laten staan en op http://furious-sword-84.heroku.com kan je nu mijn InsultMe applicatie vinden. InsultMe heeft overigens geen enkele serieuze bedoeling, het is slechts mijn versie van HelloWorld.
Nu de sources overbrengen naar Heroku:
>git push heroku master
De database tabellen aanmaken:
> heroku rake db:migrate
En nog wat initiele vulling voor de tabellen door middel van fixtures:
> heroku rake db:fixtures:load
Of de locale database overzetten naar de Heroku database:
> heroku db:push
Een database terughalen kan natuurlijk ook:
> heroku db:pull
That’s all!
Bekende key-factoren voor succesvolle “as a Service” producten zijn “eenvoud”, “prijs” met in ieder geval een gratis instapniveau en “Kwaliteit”. Heroku voldoet ruimschoots aan de “eenvoud” factor. Voor de “free” versie mag de database 5MB groot worden (dat zou wat meer mogen zijn) en krijg je de beschikking over 1 Dyno. De performance van die ene Dyno lijkt overeen te komen met de applicatie draaien onder WEBrick op mijn MacBook (ja, ook als ik het gebruik met een serieuzere applicatie dan InsultMe). Voor grotere applicaties is het eenvoudig om wat op-te-schalen. Ik heb me nooit verdiept in PaaS-pricing, maar de kosten hiervoor lijken me reëel en komen in de buurt van de MS-Azure prijzen (ik hoor het graag als dit niet zo is).
En over “Kwaliteit”: het moet werken, gewoon altijd. Ik heb nog niet meegemaakt dat mijn applicaties niet te benaderen waren. Ook hiervoor ben ik benieuwd naar ervaringen van anderen.
Apache Celix – Introduction
Posted by Alexander Broekhuis in Celix, OSGi on December 21st, 2010
Apache Celix
Last summer I started working on a project to see if it is possible to implement OSGi concepts in C. After a simple Proof of Concept the idea grew to see if it is possible to not only use the OSGi concepts, but actually implement the OSGi specification. Since the OSGi specification is written for Java, simply implementing it in C cannot be done. So an adaptation is needed.
This adaptation will be based on the previously mentioned proof of concept, and, after being accepted by the Apache Incubator PMC, hosted at apache as an Incubator project named Apache Celix.
With a series of posts I will detail how Celix implements the OSGi specification, where and why it deviates from the original specification. But for a start, why is it created and what will it deliver?
Why?
OSGi provides a dynamic, extensible and easily maintainable environment for complex and distributed architectures. But with one large restriction, it is written for Java based systems only. For embedded/realtime systems there is no alternative, and yet the need for a well defined and modular architecture is becoming more and more important. Besides the dynamics, such architecture also makes it possible to reuse parts of a system, which reduces the time needed to implement and maintain software.
Why OSGi?
Celix uses C functions (Dynamic Loading) that have been around for a long time, so why follow the OSGi specification, and create abstractions to be able to expose the dynamic concepts like OSGi does?
There are three reasons for this, the first being the fact that OSGi solves much more than existing C functions do. For example, dynamic loading only solves how a library can be added at runtime, but for a complete solution a registry is needed to keep track of available libraries and services provided by these libraries. The OSGi specification provides a good and proven specification for this.
This is also where the second reason emerges, since the OSGi specification is implemented by several vendors (commercial and open source), and being used by many organizations and people, it is a well known and proven API. Following this API makes it easier to understand and use Celix.
Finally, implementing the OSGi Compendium specifications for Remote Services also makes it possible to create a seamless, yet flexible, integration between Celix and OSGi implementations.
Why Apache?
As mentioned before, Apache Celix is open sourced at the Apache Incubator. This is mainly for two reasons:
-
Community
As a middleware project, we think Celix can benefit from a large community. Apache provides the means to support such a community.
-
Apache Felix
Several parts of Celix are a direct port of Apache Felix, and with Felix being hosted at Apache, it is a logical choice to also open source Celix at Apache.
While Apache provides a great basis for Celix as an Open Source project, the community still has to grow. Celix is a new project, and user, committers as well as testers are still needed and always welcome. As with all Apache project, communication is done using mailing lists, to subscribe to the development list of Celix send an email to <celix-dev-subscribe@incubator.apache.org>.
The original proposal can be found on the wiki of the Apache Incubator and the status of Celix in the Apache Incubator can be tracked on the project page for Celix.
What will it deliver?
Eventually, the goal is to have an implementation of the full OSGi Core, and part of the Compendium specification in C. But as with all good things, the OSGi specification was written over a longer period, and expecting Celix to implement everything at once, and still deliver a usable product in a reasonable time would be naive.
So for a first release the following goals are set:
-
Provide a basic implementation of the OSGi Framework API
-
Provide an implementation of Remote Services
For both Celix <-> Celix but also Celix <-> OSGi
While this is a short list, actually implementing the OSGi Framework API entails a lot of functionality. In the coming blog posts I will detail parts of the specification, and how they are implemented in Celix.
Call for help
Finally, I would like to invite everyone to join the Apache Celix community, and provide us with feedback, ideas, bugs, and maybe even bugfixes and additions to the current code base.
Apachecon NA 2010, some trends
Posted by Angelo van der Sijpt in Uncategorized on December 7th, 2010
With Apachecon NA 2010 about a month behind us, I’d like to share some of the trends I picked up there.
Big Data, the cloud
One of the main trends I noticed is the interest in Big Data (mainly Apache Cassandra) and Big Data processing in various shapes and forms (e.g. Apache Lucene, Apache Hadoop). In relation to many of these, we find a ring of ‘cloudness’: the products tend to allow distribution and replication of data and functionality.
But it doesn’t stop there. It’s no surprise to find cloud-references in talks about Apache Tuscany (including a talk on Building cloud native software, which I regrettably missed), but for instance Tomcat is making its move into cloud territory with Stratos.
OSGi all around
Of course I have a vested interest in OSGi, and my talk during the OSGi track on friday shows this. However, apart from in its own track, OSGi and OSGi-based technologies popped up in a number of other tracks. To name a few,
- Apache Sling is an OSGi based web application platform, and showed up in the Content Technologies track,
- Apache Geronimo 3.0 (in the Content Technologies track) is now based on OSGi, and
- in the Enterprise Track, a number of sessions were decidated to Apache Aries, which focuses on the OSGi Enterprise specification.
It is an interesting development that OSGi is now mainly being referenced in the web- and enterprise spaces, whereas it started out as a specification for embedded devices.
Does this mean that OSGi is really getting traction in the software community? Yes and no. I believe the thing that is really getting traction is the notions of modularity on the one hand, and µServices on the other. OSGi is currently the main technology that marries those two notions.
Business is not a dirty word
I noticed there were roughly three kinds of talks,
- Community talks are all about how Apache works, and how open source software fits into the world around it,
- Technology talks focus on some Apache project, or a combination of projects, and go into the technical details, and
- Industry talks that show how the projects are used in industry.
The last kind of talk shows how industry, the ‘people with the problem’ use open source technology to run their business. No, I probably don’t really care about the products and services you deliver, but I am very interested in your case studies in using open source in your daily life.
A few days ago, Slashdot ran a story Paid Developers Power the Linux Kernel. In the light of sessions on open source in industry (I counted about half a dozen on this year’s schedule), this is not that surprising: if you’re a good enough developer to become a committer in an open source project, it’s very unlikely you’re working as a janitor during the daytime. All of us work for a corporation in some way, and many of us work on open source project during our paid time. I believe this is a good thing!

Nowadays DSLs seem to be everywhere. If 5 years ago DSL was an exotic word in the UML dominated model driven world, today it has established a strong following. A recent research on how MDE is used in industry [1], indicated that nearly 40% of respondents use in-house DSLs (alongside of other languages). The in-house qualifier is important, as these DSLs are very likely to be developed with metamodels. In such cases, a quality benchmark may help language development. Yet, it is not easy to find such a benchmark, let alone one that is widely accepted.