Ai
3 Star 6 Fork 0

Gitee 极速下载/viztracer

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/gaogaotiantian/viztracer
克隆/下载
base_tmpl.py 3.21 KB
一键复制 编辑 原始数据 按行查看 历史
# Licensed under the Apache License: http://www.apache.org/licenses/LICENSE-2.0
# For details: https://github.com/gaogaotiantian/viztracer/blob/master/NOTICE.txt
import gc
import io
import json
import logging
import os
import sys
import time
from unittest import TestCase
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s %(levelname)s: %(message)s",
)
class BaseTmpl(TestCase):
trace_test_time = os.getenv("GITHUB_ACTION") and not os.getenv("COVERAGE_RUN")
pkg_config = None
@classmethod
def setUpClass(cls):
if cls.trace_test_time:
cls._test_time_events = []
@classmethod
def tearDownClass(cls):
if cls.trace_test_time:
if os.path.exists("test_time_trace.json"):
with open("test_time_trace.json", "r") as f:
trace = json.load(f)
else:
trace = {"traceEvents": []}
trace["traceEvents"].extend(cls._test_time_events)
with open("test_time_trace.json", "w") as f:
json.dump(trace, f)
def setUp(self):
logging.info("=" * 60)
logging.info(f"{self.id()} start")
self.stdout = io.StringIO()
self.stdout_orig, sys.stdout = sys.stdout, self.stdout
if self.trace_test_time:
self._test_start_time = time.time()
def tearDown(self):
if self.trace_test_time:
test_duration = time.time() - self._test_start_time
self._test_time_events.append({
"name": self.id(),
"cat": "test",
"ph": "X",
"ts": self._test_start_time * 1e6,
"dur": test_duration * 1e6,
})
sys.stdout = self.stdout_orig
logging.info(f"{self.id()} finish")
gc.collect()
def dbgPrint(self, *args, **kwargs):
print(*args, file=self.stdout_orig, **kwargs)
def assertEventNumber(self, data, expected_entries):
entries = [entry for entry in data["traceEvents"] if entry["ph"] != "M"]
entries_count = len(entries)
self.assertEqual(entries_count, expected_entries,
f"Event number incorrect, {entries_count}(expected {expected_entries}) - {entries}")
def assertFileExists(self, path, timeout=None, msg=None):
err_msg = f"file {path} does not exist!"
if msg is not None:
err_msg = f"file {path} does not exist! {msg}"
if timeout is None:
if not os.path.exists(path):
raise AssertionError(err_msg)
else:
start = time.time()
while True:
if os.path.exists(path):
return
elif time.time() - start > timeout:
raise AssertionError(err_msg)
else:
time.sleep(0.5)
def assertFileNotExist(self, path):
if os.path.exists(path):
raise AssertionError(f"file {path} does exist!")
def assertTrueTimeout(self, func, timeout):
start = time.time()
while True:
try:
func()
break
except AssertionError as e:
if time.time() - start > timeout:
raise e
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C/C++
1
https://gitee.com/mirrors/viztracer.git
git@gitee.com:mirrors/viztracer.git
mirrors
viztracer
viztracer
master

搜索帮助