为什么这个测试要花这么长时间?

Mar*_*bel 5 c# xunit .net-core kafka-producer-api confluent-platform

我有以下 xUnit(版本 2.3.1)测试,用于向 Kafka 发送 100 条消息

[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();
}
Run Code Online (Sandbox Code Playgroud)

方法Send代码为

public async void  Send(MessagingBase.Message message, string topic)
{
    CacheMessage(message);
Run Code Online (Sandbox Code Playgroud)

using (var Producer = new Producer(Configuration, null, new StringSerializer(Encoding.UTF8))) { 等待 Producer.ProduceAsync(topic, null,message.Content).ContinueWith(antecedant => ProduceAsyncComplete(message)); }

    var producer = new Producer<Null, string>(Configuration, null, new StringSerializer(Encoding.UTF8));

    await producer.ProduceAsync(topic, null,message.Content).ContinueWith(antecedant => ProduceAsyncComplete(message));

}
Run Code Online (Sandbox Code Playgroud)

CacheMessage目前是ProduceAsyncComplete空的。

现在秒表告诉我,消息创建和发送大约需要 1 秒,并且消息会相应地在控制台中的示例使用者上弹出。然而整个测试总共需要14秒才能完成。

有人对此有解释吗? 这些回调的调用有这么大的延迟是正常的吗?

[更新]

我们越来越近了。因此,我明确地对回调进行了计时,并且 100 个回调实际上也在 1 秒内处理。这仍然留下了一个问题:为什么测试需要这么长时间。事实证明,分析 xUnit 测试并不那么容易,但我正在努力。

[更新 2] 出于一时兴起,我删除了producer变量周围的使用,现在我的测试降至 2 秒 - 这是我所期望的。

测试环境以某种方式使 using 块保持“打开”状态,并且稍后才将其关闭。