我想通过使用await / async来测试程序产生的开销。
为了测试这个,我编写了以下测试类:
public class Entity : INotifyCompletion {
private Action continuation;
private int i;
public void OnCompleted(Action continuation) {
this.continuation = continuation;
}
public Entity GetAwaiter() {
return this;
}
public Entity GetResult() {
return this;
}
public bool IsCompleted { get { return true; } }
public void Execute() {
if (i > 0) Console.WriteLine("What");
}
}
然后我写了一个测试工具。测试工具通过TestA和TestB迭代1600次,仅测量后者1500次(以允许JIT进行预热')。 set
是Entity对象的集合(但实现无关紧要)。该集合中有50,000个实体。测试工具使用Stopwatch
类进行测试。
private static void DoTestA() {
Entity[] objects = set.GetElements();
Parallel.For(0, objects.Length, async i => {
Entity e = objects[i];
if (e == null) return;
(await e).Execute();
});
}
private static void DoTestB() {
Entity[] objects = set.GetElements();
Parallel.For(0, objects.Length, i => {
Entity e = objects[i];
if (e == null) return;
e.Execute();
});
}
这两个例程是相同的,除了一个在调用Execute()之前等待实体(Execute()
没有任何用处,它只是一些愚蠢的代码,以确保处理器真的为每个做一些事情实体)。
在 AnyCPU 的发布模式下执行测试后,我得到以下输出:
>>> 1500 repetitions >>> IN NANOSECONDS (1000ns = 0.001ms)
Method Avg. Min. Max. Jitter Total
A 1,301,465ns 1,232,200ns 2,869,000ns 1,567,534ns ! 1952.199ms
B 130,053ns 116,000ns 711,200ns 581,146ns ! 195.081ms
正如您所看到的,其中等待的方法慢了大约10倍。
据我所知,事情并非如此。等待 - GetResult
永远是真的。这是否意味着即使等待的事情也会执行状态机。已经准备好了吗?
如果是这样,有什么方法吗?我想使用 async / await 的语义,但这个开销对我的应用来说太高了......
编辑:在请求后添加完整的基准代码:
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace CSharpPerfTest {
public class Entity : INotifyCompletion {
private Action continuation;
private int i;
public void OnCompleted(Action continuation) {
this.continuation = continuation;
}
public Entity GetAwaiter() {
return this;
}
public Entity GetResult() {
return this;
}
public bool IsCompleted { get { return true; } }
public void Execute() {
if (i > 0) Console.WriteLine("What");
}
}
static class Program {
static ConcurrentSet<Entity> set;
const int MAX_ELEMENTS = 50000;
// Called once before all testing begins
private static void OnceBefore() {
set = new ConcurrentSet<Entity>();
Parallel.For(0, MAX_ELEMENTS, i => {
set.Add(new Entity());
});
}
// Called twice each repetition, once before DoTestA and once before DoTestB
private static void PreTest() {
}
private static void DoTestA() {
Entity[] objects = set.GetElements();
Parallel.For(0, objects.Length, async i => {
Entity e = objects[i];
if (e == null) return;
(await e).Execute();
});
}
private static void DoTestB() {
Entity[] objects = set.GetElements();
Parallel.For(0, objects.Length, i => {
Entity e = objects[i];
if (e == null) return;
e.Execute();
});
}
private const int REPETITIONS = 1500;
private const int JIT_WARMUPS = 10;
#region Test Harness
private static double[] aTimes = new double[REPETITIONS];
private static double[] bTimes = new double[REPETITIONS];
private static void Main(string[] args) {
Stopwatch stopwatch = new Stopwatch();
OnceBefore();
for (int i = JIT_WARMUPS * -1; i < REPETITIONS; ++i) {
Console.WriteLine("Starting repetition " + i);
PreTest();
stopwatch.Restart();
DoTestA();
stopwatch.Stop();
if (i >= 0) aTimes[i] = stopwatch.Elapsed.TotalMilliseconds;
PreTest();
stopwatch.Restart();
DoTestB();
stopwatch.Stop();
if (i >= 0) bTimes[i] = stopwatch.Elapsed.TotalMilliseconds;
}
DisplayScores();
}
private static void DisplayScores() {
Console.WriteLine();
Console.WriteLine();
bool inNanos = false;
if (aTimes.Average() < 10 || bTimes.Average() < 10) {
inNanos = true;
for (int i = 0; i < aTimes.Length; ++i) aTimes[i] *= 1000000;
for (int i = 0; i < bTimes.Length; ++i) bTimes[i] *= 1000000;
}
Console.WriteLine(">>> " + REPETITIONS + " repetitions >>> " + (inNanos ? "IN NANOSECONDS (1000ns = 0.001ms)" : "IN MILLISECONDS (1000ms = 1s)"));
Console.WriteLine("Method Avg. Min. Max. Jitter Total");
Console.WriteLine(
"A "
+ (String.Format("{0:N0}", (long) aTimes.Average()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) aTimes.Min()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) aTimes.Max()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) Math.Max(aTimes.Average() - aTimes.Min(), aTimes.Max() - aTimes.Average())) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ ((long) aTimes.Sum() >= 10000 && inNanos ? "! " + String.Format("{0:f3}", aTimes.Sum() / 1000000) + "ms" : (long) aTimes.Sum() + (inNanos ? "ns" : "ms"))
);
Console.WriteLine(
"B "
+ (String.Format("{0:N0}", (long) bTimes.Average()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) bTimes.Min()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) bTimes.Max()) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ (String.Format("{0:N0}", (long) Math.Max(bTimes.Average() - bTimes.Min(), bTimes.Max() - bTimes.Average())) + (inNanos ? "ns" : "ms")).PadRight(13, ' ')
+ ((long) bTimes.Sum() >= 10000 && inNanos ? "! " + String.Format("{0:f3}", bTimes.Sum() / 1000000) + "ms" : (long) bTimes.Sum() + (inNanos ? "ns" : "ms"))
);
Console.ReadKey();
}
#endregion
}
}
答案 0 :(得分:3)
如果您的函数has a response time that 1ms for 50,000 calls is considered significant不应该等待该代码,而是同步运行它。
使用异步代码的开销很小,它必须为在内部驱动它的状态机添加函数调用。如果与运行状态机的开销成本相比,您进行异步的工作量也很小,那么如果您的代码应该是异步的,那么您应该重新考虑所需的代码。
答案 1 :(得分:2)
从评论转换为答案:显然,这不是一个干净的基准测试。
如果不需要异步延续,请不要使用它。没有它,代码总是更快。如果你做需要它,那么期望一些开销。当您使用特定的语言/运行时功能时,您应该了解幕后发生的事情。
即使你删除Parallel.For
,将lambdas转换为方法和prevent inlining,仍然会有一些struct
复制和await
延续闭包分配,以支持状态机功能(an example of the generated code)。
更公平的基准将在线程没有同步上下文的情况下使用回调闭包和async/await
测试Task.ContinueWith
与替代实现。我不希望在这种情况下有任何重大差异。
在旁注中,您将async void
lambda Action
传递给Parallel.For
。您应该知道执行控件将在lambda 中的第一个Parallel.For
后立即返回await
,然后它本质上是在Parallel.For
之外发出“即发即弃”的电话。我真的无法想到任何有用的场景。