Python代码在Azure应用服务上运行比本地慢10倍。

huangapple go评论77阅读模式
英文:

Python Code on Azure App Service running 10x slower than locally

问题

def check_regex(voice_input, phrase, errors_allowed=4):
    print("开始正则匹配")
    search_str = "(%s){e<%i}" % (phrase, errors_allowed)
    regex_result = regex.search(
        search_str,
        voice_input,
        flags=regex.IGNORECASE)
    if regex_result is not None:
        print(f"正则匹配成功: {voice_input}{phrase}")
        print(regex_result)
        return True
    else:
        print("无匹配项")
        return False
英文:

I have a small django application that I am hosting on App Service. I noticed it runs dramatically slower on App Service than the exact same code locally. I don't see any errors and the App Services metrics don't look out of the ordinary. Any advice for how to fix this?

Here is an example function that is running 10x slower (less than 1 second for all local tests, up to 10-20 seconds on App Service).

def check_regex(voice_input, phrase, errors_allowed=4):
    print("START REGEX")
    search_str = "(%s){e<%i}" % (phrase, errors_allowed)
    regex_result = regex.search(
        search_str,
        voice_input,
        flags=regex.IGNORECASE)
    if regex_result is not None:
        print(f"REGEX CHECK: {voice_input} and {phrase}")
        print(regex_result)
        return True
    else:
        print("NO MATCH")
        return False

答案1

得分: 1

你可以将编译正则表达式的方法放在函数块之外,这样会预编译该方法,移除打印语句,以减少I/O开销,并优化代码如下所示:

# 在函数外部预编译正则表达式模式
compiled_pattern = regex.compile("({}){{e<{}}}".format(phrase, errors_allowed), flags=regex.IGNORECASE)

def check_regex(voice_input, phrase, errors_allowed=4):
    # 注释掉的打印语句以提高性能
    # print("START REGEX")
    regex_result = compiled_pattern.search(voice_input)
    if regex_result is not None:
        # print(f"REGEX CHECK: {voice_input} and {phrase}")
        # print(regex_result)
        return True
    else:
        # print("NO MATCH")
        return False

为了了解你的应用服务为什么运行缓慢,请尝试 监视 并检查 Web 应用的日志,同时为 Web 应用启用 Application Insights,以获取以下性能图表:

Python代码在Azure应用服务上运行比本地慢10倍。

启用 App Service 日志如下所示:

Python代码在Azure应用服务上运行比本地慢10倍。

访问 Development Tools > Advanced Tool > Go > 在 kudu 控制台中选择 Current Docker logs (Download as zip) 并检查日志,你还可以使用这个 Rest API 调用下面的日志:

Python代码在Azure应用服务上运行比本地慢10倍。

一个方法是选择 Diagnose and Solve Problems 并选择 Availability and Performance 选项卡以获取日志和洞察力:

Python代码在Azure应用服务上运行比本地慢10倍。

Python代码在Azure应用服务上运行比本地慢10倍。

滚动并选择 View solution 以解决 Web 应用程序运行缓慢的问题:

Python代码在Azure应用服务上运行比本地慢10倍。

检查应用程序日志和I/O操作:

Python代码在Azure应用服务上运行比本地慢10倍。

英文:

You can add the compile regex method outside the function block this will pre-compile the method, Remove print statements this will reduce I/O's overhead and optimize the code like below:-

# Pre-compile the regex pattern outside the function
compiled_pattern = regex.compile(&quot;({}){{e&lt;{}}}&quot;.format(phrase, errors_allowed), flags=regex.IGNORECASE)

def check_regex(voice_input, phrase, errors_allowed=4):
    # Commented out print statements for improved performance
    # print(&quot;START REGEX&quot;)
    regex_result = compiled_pattern.search(voice_input)
    if regex_result is not None:
        # print(f&quot;REGEX CHECK: {voice_input} and {phrase}&quot;)
        # print(regex_result)
        return True
    else:
        # print(&quot;NO MATCH&quot;)
        return False

In order to get insights on your App service running slow, Try to Monitor and inspect logs of your Web app and enable Application Insights for your Web app and get the performance graph like below:-

Python代码在Azure应用服务上运行比本地慢10倍。

Enable App Service Logs like below:-

Python代码在Azure应用服务上运行比本地慢10倍。

Visit Development Tools > Advanced Tool > Go > In kudu console select Current Docker logs (Download as zip) and inspect the logs, You can also call the logs below by this Rest API

Python代码在Azure应用服务上运行比本地慢10倍。

One method is to select Diagnose and Solve Problems and select Availability and Performance tab to get the Logs and insight:-

Python代码在Azure应用服务上运行比本地慢10倍。

Python代码在Azure应用服务上运行比本地慢10倍。

Scroll down and select View solution to resolve the slow web app issue:-

Python代码在Azure应用服务上运行比本地慢10倍。

Inspect Application Logs and I/O's:-

Python代码在Azure应用服务上运行比本地慢10倍。

huangapple
  • 本文由 发表于 2023年7月11日 11:05:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658470.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定