Skip to content

Instantly share code, notes, and snippets.

@evan-forbes
Created July 25, 2023 10:41
Show Gist options
  • Save evan-forbes/948c8cf574f2f50b101c89a95ee1d43c to your computer and use it in GitHub Desktop.
Save evan-forbes/948c8cf574f2f50b101c89a95ee1d43c to your computer and use it in GitHub Desktop.
Script to visualize gas consumption traces
import sys
import json
import matplotlib.pyplot as plt
from matplotlib.backends.backend_pdf import PdfPages
def plot_gas_consumption(trace, color_map):
gas_readings = trace['readings']
descriptions = [reading['description'] for reading in gas_readings]
gas_values = [reading['amount'] for reading in gas_readings]
cumulative_gas = [sum(gas_values[:i+1]) for i in range(len(gas_values))]
plt.figure(figsize=(18, 6)) # Set the figure size to accommodate both charts
plt.subplot(1, 2, 1) # First subplot for the line chart
for description in color_map:
mask = [desc == description for desc in descriptions]
plt.plot([cumulative_gas[j] for j in range(len(mask)) if mask[j]],
[gas_values[j] for j in range(len(mask)) if mask[j]],
marker='o', linestyle='', label=description, color=color_map[description])
title = "Gas Consumption Trace:" + trace["msg_names"][0]
plt.xlabel("Cumulative Gas Consumption")
plt.ylabel('Gas Amount')
plt.title(title)
plt.grid(True)
# Move the legend outside the chart
plt.legend(loc='upper left', bbox_to_anchor=(1.05, 1))
# Adjust the layout to prevent overlapping of elements
plt.tight_layout()
# Calculate the percentage of gas used by each description for the pie chart
gas_total = sum(gas_values)
gas_totals = {}
for val in gas_readings:
description = val['description']
try:
gas_totals[description] += val['amount']
except KeyError:
gas_totals[description] = val['amount']
plt.subplot(1, 2, 2) # Second subplot for the pie chart
values = [gas_totals[desc] for desc in gas_totals]
keys = [desc for desc in gas_totals]
plt.pie(values, labels=keys, colors=[color_map[desc] for desc in gas_totals],
autopct='%1.1f%%', startangle=90)
plt.title('Gas Usage by Description')
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python script.py <json_file_path>")
sys.exit(1)
json_file = sys.argv[1]
with open(json_file, 'r') as file:
json_data = json.load(file)
# Create a color mapping dictionary
unique_descriptions = list(set(reading['description'] for trace in json_data for reading in trace['readings']))
num_unique_descriptions = len(unique_descriptions)
color_map = {description: plt.get_cmap('tab20')(i / num_unique_descriptions) for i, description in enumerate(unique_descriptions)}
# Create a PDF file to save the plots
with PdfPages('gas_consumption_plots.pdf') as pdf:
for trace in json_data:
if trace['readings']:
plot_gas_consumption(trace, color_map)
pdf.savefig() # Save the current plot to the PDF file
print("Plots have been saved to 'gas_consumption_plots.pdf'.")
@evan-forbes
Copy link
Author

I will not be maintaining this lol

it is purely a quick and dirty chatgpt barfed script to plot a gas consumption script per https://github.com/celestiaorg/celestia-app/blob/2a4ef9c9f3c276dfc514466bce9be24434d53189/specs/src/specs/resource_pricing.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment