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

How can I speed up my rate of sending event to 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 的结果,因为它会告诉您是否可以添加消息到批次(每批次有一个基于大小的限制)。

此外,由于我不知道您的密码:

Each of the Event Hubs client types is safe to cache and use as a singleton for the lifetime of the application, which is best practice when events are being published or read regularly.

(Source)