如何加快向 AzureEventHub 发送事件的速度?

问题描述

我正在使用这个简单的 foreach 循环将事件发送到 Azure EventHub,问题是它看起来失败很慢并且每秒只发送 1-2 个事件,所以我觉得我一定遗漏了一些东西,我该如何加快速度?我是异步发送它们还是我做错了什么?

 if (!apiInputPutCalendarService.TimeSlotIdsToClose.Any())
                {
                    return new BadRequestObjectResult(new
                    {
                        Status = "NOK",Error = "There are no timeslots to close",Result = ""
                    });
                }
                else
                {
                    foreach (String calendarTimeSlotId in apiInputPutCalendarService.TimeSlotIdsToClose)
                    {
                        EventHubCDSSoftDeleteTriggerModel scanMsg = new EventHubCDSSoftDeleteTriggerModel
                        {
                            TimeSlotId = calendarTimeSlotId
                        };

                        var scanMessageJsonString = JsonConvert.SerializeObject(scanMsg);

                        await EventHubHelper.SendEventToEventHubAsync(_eventHubClient,scanMessageJsonString);

                        log.Loginformation($"Message: {scanMessageJsonString} sent!");
                    }
                }

这么小的简单消息,我预计每秒至少发送 100 条消息

在这里遗漏了什么?

解决方法

那么,您是否测量了日志记录和序列化以及调用 TimeSlotIdsToClose 的影响?另外,我会先不发送消息,而是批量发送消息:

await using (var producerClient = new EventHubProducerClient(connectionString,eventHubName))
        {
            // Create a batch of events 
            using EventDataBatch eventBatch = await producerClient.CreateBatchAsync();

            // Add events to the batch. An event is a represented by a collection of bytes and metadata. 
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("First event")));
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Second event")));
            eventBatch.TryAdd(new EventData(Encoding.UTF8.GetBytes("Third event")));

            // Use the producer client to send the batch of events to the event hub
            await producerClient.SendAsync(eventBatch);
            Console.WriteLine("A batch of 3 events has been published.");
        }

(source)

请注意,上面的代码没有重用EventHubProducerClient,这是推荐的,并且您不应该忽略TryAdd的te结果,因为它会告诉您是否可以将消息添加到批次(每批次有基于大小的限制)。

另外,因为我不知道你的代码:

每种事件中心客户端类型都可以安全地缓存并在应用程序的整个生命周期内用作单例,这是定期发布或读取事件时的最佳做法。

(Source)