|
| 1 | +# BSD 3-Clause License |
| 2 | +# |
| 3 | +# Copyright (c) 2021., Redis Labs Modules |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +import logging |
| 7 | + |
| 8 | +import redis |
| 9 | +from pytablewriter import MarkdownTableWriter |
| 10 | + |
| 11 | +from redisbench_admin.utils.remote import get_overall_dashboard_keynames |
| 12 | +from redistimeseries.client import Client |
| 13 | + |
| 14 | +from redisbench_admin.utils.utils import get_ts_metric_name |
| 15 | + |
| 16 | + |
| 17 | +def compare_command_logic(args): |
| 18 | + logging.info("Checking connection to RedisTimeSeries.") |
| 19 | + rts = Client( |
| 20 | + host=args.redistimeseries_host, |
| 21 | + port=args.redistimeseries_port, |
| 22 | + password=args.redistimeseries_pass, |
| 23 | + ) |
| 24 | + rts.redis.ping() |
| 25 | + |
| 26 | + tf_github_org = args.github_org |
| 27 | + tf_github_repo = args.github_repo |
| 28 | + tf_triggering_env = args.triggering_env |
| 29 | + deployment_type = args.deployment_type |
| 30 | + from_ts_ms = args.from_timestamp |
| 31 | + to_ts_ms = args.to_timestamp |
| 32 | + baseline_branch = args.baseline_branch |
| 33 | + comparison_branch = args.comparison_branch |
| 34 | + metric_name = args.metric_name |
| 35 | + metric_mode = args.metric_mode |
| 36 | + ( |
| 37 | + prefix, |
| 38 | + testcases_setname, |
| 39 | + tsname_project_total_failures, |
| 40 | + tsname_project_total_success, |
| 41 | + ) = get_overall_dashboard_keynames(tf_github_org, tf_github_repo, tf_triggering_env) |
| 42 | + test_names = [] |
| 43 | + try: |
| 44 | + test_names = rts.redis.smembers(testcases_setname) |
| 45 | + test_names = list(test_names) |
| 46 | + test_names.sort() |
| 47 | + except redis.exceptions.ResponseError as e: |
| 48 | + logging.warning( |
| 49 | + "Error while trying to fetch test cases set (key={}) {}. ".format( |
| 50 | + testcases_setname, e.__str__() |
| 51 | + ) |
| 52 | + ) |
| 53 | + pass |
| 54 | + |
| 55 | + logging.warning( |
| 56 | + "Based on test-cases set (key={}) we have {} distinct benchmarks. ".format( |
| 57 | + testcases_setname, len(test_names) |
| 58 | + ) |
| 59 | + ) |
| 60 | + profilers_artifacts_matrix = [] |
| 61 | + for test_name in test_names: |
| 62 | + |
| 63 | + test_name = test_name.decode() |
| 64 | + |
| 65 | + ts_name_baseline = get_ts_metric_name( |
| 66 | + "by.branch", |
| 67 | + baseline_branch, |
| 68 | + tf_github_org, |
| 69 | + tf_github_repo, |
| 70 | + deployment_type, |
| 71 | + test_name, |
| 72 | + tf_triggering_env, |
| 73 | + metric_name, |
| 74 | + ) |
| 75 | + ts_name_comparison = get_ts_metric_name( |
| 76 | + "by.branch", |
| 77 | + comparison_branch, |
| 78 | + tf_github_org, |
| 79 | + tf_github_repo, |
| 80 | + deployment_type, |
| 81 | + test_name, |
| 82 | + tf_triggering_env, |
| 83 | + metric_name, |
| 84 | + ) |
| 85 | + baseline_v = "N/A" |
| 86 | + comparison_v = "N/A" |
| 87 | + try: |
| 88 | + |
| 89 | + baseline_datapoints = rts.revrange( |
| 90 | + ts_name_baseline, from_ts_ms, to_ts_ms, count=1 |
| 91 | + ) |
| 92 | + if len(baseline_datapoints) > 0: |
| 93 | + _, baseline_v = baseline_datapoints[0] |
| 94 | + comparison_datapoints = rts.revrange( |
| 95 | + ts_name_comparison, from_ts_ms, to_ts_ms, count=1 |
| 96 | + ) |
| 97 | + if len(comparison_datapoints) > 0: |
| 98 | + _, comparison_v = comparison_datapoints[0] |
| 99 | + except redis.exceptions.ResponseError: |
| 100 | + pass |
| 101 | + percentage_change = "N/A" |
| 102 | + if baseline_v != "N/A" and comparison_v != "N/A": |
| 103 | + if metric_mode == "higher-better": |
| 104 | + percentage_change = "{0:.2f} %".format( |
| 105 | + (float(comparison_v) / float(baseline_v) - 1) * 100.0 |
| 106 | + ) |
| 107 | + else: |
| 108 | + # lower-better |
| 109 | + percentage_change = "{0:.2f} %".format( |
| 110 | + (float(baseline_v) / float(comparison_v) - 1) * 100.0 |
| 111 | + ) |
| 112 | + if baseline_v != "N/A" or comparison_v != "N/A": |
| 113 | + profilers_artifacts_matrix.append( |
| 114 | + [ |
| 115 | + test_name, |
| 116 | + baseline_v, |
| 117 | + comparison_v, |
| 118 | + percentage_change, |
| 119 | + ] |
| 120 | + ) |
| 121 | + logging.info("Printing differential analysis between branches") |
| 122 | + |
| 123 | + writer = MarkdownTableWriter( |
| 124 | + table_name="Comparison between {} and {} for metric: {}".format( |
| 125 | + baseline_branch, comparison_branch, metric_name |
| 126 | + ), |
| 127 | + headers=[ |
| 128 | + "Test Case", |
| 129 | + "Baseline value", |
| 130 | + "Comparison Value", |
| 131 | + "% change ({})".format(metric_mode), |
| 132 | + ], |
| 133 | + value_matrix=profilers_artifacts_matrix, |
| 134 | + ) |
| 135 | + writer.write_table() |
0 commit comments