To find the answer to this ques­tion I put together a very sim­ple test, but before we go into it..

Dis­claimer: this test and its result should be taken at face value only, it is intended to illus­trate that post­ing mes­sages to an agent is super light­weight, but in a real world appli­ca­tion there will be many other con­straints and con­sid­er­a­tions (such as latency, etc.) that should be taken into account before you decide to use F# agent instead of other alternatives.

 

So, back to the test itself, here’s the test code:

image

the line that posts 30 mil­lion mes­sages (4 byte inte­gers in this case) to the agent returned in 0.983 sec­ond, but the agent actu­ally took just over 12 sec­onds to receive and process all the messages.

Suf­fice to say that in most cases try­ing to get mes­sages to a F# agent is unlikely to be the bot­tle­neck in your sys­tem at a solid 30 mil­lion mes­sages a second Winking smile

How­ever, pro­cess­ing of those mes­sages takes sig­nif­i­cantly longer, and if the back­log con­tin­ues to build then even­tu­ally you will be hit with an Out­OfMem­o­ryEx­cep­tion. Also, if you’re depend­ing on a sync/async reply from the agent, latency can also be a big prob­lem if the agent is allowed to build up a huge back­log of mes­sages. Keep these in mind when you’re design­ing your appli­ca­tion around F#’s agents.

 

Finally, for the ques­tion of ‘how many mes­sages can you post to a F# agent in one sec­ond?’, on my Corei7 3.3GHz, the answer is a cool 30 mil­lion Smile

Share

2 Responses to “F# – How many messages can you post to a F# agent in one second?”

  1. That is awe­some. Prob­a­bly a bet­ter ques­tion might be steady state, throt­tled where the garbage col­lec­tor can keep up. Very nice.

  2. I once exper­i­mented with re-writing Async and doing CML-style chan­nels in F#. Got 1M ping/pong inter­ac­tions a sec­ond, on i3. Would be fun if you could run and com­pare on your machine. Code here:

    http://t0yv0.blogspot.com/2011/12/making-async-5x-faster.html

Leave a Reply