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

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

Dis­claimer: this test and its result should be tak­en at face val­ue only, it is intend­ed 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 oth­er con­straints and con­sid­er­a­tions (such as laten­cy, etc.) that should be tak­en into account before you decide to use F# agent instead of oth­er alter­na­tives.


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


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­al­ly took just over 12 sec­onds to receive and process all the mes­sages.

Suf­fice to say that in most cas­es try­ing to get mes­sages to a F# agent is unlike­ly to be the bot­tle­neck in your sys­tem at a sol­id 30 mil­lion mes­sages a sec­ond Winking smile

How­ev­er, pro­cess­ing of those mes­sages takes sig­nif­i­cant­ly longer, and if the back­log con­tin­ues to build then even­tu­al­ly 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, laten­cy 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.


Final­ly, 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