import gzip import json import os import pytest import sys sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) from telemetry.TelemetryDataParse import * @pytest.fixture() def telemetry_v2_gz_path(): return os.path.join( os.path.dirname(os.path.abspath(__file__)), "test_data/telemetry_data_v2.gz", ) @pytest.fixture() def telemetry_v2_data(telemetry_v2_gz_path: str): with gzip.open(telemetry_v2_gz_path) as fp: raw_data = fp.readline() try: _ = util.json_to_pb(json.loads(raw_data)) print("RwaData matches the current schema ^_^") except Exception as e: print(e) print("RwaData doesn't match the current schema !") telemetry_data = DataParse(raw_data) assert telemetry_data != None return telemetry_data class TestTelemetryV2: def test_get_app(self, telemetry_v2_data): assert telemetry_v2_data.get_app() == "tidb" def test_get_ip(self, telemetry_v2_data): assert telemetry_v2_data.get_ip() == "117.50.84.111" def test_get_host(self, telemetry_v2_data): assert telemetry_v2_data.get_host() == "pingcap-com-beijing-1" def test_get_cluster_index(self, telemetry_v2_data): assert telemetry_v2_data.get_cluster_index() == {"1c168a":{"clusterPKType":"INT","isClustered":True},"1f10b3":{"clusterPKType":"NA","isClustered":False},"37a680":{"clusterPKType":"INT","isClustered":True}} def test_get_cluster_index_usage(self, telemetry_v2_data): assert telemetry_v2_data.get_cluster_index_usage() == [{'1c168a': True}, {'1f10b3': False}, {'37a680': True}] def test_get_cluster_index_pk(self, telemetry_v2_data): assert telemetry_v2_data.get_cluster_index_pk() == [{'1c168a': "INT"}, {'1f10b3': "NA"}, {'37a680': "INT"}] def test_get_tracking_id(self, telemetry_v2_data): assert ( telemetry_v2_data.get_tracking_id() == "edcd85e9-e05d-4dfe-b7b3-93e46b792ca7" ) def test_get_report_timestamp(self, telemetry_v2_data): assert telemetry_v2_data.get_report_timestamp() == 1624953292 def test_get_cluster_index_cnt(self, telemetry_v2_data): assert telemetry_v2_data.get_cluster_index_cnt() == 3 def test_get_cluster_index_true_cnt(self, telemetry_v2_data): assert telemetry_v2_data.get_cluster_index_true_cnt() == 2 def test_get_cluster_index_false_cnt(self, telemetry_v2_data): assert telemetry_v2_data.get_cluster_index_false_cnt() == 1 def test_get_async_commit_used(self, telemetry_v2_data): assert telemetry_v2_data.get_async_commit_used() == True def test_get_async_commit_cnt(self, telemetry_v2_data): assert telemetry_v2_data.get_async_commit_cnt() == 0 def test_get_onePC_used(self, telemetry_v2_data): assert telemetry_v2_data.get_onePC_used() == True def test_get_onePC_cnt(self, telemetry_v2_data): assert telemetry_v2_data.get_onePC_cnt() == 0 def test_get_twoPC_cnt(self, telemetry_v2_data): assert telemetry_v2_data.get_twoPC_cnt() == 0 def test_get_execute_count(self, telemetry_v2_data): assert telemetry_v2_data.get_execute_count_sum() == 1853 def test_get_cop_usage(self, telemetry_v2_data): assert telemetry_v2_data.get_copr_cache_usage() == {"gte0":4372,"gte1":1183,"gte10":1183,"gte100":215,"gte20":1148,"gte40":880,"gte80":335} def test_get_tiflash_usage_pushdown(self, telemetry_v2_data): assert telemetry_v2_data.get_tiflash_pushdown() == 33 def test_get_tiflash_usage_exchange(self, telemetry_v2_data): assert telemetry_v2_data.get_tiflash_exchange_pushdown() == 29 def test_get_sql_usage(self, telemetry_v2_data): assert telemetry_v2_data.get_sql_usage() == [{"total":1862, "type":{"AlterTable":0,"Begin":0,"Commit":1,"CreateDatabase":0,"CreateIndex":0,"CreateTable":0,"Delete":1,"DropDatabase":0,"Explain":20,"Insert":3,"Replace":1,"Rollback":0,"Select":980,"Set":310,"Show":419,"TruncateTable":0,"Update":16,"Use":107,"other":4} }] def test_get_slow_query_usage(self, telemetry_v2_data): assert telemetry_v2_data.get_slow_query_stats() == {"slowQueryBucket":{"+Inf":58,"0.001":0,"0.002":0,"0.004":0,"0.008":0,"0.016":0,"0.032":0,"0.064":0,"0.128":0,"0.256":0,"0.512":6,"1.024":8,"1048.576":58,"131.072":58,"134217.728":58,"16.384":49,"16777.216":58,"2.048":11,"2097.152":58,"262.144":58,"32.768":51,"33554.432":58,"4.096":23,"4194.304":58,"524.288":58,"65.536":58,"67108.864":58,"8.192":33,"8388.608":58}} def test_get_host_extra(self, telemetry_v2_data): assert isinstance(telemetry_v2_data.get_host_extra(), dict) def test_get_instances(self, telemetry_v2_data): assert isinstance(telemetry_v2_data.get_instances(), list) def test_get_hardware(self, telemetry_v2_data): assert isinstance(telemetry_v2_data.get_hardware(), list)