Skip to content

Instantly share code, notes, and snippets.

@bent-rasmussen
Last active September 20, 2020 21:10
Show Gist options
  • Save bent-rasmussen/af0470305800f048b3041b872309882a to your computer and use it in GitHub Desktop.
Save bent-rasmussen/af0470305800f048b3041b872309882a to your computer and use it in GitHub Desktop.
MailboxProcessor perfornance test to compare against the high-performance task-and-channel-based version (separate Gist).
// MailboxProcessor test for comparison against the performance-optimized task-and-channel-based actor impl. here:
//
// https://gist.github.com/bent-rasmussen/8152df3c842a06acabe219dd877802d5
//
// Tested in LINQPad (hence Dump method usage).
type Message =
| Incr
| Not of bool * AsyncReplyChannel<bool>
| Counter of AsyncReplyChannel<int>
let agent = MailboxProcessor.Start(fun inbox ->
let mutable counter = 0
let rec loop () = async {
match! inbox.Receive() with
| Incr ->
counter <- counter + 1
| Not (b, reply) ->
reply.Reply(not b)
| Counter ch ->
ch.Reply(counter)
do! loop()
}
loop()
)
let clock = Stopwatch.StartNew()
for i = 1 to 1_000_000 do
agent.Post(Incr)
let elapsed = clock.Elapsed
let counter = agent.PostAndReply(Counter)
counter.Dump("Counter")
clock.Elapsed.TotalSeconds.Dump("Seconds elapsed")
async {
let clock = Stopwatch.StartNew()
let mutable b = false
for i = 1 to 1_000_000 do
let! b' = agent.PostAndAsyncReply(fun reply -> Not (b, reply))
b <- b'
let elapsed = clock.Elapsed
counter.Dump("b")
clock.Elapsed.TotalSeconds.Dump("Seconds elapsed")
}
|> Async.RunSynchronously
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment