mirror of
https://github.com/github/codeql.git
synced 2026-04-28 18:25:24 +02:00
Python: Copy Python extractor to codeql repo
This commit is contained in:
70
python/extractor/semmle/profiling.py
Normal file
70
python/extractor/semmle/profiling.py
Normal file
@@ -0,0 +1,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)
|
||||
Reference in New Issue
Block a user