import os
import pytest
import sqlite3
import subprocess
import sys
import threading
sys.path.append("..")
from tools.utils import run_and_get_output
MID_TRACE_EXPECTED_SIZE_2 = 1 * 1024
SMALL_TRACE_EXPECTED_SIZE_2 = 1024 * 1024 * 1024
def task():
run_and_get_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_hiperf.txt -o /data/local/tmp/test_hiperf.htrace -t 10 -s -k"')
def task2():
run_and_get_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_hiperf2.txt -o /data/local/tmp/test_hiperf2.htrace -t 10 -s -k"')
def task3():
run_and_get_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_hiperf3.txt -o /data/local/tmp/test_hiperf3.htrace -t 10 -s -k"')
def task4():
run_and_get_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_hiperf4.txt -o /data/local/tmp/test_hiperf4.htrace -t 10 -s -k"')
def task5():
run_and_get_output(f'hdc shell "hiprofiler_cmd -c /data/local/tmp/config_hiperf5.txt -o /data/local/tmp/test_hiperf5.htrace -t 10 -s -k"')
class TestHiprofilerhiperfPlugin:
@pytest.mark.L0
def test_hiperf_plugin(self):
subprocess.run(r'hdc file send ..\inputfiles\hiperf_plugin\config_hiperf.txt /data/local/tmp',
text=True, encoding="utf-8")
task_thread = threading.Thread(target=task, args=())
task_thread.start()
task_thread.join()
subprocess.run(r'hdc file recv /data/local/tmp/test_hiperf.htrace ..\outputfiles',
text=True, encoding="utf-8")
subprocess.run(
r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_hiperf.htrace -e ..\outputfiles\test_hiperf.db')
conn = sqlite3.connect(r'..\outputfiles\test_hiperf.db')
cursor = conn.cursor()
check = False
db_relative_path = "..\\outputfiles\\test_hiperf.htrace"
absolute_path = os.path.abspath(db_relative_path)
db_size = os.path.getsize(absolute_path)
assert db_size > MID_TRACE_EXPECTED_SIZE_2
table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')]
cursor.execute('SELECT * FROM perf_callchain order by ip limit 0,10')
result = cursor.fetchall()
if len(result):
check = True
for row in result:
assert (row[1] > 0)
assert (row[7] > 0)
cursor.execute('SELECT * FROM perf_files')
result2 = cursor.fetchall()
if len(result2):
check = True
assert check
@pytest.mark.L0
def test_hiperf_plugin2(self):
subprocess.run(r'hdc file send ..\inputfiles\hiperf_plugin\config_hiperf2.txt /data/local/tmp',
text=True, encoding="utf-8")
task_thread = threading.Thread(target=task2, args=())
task_thread.start()
task_thread.join()
subprocess.run(r'hdc file recv /data/local/tmp/test_hiperf2.htrace ..\outputfiles',
text=True, encoding="utf-8")
subprocess.run(
r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_hiperf2.htrace -e ..\outputfiles\test_hiperf2.db')
conn = sqlite3.connect(r'..\outputfiles\test_hiperf2.db')
cursor = conn.cursor()
check = False
db_relative_path = "..\\outputfiles\\test_hiperf2.htrace"
absolute_path = os.path.abspath(db_relative_path)
db_size = os.path.getsize(absolute_path)
assert db_size < SMALL_TRACE_EXPECTED_SIZE_2
table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')]
cursor.execute('SELECT * FROM perf_callchain')
result = cursor.fetchall()
if len(result) == 0:
check = True
assert check
cursor.execute('SELECT * FROM perf_files')
result = cursor.fetchall()
if len(result) == 0:
check = True
assert check
@pytest.mark.L0
def test_hiperf_plugin3(self):
subprocess.run(r'hdc file send ..\inputfiles\hiperf_plugin\config_hiperf3.txt /data/local/tmp',
text=True, encoding="utf-8")
task_thread = threading.Thread(target=task3, args=())
task_thread.start()
task_thread.join()
subprocess.run(r'hdc file recv /data/local/tmp/test_hiperf3.htrace ..\outputfiles',
text=True, encoding="utf-8")
subprocess.run(
r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_hiperf3.htrace -e ..\outputfiles\test_hiperf3.db')
conn = sqlite3.connect(r'..\outputfiles\test_hiperf3.db')
cursor = conn.cursor()
check = False
db_relative_path = "..\\outputfiles\\test_hiperf3.htrace"
absolute_path = os.path.abspath(db_relative_path)
db_size = os.path.getsize(absolute_path)
assert db_size < SMALL_TRACE_EXPECTED_SIZE_2
table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')]
cursor.execute('SELECT * FROM perf_callchain')
result = cursor.fetchall()
if len(result) == 0:
check = True
assert check
cursor.execute('SELECT * FROM perf_files')
result = cursor.fetchall()
if len(result) == 0:
check = True
assert check
@pytest.mark.L0
def test_hiperf_plugin4(self):
subprocess.run(r'hdc file send ..\inputfiles\hiperf_plugin\config_hiperf4.txt /data/local/tmp',
text=True, encoding="utf-8")
task_thread = threading.Thread(target=task4, args=())
task_thread.start()
task_thread.join()
subprocess.run(r'hdc file recv /data/local/tmp/test_hiperf4.htrace ..\outputfiles',
text=True, encoding="utf-8")
subprocess.run(
r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_hiperf4.htrace -e ..\outputfiles\test_hiperf4.db')
conn = sqlite3.connect(r'..\outputfiles\test_hiperf4.db')
cursor = conn.cursor()
check = False
db_relative_path = "..\\outputfiles\\test_hiperf4.htrace"
absolute_path = os.path.abspath(db_relative_path)
db_size = os.path.getsize(absolute_path)
assert db_size < SMALL_TRACE_EXPECTED_SIZE_2
table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')]
cursor.execute('SELECT * FROM perf_callchain')
result = cursor.fetchall()
if len(result) == 0:
check = True
assert check
cursor.execute('SELECT * FROM perf_files')
result = cursor.fetchall()
if len(result) == 0:
check = True
assert check
@pytest.mark.L0
def test_hiperf_plugin5(self):
subprocess.run(r'hdc file send ..\inputfiles\hiperf_plugin\config_hiperf5.txt /data/local/tmp',
text=True, encoding="utf-8")
task_thread = threading.Thread(target=task5, args=())
task_thread.start()
task_thread.join()
subprocess.run(r'hdc file recv /data/local/tmp/test_hiperf5.htrace ..\outputfiles',
text=True, encoding="utf-8")
subprocess.run(
r'..\inputfiles\trace_streamer_db.exe ..\outputfiles\test_hiperf5.htrace -e ..\outputfiles\test_hiperf5.db')
conn = sqlite3.connect(r'..\outputfiles\test_hiperf5.db')
cursor = conn.cursor()
check = False
db_relative_path = "..\\outputfiles\\test_hiperf5.htrace"
absolute_path = os.path.abspath(db_relative_path)
db_size = os.path.getsize(absolute_path)
assert db_size > MID_TRACE_EXPECTED_SIZE_2
table_list = [a for a in cursor.execute('SELECT name FROM sqlite_master WHERE type = "table"')]
cursor.execute('SELECT * FROM perf_callchain order by ip limit 0,10')
result = cursor.fetchall()
if len(result):
check = True
assert check
for row in result:
assert (row[1] > 0)
assert (row[7] > 0)