本文介绍了Azure Functions 吞吐量低的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在评估 Azure Functions 以创建缩略图图像.较大图像的源 URL 放置在存储队列中,并使用带有队列触发器的 C# 函数来处理 URL(从源下载、调整大小并上传到另一个位置).

I'm evaluating Azure Functions to create thumbnail images. The source URLs of larger images are placed in a storage queue, and a C# function with a queue trigger is used to process the URLs (download from source, resize, and upload to another location).

每个函数调用的处理时间不到 500 毫秒,这很好.然而,在运行了一堆测试之后,我发现整体并行处理吞吐量并不是那么好.队列中有 1500-2000 个项目的工作负载,该平台每秒仅执行大约 10 个函数实例.

Each function call takes under 500ms for processing, which is fine. However, after running a bunch of tests, I've found that the overall parallel processing throughput is not that great. With workloads of 1500-2000 items in the queue, the platform only executes around 10 function instances per second.

有什么办法可以横向扩展,让平台同时执行更多的函数实例?

Is there any way to scale out and make the platform execute more function instances concurrently?

推荐答案

Consumption (Dynamic)计划下运行时,当我们看到你的函数是跟不上.这种横向扩展不是即时的,因此您的测试可能在添加更多实例之前或之后不久结束,然后才能看到这些新实例的效果.在应用服务(经典)计划中运行时,您可以预先控制实例的数量,并且可以横向扩展至您需要的数量.

When running under the Consumption (Dynamic) plan, the system will scale out to more instances automatically when we see that your function is not keeping up. This scale out is not instantaneous, so it might be that your test concluded before or shortly after more instances were added before the effects of those new instances could be seen. When running in an App Service (Classic) plan, you control the number of instances up front and can scale out to the number you require.

您可以在 host.json 文件中设置一些队列配置旋钮,这些旋钮会影响 每个 Function App 实例的并行度.在 queues 配置部分下,可以设置 batchSizenewBatchThreshold,例如:

There are a few configuration knobs for queues that you can set in your host.json file that affect the amount of parallelism per Function App instance. Under the queues configuration section, you can set batchSize and newBatchThreshold, e.g.:

{
   "queues": {
      "batchSize": 32,
      "newBatchThreshold": 50
    }
}

batchSize 是每次提取时从队列中提取的消息数.然后并行处理批处理中的所有消息.newBatchThreshold 控制何时获取下一批消息.仅当当前正在处理的消息数量低于此阈值时,才会从队列中获取新一批消息.因此增加 newBatchThreshold 将允许并行处理更多消息.有关这些设置的更多详细信息,请参阅 此处 wiki.

batchSize is the number of messages that are pulled from the queue on each fetch. All the messages in a batch are then processed in parallel. newBatchThreshold governs when the next batch of messages will be fetched. A new batch of messages is only fetched from the queue when the number of messages currently being processed drops below this threshold. So increasing newBatchThreshold will allow more messages to be processed in parallel. See the wiki here for more details on these settings.

请注意,在调整这些设置时,您必须考虑您的工作量.例如,如果您的函数非常占用内存/CPU,则不能在单个实例上并行运行太多函数.所以你可能需要尝试一下.

Note that you have to take your workload into account when adjusting these settings. For example, if your function is very memory/CPU intensive, you can't run too many of them in parallel on a single instance. So you may have to experiment a bit.

除了以上所有内容之外,您还应该确保您的函数是正确的 async 函数,以确保线程不会在 IO 上被不必要地阻塞,并且没有其他潜在的瓶-你的函数代码本身就存在着脖子.

In addition to all of the above, you should also make sure your function is a proper async function, to ensure threads aren't being blocked unnecessarily on IO, and that no other potential bottle-necks exist in your function code itself.

这篇关于Azure Functions 吞吐量低的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 08:51
查看更多