设置运行内存_我是如何为Serverless配置内存和超时时间的

之前写过一篇文章:Serverless架构与资源评估:性能与成本探索​

是关于性能和成本的探索,探索之后,就不得不引出一个新的问题:我们在使用Serverless架构的时候,要如何来设置自己的运行内存和超时时间呢?

其实评估的方法有很多,但是今天我想分享一下我的评估方法。

首先,我会将我的函数上线,选择一个稍微大一点的内存,例如,我将我的函数执行一次:

2233a11248c6614ebe3b27460b37ecbd.png

得到这样的结果,我就将我的函数设置为128M或者256M,超时时间设置成3S。

然后我让我的函数跑一段时间,例如我这个接口每天触发次数大约为4000+次:

4e1e7fe9c35081145c8d41d08b91919e.png

我就将这个函数的日志捞出来,写成一脚本,做统计:

import json, time, numpy, base64
import matplotlib.pyplot as plt
from matplotlib import font_manager
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.scf.v20180416 import scf_client, models

secretId = ""
secretKey = ""
region = "ap-guangzhou"
namespace = "default"
functionName = "course"

font = font_manager.FontProperties(fname="./fdbsjw.ttf")

try:
    cred = credential.Credential(secretId, secretKey)
    httpProfile = HttpProfile()
    httpProfile.endpoint = "scf.tencentcloudapi.com"

    clientProfile = ClientProfile()
    clientProfile.httpProfile = httpProfile
    client = scf_client.ScfClient(cred, region, clientProfile)

    req = models.GetFunctionLogsRequest()

    strTimeNow = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time())))
    strTimeLast = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(int(time.time()) - 86400))
    params = {
        "FunctionName": functionName,
        "Limit": 500,
        "StartTime": strTimeLast,
        "EndTime": strTimeNow,
        "Namespace": namespace
    }
    req.from_json_string(json.dumps(params))

    resp = client.GetFunctionLogs(req)

    durationList = []
    memUsageList = []

    for eveItem in json.loads(resp.to_json_string())["Data"]:
        durationList.append(eveItem['Duration'])
        memUsageList.append(eveItem['MemUsage'] / 1024 / 1024)

    durationDict = {
        "min": min(durationList),  # 运行最小时间
        "max": max(durationList),  # 运行最大时间
        "mean": numpy.mean(durationList)  # 运行平均时间
    }
    memUsageDict = {
        "min": min(memUsageList),  # 内存最小使用
        "max": max(memUsageList),  # 内存最大使用
        "mean": numpy.mean(memUsageList)  # 内存平均使用
    }

    plt.figure(figsize=(10, 15))
    plt.subplot(4, 1, 1)
    plt.title('运行次数与运行时间图', fontproperties=font)
    x_data = range(0, len(durationList))
    plt.plot(x_data, durationList)
    plt.subplot(4, 1, 2)
    plt.title('运行时间直方分布图', fontproperties=font)
    plt.hist(durationList, bins=20)
    plt.subplot(4, 1, 3)
    plt.title('运行次数与内存使用图', fontproperties=font)
    x_data = range(0, len(memUsageList))
    plt.plot(x_data, memUsageList)
    plt.subplot(4, 1, 4)
    plt.title('内存使用直方分布图', fontproperties=font)
    plt.hist(memUsageList, bins=20)


    with open("/tmp/result.png", "rb") as f:
        base64_data = base64.b64encode(f.read())

    print("-" * 10 + "运行时间相关数据" + "-" * 10)
    print("运行最小时间:t", durationDict["min"], "ms")
    print("运行最大时间:t", durationDict["max"], "ms")
    print("运行平均时间:t", durationDict["mean"], "ms")

    print("n")

    print("-" * 10 + "内存使用相关数据" + "-" * 10)
    print("内存最小使用:t", memUsageDict["min"], "MB")
    print("内存最大使用:t", memUsageDict["max"], "MB")
    print("内存平均使用:t", memUsageDict["mean"], "MB")

    print("n")

    plt.show(dpi=200)



except TencentCloudSDKException as err:
    print(err)

运行结果:

----------运行时间相关数据----------
运行最小时间:     6.02 ms
运行最大时间:     211.22 ms
运行平均时间:     54.79572 ms


----------内存使用相关数据----------
内存最小使用:     17.94921875 MB
内存最大使用:     37.21875190734863 MB
内存平均使用:     24.83201559448242 MB

01880c47f2dbabf602b417b93059318b.png

通过这样一个结果,可以清楚看出,近500次,每次函数的时间消耗和内存使用。

可以看到时间消耗,基本在1S以下,所以此处超时时间设置成1S基本就是合理的,而内存使用基本是64M以下,所以此时内存设置成64M就可以。

我还有另一个函数:

----------运行时间相关数据----------
运行最小时间:     63445.13 ms
运行最大时间:     442629.12 ms
运行平均时间:     91032.31301886792 ms


----------内存使用相关数据----------
内存最小使用:     26.875 MB
内存最大使用:     58.69140625 MB
内存平均使用:     36.270415755937684 MB

05d0b0adb0f1e955835c5270d70d815f.png

如果上一个函数,是一个非常平稳和光滑的函数,很容易预估资源使用率,那么这个函数则可以很明显看出波动,运行时间层面,可以看到绝大部分在150S以下,部分不到200S,最高峰值近450S,所以这个时候,我们就可以根据我们的业务需求来判定,这个突然之间增加到450S的请求,是否可以被中止,此时,我推荐将这个函数的超时时间设置为200S,至于内存部分,可以看到绝大部分都是40MB以内,部分出现在45-55MB,最高未超过60MB,所以此时可以推荐这个函数设置为64MB。

其实目前来说,云函数在执行时,可能会有一定的波动,所以无论是内存使用还是超时时间,都可能会出现一定的波动,可以根据自身的业务需求来做一些舍弃,将我们的资源使用量压到最低,节约成本。

我的做法基本就是分为两步走:

  • 简单运行两次,评估一下基础资源使用量,然后设置一个稍微偏高的值;
  • 函数运行一段时间,得到一定的样本值,在进行数据可视化和基本的数据分析,得到一个相对稳定权威的数据;

7d1a6f50374bc1817eed30c36aed5ea4.png
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值