forked from github/codeql
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathprofiling.py
70 lines (53 loc) · 1.75 KB
/
profiling.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from . import util
import os.path
import sys
from time import time
import collections
__all__ = [ 'get_profiler' ]
class NoProfiler(object):
'''Dummy profiler'''
def __init__(self):
pass
def __enter__(self):
return self
def __exit__(self, *args):
pass
class StatProfiler(object):
''' statprof based statistical profiler'''
def __init__(self, outpath):
self.outpath = outpath
def __enter__(self):
statprof.start()
return self
def __exit__(self, *args):
statprof.stop()
with open(self.outpath, "w") as fd:
statprof.display(fd)
def get_profiler(options, id, logger):
'''Returns a profile based on options and version. `id` is used to
label the output file.'''
global statprof
if options.profile_out:
if sys.version_info >= (3,0):
logger.warning("Cannot create profiler: statprof is Python2 only.")
else:
try:
import statprof
util.makedirs(options.profile_out)
outpath = os.path.join(options.profile_out, "profile-%s.txt" % id)
logger.info("Writing profile information to %s", outpath)
return StatProfiler(outpath)
except ImportError:
logger.warning("Cannot create profiler: no statprof module.")
except Exception as ex:
logger.warning("Cannot create profiler: %s", ex)
return NoProfiler()
class MillisecondTimer(object):
def __init__(self):
self.elapsed = 0.0
def __enter__(self):
self.start = time()
return self
def __exit__(self, *_):
self.elapsed += (time() - self.start)*1000
timers = collections.defaultdict(MillisecondTimer)