profiling: Use mkstemp for profile.data filename.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
This commit is contained in:
Anders Kaseorg 2019-01-14 17:58:03 -08:00 committed by Tim Abbott
parent 601b5eb036
commit 79d888223f
2 changed files with 14 additions and 9 deletions

View File

@ -5,8 +5,10 @@ from mypy_extensions import NoReturn
import glob import glob
import argparse import argparse
import os import os
import shlex
import sys import sys
import subprocess import subprocess
import tempfile
import ujson import ujson
import httplib2 import httplib2
import httpretty import httpretty
@ -441,12 +443,13 @@ if __name__ == "__main__":
failures = True failures = True
if options.profile: if options.profile:
prof.disable() prof.disable()
prof.dump_stats("/tmp/profile.data") with tempfile.NamedTemporaryFile(prefix='profile.data.', delete=False) as stats_file:
print("Profile data saved to /tmp/profile.data") prof.dump_stats(stats_file.name)
print("You can visualize it using e.g. `snakeviz /tmp/profile.data`") 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("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("1.) `vagrant ssh -- -L 8080:127.0.0.1:8080`")
print("2.) `snakeviz -s /tmp/profile.data`") print("2.) `snakeviz -s {}`".format(shlex.quote(stats_file.name)))
if options.report_slow_tests: if options.report_slow_tests:
from zerver.lib.test_runner import report_slow_tests from zerver.lib.test_runner import report_slow_tests

View File

@ -1,5 +1,6 @@
import cProfile import cProfile
import logging import logging
import tempfile
from typing import Any, Dict from typing import Any, Dict
from django.core.management.base import CommandParser from django.core.management.base import CommandParser
@ -41,9 +42,10 @@ def profile_request(request: HttpRequest) -> HttpResponse:
ret = get_messages_backend(request, request.user, ret = get_messages_backend(request, request.user,
apply_markdown=True) apply_markdown=True)
prof.disable() prof.disable()
prof.dump_stats("/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) request_logger.process_response(request, ret)
logging.info("Profiling data written to /tmp/profile.data") logging.info("Profiling data written to {}".format(stats_file.name))
return ret return ret
class Command(ZulipBaseCommand): class Command(ZulipBaseCommand):