github.com/aakash4dev/cometbft@v0.38.2/scripts/qa/reporting/latency_plotter.py (about)

     1  import sys
     2  import os
     3  from datetime import datetime
     4  import pytz
     5  
     6  import matplotlib as mpl
     7  import matplotlib.pyplot as plt
     8  
     9  import numpy as np
    10  import pandas as pd
    11  
    12  release = 'v0.38.0-alpha2'
    13  
    14  #FIXME: figure out in which timezone prometheus was running to adjust to UTC.
    15  tz = pytz.timezone('America/Sao_Paulo')
    16  
    17  if len(sys.argv) != 2:
    18      print('Pls provide the raw.csv file')
    19      exit()
    20  else:
    21      csvpath = sys.argv[1]
    22      if not os.path.exists(csvpath):
    23         print('Pls provide a valid the raw.csv file')
    24         exit()
    25          
    26      print(csvpath)
    27  
    28  path = os.path.join('imgs')
    29  
    30  #Load the CSV
    31  csv = pd.read_csv(csvpath)
    32  
    33  #Transform ns to s in the latency/duration
    34  csv['duration_ns'] = csv['duration_ns'].apply(lambda x: x/10**9)
    35  csv['block_time'] = csv['block_time'].apply(lambda x: x/10**9)
    36  
    37  #Group by experiment
    38  groups = csv.groupby(['experiment_id'])
    39  
    40  #number of rows and columns in the graph
    41  ncols = 2 if groups.ngroups > 1 else 1
    42  nrows = int( np.ceil(groups.ngroups / ncols)) if groups.ngroups > 1 else 1
    43  fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(6*ncols, 4*nrows), sharey=False)
    44  fig.tight_layout(pad=5.0)
    45  
    46  
    47  #Plot experiments as subplots 
    48  for (key,ax) in zip(groups.groups.keys(), [axes] if ncols == 1 else axes.flatten()):
    49      group = groups.get_group(key)
    50      ax.set_ylabel('latency (s)')
    51      ax.set_xlabel('experiment time (s)')
    52      ax.set_title(key)
    53      ax.grid(True)
    54  
    55      #Group by connection number and transaction rate
    56      paramGroups = group.groupby(['connections','rate'])
    57      for (subKey) in paramGroups.groups.keys():
    58          subGroup = paramGroups.get_group(subKey)
    59          startTime = subGroup.block_time.min()
    60          endTime = subGroup.block_time.max()
    61          localStartTime = tz.localize(datetime.fromtimestamp(startTime)).astimezone(pytz.utc)
    62          localEndTime  = tz.localize(datetime.fromtimestamp(endTime)).astimezone(pytz.utc)
    63          subGroup.block_time.apply(lambda x: x - startTime )
    64          mean = subGroup.duration_ns.mean()
    65          print('exp', key ,'start', localEndTime.strftime("%Y-%m-%dT%H:%M:%SZ"), 'end', localStartTime.strftime("%Y-%m-%dT%H:%M:%SZ"), 'duration', endTime - startTime, "mean", mean)
    66  
    67          (con,rate) = subKey
    68          label = 'c='+str(con) + ' r='+ str(rate)
    69          ax.axhline(y = mean, color = 'r', linestyle = '-', label="mean")
    70          ax.scatter(subGroup.block_time, subGroup.duration_ns, label=label)
    71      ax.legend()
    72  
    73      #Save individual axes
    74      extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
    75      fig.savefig(os.path.join(path,'e_'+key + '.png'), bbox_inches=extent.expanded(1.2, 1.3))
    76  
    77  fig.suptitle('Vote Extensions Testnet - ' + release)
    78  
    79  # Save the figure with subplots
    80  fig.savefig(os.path.join(path,'all_experiments.png'))
    81  
    82  
    83  
    84  #Group by configuration
    85  groups = csv.groupby(['connections','rate'])
    86  
    87  #number of rows and columns in the graph
    88  ncols = 2 if groups.ngroups > 1 else 1
    89  nrows = int( np.ceil(groups.ngroups / ncols)) if groups.ngroups > 1 else 1
    90  fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(6*ncols, 4*nrows), sharey=True)
    91  fig.tight_layout(pad=5.0)
    92  
    93  #Plot configurations as subplots 
    94  for (key,ax) in zip(groups.groups.keys(), [axes] if ncols == 1 else axes.flatten()):
    95      group = groups.get_group(key)
    96      ax.set_ylabel('latency (s)')
    97      ax.set_xlabel('experiment time (s)')
    98      ax.grid(True)
    99      (con,rate) = key
   100      label = 'c='+str(con) + ' r='+ str(rate)
   101      ax.set_title(label)
   102  
   103      #Group by experiment 
   104      paramGroups = group.groupby(['experiment_id'])
   105      for (subKey) in paramGroups.groups.keys():
   106          subGroup = paramGroups.get_group(subKey)
   107          startTime = subGroup.block_time.min()
   108          subGroupMod = subGroup.block_time.apply(lambda x: x - startTime)
   109          ax.scatter(subGroupMod, subGroup.duration_ns, label=label)
   110      #ax.legend()
   111      
   112  
   113      #Save individual axes
   114      extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
   115      fig.savefig(os.path.join(path,'c'+str(con) + 'r'+ str(rate) + '.png'), bbox_inches=extent.expanded(1.2, 1.3))
   116  
   117  fig.suptitle('Vote Extensions Testnet - ' + release)
   118  
   119  
   120  # Save the figure with subplots
   121  fig.savefig(os.path.join(path,'all_configs.png'))
   122  
   123  
   124  fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(6*ncols, 4*nrows), sharey=True)
   125  fig.tight_layout(pad=5.0)
   126  
   127  #Plot configurations as subplots 
   128  for (key,ax) in zip(groups.groups.keys(), [axes] if ncols == 1 else axes.flatten()):
   129      group = groups.get_group(key)
   130      ax.set_ylabel('latency (s)')
   131      ax.set_xlabel('experiment time (s)')
   132      ax.grid(True)
   133      (con,rate) = key
   134      label = 'c='+str(con) + ' r='+ str(rate)
   135      ax.set_title(label)
   136  
   137      #Group by experiment, but merge them as a single experiment
   138      paramGroups = group.groupby(['experiment_id'])
   139      for (subKey) in paramGroups.groups.keys():
   140          subGroup = paramGroups.get_group(subKey)
   141          startTime = subGroup.block_time.min()
   142          subGroupMod = subGroup.block_time.apply(lambda x: x - startTime)
   143          ax.scatter(subGroupMod, subGroup.duration_ns, marker='o',c='#1f77b4')
   144      
   145      #Save individual axes
   146      extent = ax.get_window_extent().transformed(fig.dpi_scale_trans.inverted())
   147      (con,rate) = key
   148      fig.savefig(os.path.join(path,'c'+str(con) + 'r'+ str(rate) + '_merged.png'), bbox_inches=extent)
   149  
   150  plt.show()