提交 b96c6019 编写于 作者: B barrierye

fix profiler bug

上级 fd3ea999
...@@ -23,6 +23,7 @@ elif sys.version_info.major == 3: ...@@ -23,6 +23,7 @@ elif sys.version_info.major == 3:
else: else:
raise Exception("Error Python version") raise Exception("Error Python version")
import time import time
import threading
_LOGGER = logging.getLogger() _LOGGER = logging.getLogger()
...@@ -33,6 +34,7 @@ class TimeProfiler(object): ...@@ -33,6 +34,7 @@ class TimeProfiler(object):
self._print_head = 'PROFILE\tpid:{}\t'.format(self._pid) self._print_head = 'PROFILE\tpid:{}\t'.format(self._pid)
self._time_record = Queue.Queue() self._time_record = Queue.Queue()
self._enable = False self._enable = False
self._lock = threading.Lock()
def enable(self, enable): def enable(self, enable):
self._enable = enable self._enable = enable
...@@ -40,26 +42,29 @@ class TimeProfiler(object): ...@@ -40,26 +42,29 @@ class TimeProfiler(object):
def record(self, name_with_tag): def record(self, name_with_tag):
if self._enable is False: if self._enable is False:
return return
timestamp = int(round(time.time() * 1000000))
name_with_tag = name_with_tag.split("_") name_with_tag = name_with_tag.split("_")
tag = name_with_tag[-1] tag = name_with_tag[-1]
name = '_'.join(name_with_tag[:-1]) name = '_'.join(name_with_tag[:-1])
self._time_record.put((name, tag, int(round(time.time() * 1000000)))) with self._lock:
self._time_record.put((name, tag, timestamp))
def print_profile(self): def print_profile(self):
if self._enable is False: if self._enable is False:
return return
print_str = self._print_head print_str = self._print_head
tmp = {} tmp = {}
while not self._time_record.empty(): with self._lock:
name, tag, timestamp = self._time_record.get() while not self._time_record.empty():
if name in tmp: name, tag, timestamp = self._time_record.get()
ptag, ptimestamp = tmp.pop(name) if name in tmp:
print_str += "{}_{}:{} ".format(name, ptag, ptimestamp) ptag, ptimestamp = tmp.pop(name)
print_str += "{}_{}:{} ".format(name, tag, timestamp) print_str += "{}_{}:{} ".format(name, ptag, ptimestamp)
else: print_str += "{}_{}:{} ".format(name, tag, timestamp)
tmp[name] = (tag, timestamp) else:
print_str = "\n{}\n".format(print_str) tmp[name] = (tag, timestamp)
sys.stderr.write(print_str) print_str = "\n{}\n".format(print_str)
for name, item in tmp.items(): sys.stderr.write(print_str)
tag, timestamp = item for name, item in tmp.items():
self._time_record.put((name, tag, timestamp)) tag, timestamp = item
self._time_record.put((name, tag, timestamp))
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册