I have the following xUnit (version 2.3.1) test for sending 100 messages to Kafka
[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();
}
The Send
method code is
public async void Send(MessagingBase.Message message, string topic)
{
CacheMessage(message);
using (var producer = new Producer(Configuration, null, new StringSerializer(Encoding.UTF8)))
{
await 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));
}
CacheMessage
and ProduceAsyncComplete
are currently empty.
Now the stopwatch tells me that the message creation and sending takes about 1s and the messages popup accordingly on the example consumer in a console. However the whole test takes a total of 14s to complete.
Does anyone have an explanation for that?
Is it normal that these callbacks are called with such a great delay?
[UPDATE]
We're getting closer. So I explicitly timed the Callbacks and the 100 callbacks are actually handled within one second as well. This still leaves the question why the test takes so long. Turns out it's not so easy to profile xUnit tests, but I'm on it.
[UPDATE 2]
Out of a whim I removed the using around the producer
variable and now my test is down to 2s - which is what I was expecting.
Somehow the test environment keeps the using block "open" and only closes it later.