压测即压力测试,通过压力测试您可以获取服务器的最大承受能力,以便实现更好地运营和开发。例如当函数类型是事件函数时,您可以直接通过压测相关命令对指定函数进行压测,无需编辑一个压测脚本。本文介绍Serverless Devs压测的基本原理和如何使用相关命令实现对函数的压测。

前提条件

您已完成以下操作:

基本原理

对于函数计算不同类型的函数来说,一般通过以下方式实现压测:
  • 匿名HTTP函数

    对于匿名HTTP函数,一般直接使用常规的Web测试工具,例如Apache Bench。

  • 事件函数

    对于事件函数,目前只能通过编写压测脚本,才可以实现函数的压测。

为了更加便捷地压测函数,Serverless Devs为您提供了压测功能,方便您一键压测业务函数。

yaceyuanlitushuoming
如上图所示,压测流程如下:
  1. Serverless Devs创建辅助函数并调用(图示中①)。同时,压测参数会被放置在压测函数的输入(例如HTTP函数的请求体或event函数的入参)中。
    说明 辅助服务和辅助函数的名称均为_DEFAULT_FC_STRESS_COMPONENT_SERVICE
  2. 辅助函数被调用后会基于Python Locust对目标函数进行压测(图示中②)。关于Python Locust的详细信息,请参见What is Locust
  3. 辅助函数将压测结果返回到本地(图示中③),并生成一个HTML文件(图示中④)。通过该文件,您可以可视化地查看压测结果的参数信息,例如:
    • Average:请求的平均响应时间。
    • Fails:失败的请求次数。
    • Failures:每秒失败的请求次数。
    • MAX:服务的最大请求时间,单位毫秒。
    • Method:请求类型。
    • Name:压测的请求路径。
    • RPS:每秒的请求次数。
    • Request:总的请求次数。
    • p50:所有请求中有50%的请求响应时间小于此参数的值。

操作步骤

  1. 根据函数类型,选择压测命令,实现压测。
    • 匿名HTTP函数
      当您的函数是匿名的HTTP函数时,您可以执行以下命令,实现压测:
      s cli fc stress start --num-user 6 --spawn-rate 10 --run-time 30 --function-type http --method GET  --url https://188077086902****.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/demo.LATEST/test/ --region cn-shanghai --access default --payload "hello world"
      输出示例:
      [2021-11-03T10:35:38.878] [INFO ] [FC-STRESS] - Reading payload content...
      [2021-11-03T10:35:38.929] [INFO ] [FC-STRESS] - Using stress options:
      numUser: 6
      spawnRate: 10
      runningTime: 30
      functionType: http
      invocationType: Sync
      
      [2021-11-03T10:35:38.930] [INFO ] [FC-STRESS] - Preparing helper reource for stress test.
      ......
      √ Stress test complete.
      [2021-11-03T10:36:12.474] [INFO ] [FC-STRESS] - Displaying the result of stress test.
      Html report flie: /Users/test/.s/cache/fc-stress/html/url#2021-09-10T17-25-15.html//生成的压测文件路径。
      Execute 'open /Users/test/.s/cache/fc-stress/html/url#2021-09-10T17-25-15.html' on macos for html report with browser.
      Average: 17//所有请求的平均响应时间,单位毫秒。
      Fails: 0//当前失败的请求次数。
      Failures/s: 0//每秒失败的请求次数。
      Max: 154//最大的请求服务时间,单位毫秒。
      Method: GET//请求类型。
      Min: 6//最小的请求服务时间,单位毫秒。
      Name: /2016-08-15/proxy/stress-demo/http/ //请求路径。
      RPS: 330.9//每秒请求数。
      Requests: 3306//总的请求数。
      p50: 15//所有请求中有50%的请求响应时间小于15秒。
      p60: 16//所有请求中有60%的请求响应时间小于16秒。
      p70: 18//所有请求中有70%的请求响应时间小于18秒。
      p90: 23//所有请求中有90%的请求响应时间小于23秒。
      p95: 29//所有请求中有95%的请求响应时间小于29秒。
      p99: 82//所有请求中有99%的请求响应时间小于82秒。
    • 事件函数
      s cli fc stress start --num-user 6 --spawn-rate 10 --run-time 30 --function-type event --service-name myService --function-name myFunction --qualifier myQualifier --payload "hello world" --region myRegion --access myAccess
      输出示例:
      [2021-11-01T14:24:55.264] [INFO ] [FC-STRESS] - Reading payload content...
      [2021-11-01T14:24:55.306] [INFO ] [FC-STRESS] - Using stress options:
      numUser: 6
      spawnRate: 10
      runningTime: 30
      functionType: event
      invocationType: Sync
      
      [2021-11-01T14:24:55.308] [INFO ] [FC-STRESS] - Preparing helper reource for stress test.
      ......
      [2021-11-01T14:25:26.700] [INFO ] [FC-STRESS] - Displaying the result of stress test.
      Html report flie: /Users/test/.s/cache/fc-stress/html/test-iiii.LATEST-event#2021-11-01T14-25-26.html
      Execute 'open /Users/test/.s/cache/fc-stress/html/test-iiii.LATEST-event#2021-11-01T14-25-26.html' on macos for html report with browser.
      Average: 12//所有请求的平均响应时间,单位毫秒。
      Fails: 0//当前请求失败的请求次数。
      Failures/s: 0//每秒失败的请求数。
      Max: 227//最大的请求服务时间,单位毫秒。
      Method: POST//请求类型。
      Min: 7//最小的请求服务时间,单位毫秒。
      Name: /2016-08-15/services/test-iiii.LATEST/functions/event/invocations//请求路径。
      RPS: 456.8//每秒请求数。
      Requests: 13703//总的请求数。
      p50: 11//所有请求中有50%的请求响应时间小于11秒。
      p60: 12//所有请求中有60%的请求响应时间小于12秒。
      p70: 12//所有请求中有70%的请求响应时间小于12秒。
      p90: 15//所有请求中有90%的请求响应时间小于15秒。
      p95: 17//所有请求中有95%的请求响应时间小于17秒。
      p99: 45//所有请求中有99%的请求响应时间小于45秒。
    成功执行压测命令后,您可以使用浏览器打开生成的压测文件,可视化的查看压测参数信息。您可以在输出示例中获取压测的文件路径为Html report flie,压测信息如下所示:imageyacejieguo
  2. 可选:执行以下命令,清理辅助函数和本地生成的所有压测文件。
    s cli fc stress clean --region cn-shanghai --access default -y
    输出示例:
    [2021-10-25T14:39:22.028] [INFO ] [FC-STRESS] - Cleaning helper resource and local html report files...
    ......
    [2021-10-25T14:39:22.976] [INFO ] [FC-DEPLOY] - Using region: cn-shanghai
    [2021-10-25T14:39:22.977] [INFO ] [FC-DEPLOY] - Using access alias: default
    [2021-10-25T14:39:22.977] [INFO ] [FC-DEPLOY] - Using accessKeyID: LTAI4G4cwJkK4Rza6xd9****
    [2021-10-25T14:39:22.977] [INFO ] [FC-DEPLOY] - Using accessKeySecret: eCc0GxSpzfq1DVspnqqd6nmYNN****
    [2021-10-25T14:39:23.025] [INFO ] [FC-DEPLOY] - Checking Service _DEFAULT_FC_STRESS_COMPONENT_SERVICE exists
    [2021-10-25T14:39:23.404] [INFO ] [FC-DEPLOY] - Service: _DEFAULT_FC_STRESS_COMPONENT_SERVICE already exists online.
     Using fc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set deploy-type pulumi] to switch.
     Delete function _DEFAULT_FC_STRESS_COMPONENT_SERVICE/_DEFAULT_FC_STRESS_COMPONENT_SERVICE success.
     Delete service _DEFAULT_FC_STRESS_COMPONENT_SERVICE success.
    End of method: stress

更多信息

如果您还需要指定其他的参数,请参见使用s cli fc stress的相关命令实现压测