5

目前有两个主要选项可以在 Azure IoT Edge 上创建基于代码的自定义模块:

  • 自定义模块(目前是 .NET Core,很快还有 Python 等)
  • Azure Functions(目前仅 .NET Core)

所以现在我的问题是,当我想在 .NET Core (C#) 中编写自定义代码时,使用其中一个有什么好处?

函数所需的样板代码要少得多,但是性能之类的呢?

4

2 回答 2

5

我不知道。让我们板凳!...在 Windows 上,因为那是我得心应手的。CPU是酷睿i7-3770K。

.NET Core Web API(v2.1.3,中间件是任何dotnet new webapi连接)—

public class ValuesController : Controller
{
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }
}

Azure Functions 脚本宿主 (v2.0.11587.0) —

// run.csx
public static IEnumerable<string> Run(HttpRequest req)
{
    return new string[] { "value1", "value2" };
}


// host.json
// Default "consoleLevel" is verbose which blocks on flushing stdout,
// performance suffered unnecessarily so i switched to "error".
{
    "tracing": {
      "consoleLevel": "error",
      "fileLoggingMode": "debugOnly"
    }
}

dotnet 核心和功能主机并排

结果:

// .NET Core Web API
C:\lab\bomb>bombardier-windows-amd64.exe -n 654321 http://127.0.0.1:5000/api/values

Bombarding http://127.0.0.1:5000/api/values with 654321 requests using 125 connections
 654321 / 654321 [===================================] 100.00% 23s
Done!
Statistics        Avg      Stdev        Max
  Reqs/sec     27744.21    6713.91  124074.44
  Latency        4.45ms   200.44us    46.97ms
  HTTP codes:
    1xx - 0, 2xx - 654321, 3xx - 0, 4xx - 0, 5xx - 0
    others - 0
  Throughput:     6.69MB/s



// Functions script host with .csx
C:\lab\bomb>bombardier-windows-amd64.exe -n 654321 http://127.0.0.1:7071/api/HttpTrigger

Bombarding http://127.0.0.1:7071/api/HttpTrigger with 654321 requests using 125 connections
 654321 / 654321 [===================================] 100.00% 5m31s
Done!
Statistics        Avg      Stdev        Max
  Reqs/sec      1976.64     181.69    4213.32
  Latency       63.23ms    20.74ms      2.12s
  HTTP codes:
    1xx - 0, 2xx - 654321, 3xx - 0, 4xx - 0, 5xx - 0
    others - 0
  Throughput:   492.23KB/s

为了科学,我也在 v2 (.NET Core) 和 v1 (.NET Full Framework) 运行时上使用预编译函数 (.DLL) 进行了测试运行。

TL;博士

.NET Core Web API (v2.1.3):                             27744 requests/sec
Functions script host (v2.0.11587.0) .csx:               1976 requests/sec
Functions script host (v2.0.11587.0) precompiled DLL:    2062 requests/sec
Functions script host (v1.0.11535.0) precompiled DLL:    4734 requests/sec

YMMV。如果您实际上处理一些 IO 而不只是从内存中返回 16 个字节,事情可能看起来会有所不同。但是它就是这样啊。如果您不需要 Functions 提供的额外好处,请使用原始 dotnet。

于 2018-03-27T19:04:07.200 回答
1

因为除了性能方面我没有得到任何答案,这是我最后的看法:

  • 在以下情况下,函数非常适合:
    • 首先:代码不需要太频繁地执行(例如不要通过函数路由所有消息),因为性能可能是一个限制因素
    • 您的代码需要某种 HTTP 接口。只需使用 HTTP 触发函数,您无需担心在自定义代码中打开和维护 Web 服务器
    • 您需要执行一些时间触发(计划)的代码。构建一个时间触发函数,一切就绪
    • 你不喜欢任何样板代码;-) 函数很棒,因为你真的只需要编写和维护自己的代码。但由于自定义模块的模板带来了所有必需的样板代码,我认为这并没有太大的优势。

对于任何其他自定义模块,我很可能会去构建你自己的东西。如前所述,从模板开始,编写自己的模块非常简单。

希望对其他人也有帮助!(这不是微软的官方回答;))

您可以在我的 github 存储库中找到一些不同触发边缘函数的示例:https ://github.com/sebader/azureiotedge/tree/master/edge-functions

于 2018-05-28T11:43:29.983 回答