我一直在尝试设计一个系统,它允许大量并发用户同时在内存中进行表示。在着手设计这个系统时,我立即想到了某种基于演员的解决方案,这是Erlang的一个亲戚。
这个系统必须用.NET来完成,所以我开始使用MailboxProcessor在F#中开发一个原型,但是遇到了严重的性能问题。我最初的想法是每个用户使用一个参与者(MailboxProcessor)来序列化通信--一个用户的通信。
我分离出了一小部分代码,这些代码再现了我所看到的问题:
open System.Threading;
open System.Diagnostics;
type Inc() =
let mutable n = 0;
let sw = new Stopwatch()
member x.Start() =
sw.Start()
member x.Increment() =
if Interlocked.Increment(&n) >= 100000 then
printf "UpdateName Time %A" sw.ElapsedMilliseconds
type Message
= UpdateName of int * string
type User = {
Id : int
Name : string
}
[<EntryPoint>]
let main argv =
let sw = Stopwatch.StartNew()
let incr = new Inc()
let mb =
Seq.initInfinite(fun id ->
MailboxProcessor<Message>.Start(fun inbox ->
let rec loop user =
async {
let! m = inbox.Receive()
match m with
| UpdateName(id, newName) ->
let user = {user with Name = newName};
incr.Increment()
do! loop user
}
loop {Id = id; Name = sprintf "User%i" id}
)
)
|> Seq.take 100000
|> Array.ofSeq
printf "Create Time %i\n" sw.ElapsedMilliseconds
incr.Start()
for i in 0 .. 99999 do
mb.[i % mb.Length].Post(UpdateName(i, sprintf "User%i-UpdateName" i));
System.Console.ReadLine() |> ignore
0仅仅创建100 K演员,我的四核i7就需要800毫秒左右。然后向每个参与者提交UpdateName消息,等待它们完成大约需要1.8秒。
现在,我意识到来自所有队列的开销:在ThreadPool上执行、在MailboxProcessor内部设置/重置AutoResetEvents等等。但这真的是预期的表现吗?通过在MailboxProcessor上阅读MSDN和各种博客,我认为这是erlang演员的亲戚,但从我所看到的糟糕的表现来看,这在现实中似乎不成立吗?
我还尝试了一个修改过的代码版本,它使用了8 MailboxProcessors,每个代码都包含一个Map<int, User>映射,用于通过id查找用户,它带来了一些改进,使UpdateName操作的总时间减少到1.2秒。但是仍然感觉很慢,修改后的代码如下:
open System.Threading;
open System.Diagnostics;
type Inc() =
let mutable n = 0;
let sw = new Stopwatch()
member x.Start() =
sw.Start()
member x.Increment() =
if Interlocked.Increment(&n) >= 100000 then
printf "UpdateName Time %A" sw.ElapsedMilliseconds
type Message
= CreateUser of int * string
| UpdateName of int * string
type User = {
Id : int
Name : string
}
[<EntryPoint>]
let main argv =
let sw = Stopwatch.StartNew()
let incr = new Inc()
let mb =
Seq.initInfinite(fun id ->
MailboxProcessor<Message>.Start(fun inbox ->
let rec loop users =
async {
let! m = inbox.Receive()
match m with
| CreateUser(id, name) ->
do! loop (Map.add id {Id=id; Name=name} users)
| UpdateName(id, newName) ->
match Map.tryFind id users with
| None ->
do! loop users
| Some(user) ->
incr.Increment()
do! loop (Map.add id {user with Name = newName} users)
}
loop Map.empty
)
)
|> Seq.take 8
|> Array.ofSeq
printf "Create Time %i\n" sw.ElapsedMilliseconds
for i in 0 .. 99999 do
mb.[i % mb.Length].Post(CreateUser(i, sprintf "User%i-UpdateName" i));
incr.Start()
for i in 0 .. 99999 do
mb.[i % mb.Length].Post(UpdateName(i, sprintf "User%i-UpdateName" i));
System.Console.ReadLine() |> ignore
0所以我的问题是,我做错什么了吗?我有没有想过MailboxProcessor应该如何使用?或者这是预期的表现。
更新:
于是,我在##fsharp @ irc.freenode.net上找到了一些家伙,他们告诉我,使用sprintf非常慢,而事实证明,这正是我大部分性能问题的根源所在。但是,删除上面的sprintf操作,并且只对每个用户使用相同的名称,我仍然会得到大约400 up的签名操作,这感觉真的很慢。
发布于 2013-07-01 09:25:13
现在,我意识到来自所有队列的开销:在ThreadPool上执行、在MailboxProcessor内部设置/重置AutoResetEvents等等。
还有printf,Map,Seq,以及为你的全球可变的Inc而战。你在泄露堆分配的堆栈帧。实际上,运行基准测试所花费的时间中只有一小部分与MailboxProcessor有关。
但这真的是预期的表现吗?
我对您的程序的性能并不感到惊讶,但是它并没有提到MailboxProcessor的性能。
通过在MailboxProcessor上阅读MSDN和各种博客,我认为这是erlang演员的亲戚,但从我所看到的糟糕的表现来看,这在现实中似乎不成立吗?
MailboxProcessor在概念上有点类似于Erlang的一部分。你所看到的糟糕的表现是由于各种各样的因素造成的,其中一些是非常微妙的,并且会影响到任何这样的程序。
所以我的问题是,我做错什么了吗?
我觉得你做错了几件事。首先,您试图解决的问题并不清楚,因此这听起来像是一个XY问题问题。其次,您正在尝试对错误的事情进行基准测试(例如,您抱怨创建MailboxProcessor所需的微秒时间,但可能只打算在建立一个需要几个数量级的连接时才这样做)。第三,您已经编写了一个基准程序,它度量了某些事物的性能,但是将您的观察归因于完全不同的事情。
让我们更详细地看看您的基准程序。在我们做其他事情之前,让我们先修复一些bug。您应该始终使用sw.Elapsed.TotalSeconds来测量时间,因为它更精确。您应该始终在异步工作流中使用return!而不是do!,否则会泄漏堆栈帧。
我最初的时间表是:
Creation stage: 0.858s
Post stage: 1.18s接下来,让我们运行一个概要文件,以确保我们的程序确实花费了大部分时间在F# MailboxProcessor上。
77% Microsoft.FSharp.Core.PrintfImpl.gprintf(...)
4.4% Microsoft.FSharp.Control.MailboxProcessor`1.Post(!0)显然不是我们希望的那样。从更抽象的角度来看,我们正在使用sprintf之类的东西生成大量数据,然后应用它,但是我们正在一起生成和应用程序。让我们分离出初始化代码:
let ids = Array.init 100000 (fun id -> {Id = id; Name = sprintf "User%i" id})
...
ids
|> Array.map (fun id ->
MailboxProcessor<Message>.Start(fun inbox ->
...
loop id
...
printf "Create Time %fs\n" sw.Elapsed.TotalSeconds
let fxs =
[|for i in 0 .. 99999 ->
mb.[i % mb.Length].Post, UpdateName(i, sprintf "User%i-UpdateName" i)|]
incr.Start()
for f, x in fxs do
f x
...现在我们得到:
Creation stage: 0.538s
Post stage: 0.265s因此,创建速度快了60%,发布速度提高了4.5倍。
让我们尝试完全重写您的基准测试:
do
for nAgents in [1; 10; 100; 1000; 10000; 100000] do
let timer = System.Diagnostics.Stopwatch.StartNew()
use barrier = new System.Threading.Barrier(2)
let nMsgs = 1000000 / nAgents
let nAgentsFinished = ref 0
let makeAgent _ =
new MailboxProcessor<_>(fun inbox ->
let rec loop n =
async { let! () = inbox.Receive()
let n = n+1
if n=nMsgs then
let n = System.Threading.Interlocked.Increment nAgentsFinished
if n = nAgents then
barrier.SignalAndWait()
else
return! loop n }
loop 0)
let agents = Array.init nAgents makeAgent
for agent in agents do
agent.Start()
printfn "%fs to create %d agents" timer.Elapsed.TotalSeconds nAgents
timer.Restart()
for _ in 1..nMsgs do
for agent in agents do
agent.Post()
barrier.SignalAndWait()
printfn "%fs to post %d msgs" timer.Elapsed.TotalSeconds (nMsgs * nAgents)
timer.Restart()
for agent in agents do
use agent = agent
()
printfn "%fs to dispose of %d agents\n" timer.Elapsed.TotalSeconds nAgents此版本要求每个代理在该代理增加共享计数器之前对该代理进行nMsgs,这大大降低了该共享计数器的性能影响。这个程序还检查不同数量的代理的性能。在这台机器上:
Agents M msgs/s
1 2.24
10 6.67
100 7.58
1000 5.15
10000 1.15
100000 0.36因此,你所看到的速度较低的部分原因似乎是超乎寻常的数量(100,000)。对于10.1万个代理,F#实现速度比10万个代理快10倍以上。
因此,如果您能够使用这种性能,那么您应该能够用F#编写整个应用程序,但是如果您需要提供更多的性能,我建议使用另一种方法。您甚至不必通过采用像Disruptor这样的设计来牺牲使用F# (当然也可以用于原型开发)。在实践中,我发现在.NET上进行序列化的时间往往比在F#异步和MailboxProcessor上花费的时间大得多。
发布于 2013-06-28 20:38:58
在消除了sprintf之后,我得到了大约12秒的时间( Mac上的mono没有那么快)。根据Phil提出的使用字典而不是Map的建议,它达到了600‘s。还没有在Win/..Net上试过。
代码更改非常简单,我完全可以接受本地的可更改性:
let mb =
Seq.initInfinite(fun id ->
MailboxProcessor<Message>.Start(fun inbox ->
let di = System.Collections.Generic.Dictionary<int,User>()
let rec loop () =
async {
let! m = inbox.Receive()
match m with
| CreateUser(id, name) ->
di.Add(id, {Id=id; Name=name})
return! loop ()
| UpdateName(id, newName) ->
match di.TryGetValue id with
| false, _ ->
return! loop ()
| true, user ->
incr.Increment()
di.[id] <- {user with Name = newName}
return! loop ()
}
loop ()
)
)
|> Seq.take 8
|> Array.ofSeqhttps://stackoverflow.com/questions/17360286
复制相似问题