英文:
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,以获取以下性能图表:
启用 App Service 日志如下所示:
访问 Development Tools > Advanced Tool > Go > 在 kudu 控制台中选择 Current Docker logs (Download as zip) 并检查日志,你还可以使用这个 Rest API 调用下面的日志:
一个方法是选择 Diagnose and Solve Problems 并选择 Availability and Performance 选项卡以获取日志和洞察力:
滚动并选择 View solution 以解决 Web 应用程序运行缓慢的问题:
检查应用程序日志和I/O操作:
英文:
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("({}){{e<{}}}".format(phrase, errors_allowed), flags=regex.IGNORECASE)
def check_regex(voice_input, phrase, errors_allowed=4):
# Commented out print statements for improved performance
# 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
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:-
Enable App Service Logs like below:-
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
One method is to select Diagnose and Solve Problems and select Availability and Performance tab to get the Logs and insight:-
Scroll down and select View solution to resolve the slow web app issue:-
Inspect Application Logs and I/O's:-
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论