C# 为什么这次考试要花这么长时间?
我对向卡夫卡发送100条消息进行了以下xUnit(版本2.3.1)测试C# 为什么这次考试要花这么长时间?,c#,.net-core,xunit,kafka-producer-api,confluent-platform,C#,.net Core,Xunit,Kafka Producer Api,Confluent Platform,我对向卡夫卡发送100条消息进行了以下xUnit(版本2.3.1)测试 [Fact] public void Test1() { Stopwatch sw = new Stopwatch(); sw.Start(); var config = new Dictionary<string, object> { { "group.id", "gid" },
[Fact]
public void Test1()
{
Stopwatch sw = new Stopwatch();
sw.Start();
var config = new Dictionary<string, object>
{
{ "group.id", "gid" },
{ "bootstrap.servers", "localhost" },
{ "enable.auto.commit", true },
{ "default.topic.config", new Dictionary<string, object>()
{
{ "message.timeout.ms", 500 }
}
},
};
var connection = new KafkaConnection(config, new ShreddingQueueCache());
for (byte i = 0; i < 100; i++)
{
connection.Send(new Message(new Guid(1, 2, 3, new byte[] { 0, 1, 0, 1, 0, 1, i, 1 }), "content : content" + i), "imp");
}
Debug.WriteLine(sw.ElapsedMilliseconds);
sw.Stop();
}
使用(var producer=newproducer(配置,null,newstringserializer(Encoding.UTF8)))
{
等待producer.ProduceAsync(主题,null,message.Content).ContinueWith(antecedant=>ProduceAsyncComplete(message));
}
var producer=newproducer(配置,null,newstringserializer(Encoding.UTF8));
等待producer.ProduceAsync(主题,null,message.Content).ContinueWith(antecedant=>ProduceAsyncComplete(message));
}
CacheMessage
和ProduceAsyncComplete
当前为空
现在秒表告诉我消息的创建和发送大约需要1s,消息会相应地在控制台中的示例消费者上弹出。
然而,整个测试总共需要14秒才能完成
有人对此有解释吗?
以如此大的延迟调用这些回调是否正常
[更新]
我们越来越近了。因此,我显式地对回调进行计时,100个回调实际上也在一秒钟内得到处理。
这仍然留下了一个问题,为什么测试需要这么长时间。事实证明,评测xUnit测试并不是那么容易,但我正在努力
[更新2]
出于一时兴起,我删除了producer
变量周围的using,现在我的测试下降到2s,这正是我所期望的
不知何故,测试环境会保持using块“打开”,直到稍后才将其关闭。尝试使用探查器。类似的操作可能会帮助您:
public async void Send(MessagingBase.Message message, string topic)
{
CacheMessage(message);
var producer = new Producer<Null, string>(Configuration, null, new StringSerializer(Encoding.UTF8));
await producer.ProduceAsync(topic, null,message.Content).ContinueWith(antecedant => ProduceAsyncComplete(message));
}