From 79d888223f18d4ba49e54ca6aaf8d5c22751665e Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Mon, 14 Jan 2019 17:58:03 -0800 Subject: [PATCH] profiling: Use mkstemp for profile.data filename. Signed-off-by: Anders Kaseorg --- tools/test-backend | 15 +++++++++------ zilencer/management/commands/profile_request.py | 8 +++++--- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/tools/test-backend b/tools/test-backend index ee861c75f5..500c462d25 100755 --- a/tools/test-backend +++ b/tools/test-backend @@ -5,8 +5,10 @@ from mypy_extensions import NoReturn import glob import argparse import os +import shlex import sys import subprocess +import tempfile import ujson import httplib2 import httpretty @@ -441,12 +443,13 @@ if __name__ == "__main__": failures = True if options.profile: prof.disable() - prof.dump_stats("/tmp/profile.data") - print("Profile data saved to /tmp/profile.data") - print("You can visualize it using e.g. `snakeviz /tmp/profile.data`") - print("Note: If you are using vagrant for development environment you will need to do:") - print("1.) `vagrant ssh -- -L 8080:127.0.0.1:8080`") - print("2.) `snakeviz -s /tmp/profile.data`") + with tempfile.NamedTemporaryFile(prefix='profile.data.', delete=False) as stats_file: + prof.dump_stats(stats_file.name) + print("Profile data saved to {}".format(stats_file.name)) + print("You can visualize it using e.g. `snakeviz {}`".format(shlex.quote(stats_file.name))) + print("Note: If you are using vagrant for development environment you will need to do:") + print("1.) `vagrant ssh -- -L 8080:127.0.0.1:8080`") + print("2.) `snakeviz -s {}`".format(shlex.quote(stats_file.name))) if options.report_slow_tests: from zerver.lib.test_runner import report_slow_tests diff --git a/zilencer/management/commands/profile_request.py b/zilencer/management/commands/profile_request.py index 4cd9b516b8..2779803544 100644 --- a/zilencer/management/commands/profile_request.py +++ b/zilencer/management/commands/profile_request.py @@ -1,5 +1,6 @@ import cProfile import logging +import tempfile from typing import Any, Dict from django.core.management.base import CommandParser @@ -41,9 +42,10 @@ def profile_request(request: HttpRequest) -> HttpResponse: ret = get_messages_backend(request, request.user, apply_markdown=True) prof.disable() - prof.dump_stats("/tmp/profile.data") - request_logger.process_response(request, ret) - logging.info("Profiling data written to /tmp/profile.data") + with tempfile.NamedTemporaryFile(prefix='profile.data.', delete=False) as stats_file: + prof.dump_stats(stats_file.name) + request_logger.process_response(request, ret) + logging.info("Profiling data written to {}".format(stats_file.name)) return ret class Command(ZulipBaseCommand):