If you want to reduce the cost and improve the performance of a function, you can perform specific operations. For example, you can allocate suitable memory for a function in CPU-intensive scenarios and specify proper concurrency values for a function in I/O-intensive scenarios. You can use Power Tuning to easily configure parameters such as memory size and concurrency values based on your business requirements. This topic describes the background information of Power Tuning and provides examples on how to use this feature.

Prerequisites

Make sure that the following operations are complete:

Background information

In serverless computing, the following scenarios exist:
  • CPU-intensive scenarios

    For CPU-intensive scenarios, such as audio and video processing, AI inference and image processing, you can use a single instance to process a single request. In these scenarios, the function memory is proportional to the CPU power. You must select a memory size based on whether the function is cost-sensitive or latency-sensitive.

  • I/O-intensive scenarios

    For I/O-intensive scenarios, you can use a single instance to process multiple requests. In these scenarios, the function memory is proportional to the CPU power. We recommend that you use a large memory for the function. However, if you use a large memory for a function, resources may be wasted. In addition, you may not be able to select a suitable value for single-instance concurrency.

If you cannot configure suitable parameters that meet your business requirements in the preceding scenarios, Serverless Devs provides Power Tuning, which you can use to fine-tune memory and concurrency configurations. Power Tuning helps you obtain parameter configurations that meet your business requirements.

Examples

The following section provides examples on how to select detection modes. You can select a detection mode based on your business requirements.
  • Memory detection
    This example shows how to use the memory detection mode in CPU-intensive scenarios. Run the following command to obtain the optimal memory size:
    s cli fc eval start --region cn-hangzhou --function-name cpu-test --service-name Service  --eval-type memory  --run-count 10   --payload '{"key":"val"}' --memory-size 128,256,512,1024  --access default

    In this example, the function is invoked 10 times at memory sizes of 128 MB, 256 MB, 512 MB, and 1024 MB. The average values are returned. You can compare the average values to obtain the detection information.

    Sample command output:
    [2021-10-26T15:41:34.205] [INFO ] [FC-EVAL] - Reading payload content...
    [2021-10-26T15:41:34.332] [INFO ] [FC-EVAL] - Preparing helper resource for eval.
     Eval complete.
     Eval finally clean ...
    [2021-10-26T15:41:44.267] [INFO ] [FC-EVAL] - Eval result =
    [{"memorySize":128,"useTime":0,"cost":null},{"memorySize":256,"useTime":0,"cost":null},{"memorySize":512,"useTime":0,"cost":null},{"memorySize":1024,"useTime":0,"cost":null}]
    
    >-
      http://memory-tuning.devsapp.cn/#gAAAAQACAAQ=;AICCQwAAmEIAAJxCAADKQg==;27TBNKqeYTShjuc0HeuVNQ==
    After the memory is detected, a URL is generated and returned in the command output. Access the URL in a browser to view a memory diagram, from which you can obtain optimal parameter configurations.shijianhanshutanceneicunmoshi
  • Concurrency detection
    This example shows how to detect the concurrency of an HTTP function in I/O-intensive scenarios. Run the following command to obtain the information about the optimal concurrency value:
    s cli fc eval start --region cn-hangzhou --function-name demo --service-name Service --function-type http --eval-type concurrency --memory 1536 --concurrency-args 2,20,5 --rt 200 --method=get --path /login  --query 'a=1&b=2' --access default

    In this example, the memory size of the function is set to 1.5 GB, the step size is 5, and the concurrency range is [2, 20]. The function is tested under different concurrency values of 2, 7, 12, and 17 to obtain the detection results.

    Sample command output:
    [2021-10-26T14:21:37.874] [INFO ] [FC-EVAL] - Reading payload content...
    [2021-10-26T14:21:37.879] [WARN ] [FC-EVAL] - payload reset empty when method is get!!!
    [2021-10-26T14:21:37.996] [INFO ] [FC-EVAL] - Preparing helper resource for eval.
     Eval complete.
     Eval finally clean ...
    [2021-10-26T14:21:56.724] [INFO ] [FC-EVAL] - Eval result =
    [{"instanceConcurrency":2,"useTime":7.729508196721311,"qps":91},{"instanceConcurrency":7,"useTime":12.062043795620438,"qps":107},{"instanceConcurrency":12,"useTime":19.40979381443299,"qps":187},{"instanceConcurrency":17,"useTime":23.04057279236277,"qps":194}]
    
    >-
      http://concurrency-tuning.devsapp.cn/#AgAHAAwAEQA=;Ilj3QCL+QEFCR5tBGFO4QQ==;AAC2QgAA1kIAADtDAABCQw==
    After the concurrency is detected, a URL is generated and returned in the command output. Access the URL in a browser to view a concurrency diagram, from which you can obtain optimal parameter configurations.bingfadutancehttphanshu

If you want to specify other parameters, see the "Run the s cli fc eval command to detect memory or concurrency of a function" section in User guide.