-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathplot_contract_cost.py
More file actions
103 lines (89 loc) · 2.88 KB
/
plot_contract_cost.py
File metadata and controls
103 lines (89 loc) · 2.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Copyright (c) Walrus Foundation
# SPDX-License-Identifier: Apache-2.0
"""
This script provides simple plots for the output of the `gas_cost_bench` benchmarks of the
`walrus-sui` crate.
"""
import argparse
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import os
def plot_metric_per_blob(df, metric, title, output_dir, label):
plt.figure(figsize=(10, 6))
sns.lineplot(
data=df,
x="n_blobs",
y=metric,
hue="epochs",
style="epochs",
markers=True,
dashes=False,
)
plt.title(title)
plt.xlabel("Number of Blobs")
plt.ylabel(metric.replace("_", " ").title())
plt.legend(title="Epochs")
plt.tight_layout()
filename = f"{label}_{metric}.png".replace(" ", "_")
plt.savefig(os.path.join(output_dir, filename))
plt.close()
def main(csv_path, output_dir, gas_price):
# Load and clean the CSV
df = pd.read_csv(csv_path)
df.columns = df.columns.str.strip()
# Adjust cost based on the provided gas_price
if gas_price is not None:
df["computation_cost"] = df["computation_cost"] * gas_price / df["gas_price"]
df["gas_used"] = df["computation_cost"] + df["storage_cost"]
df["net_gas_used"] = df["gas_used"] - df["storage_rebate"]
# Normalize values
df["computation_cost_per_blob"] = df["computation_cost"] / df["n_blobs"]
df["net_gas_used_per_blob"] = df["net_gas_used"] / df["n_blobs"]
# Treat epochs as categorical
df["epochs"] = df["epochs"].astype(str)
# Ensure output directory exists
os.makedirs(output_dir, exist_ok=True)
# Generate plots per label
for label in df["label"].unique():
sub_df = df[df["label"] == label]
plot_metric_per_blob(
sub_df,
"computation_cost_per_blob",
f"{label} - Computation Cost per Blob",
output_dir,
label,
)
plot_metric_per_blob(
sub_df,
"net_gas_used_per_blob",
f"{label} - Net Gas Used per Blob",
output_dir,
label,
)
plot_metric_per_blob(
sub_df,
"computation_cost",
f"{label} - Total Computation Cost",
output_dir,
label,
)
print(f"Plots saved in: {output_dir}/")
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Plot normalized contract costs from a CSV file."
)
parser.add_argument("csv_path", help="Path to the CSV file.")
parser.add_argument(
"-o",
"--output-dir",
default="plots",
help="Directory to save plots (default: ./plots)",
)
parser.add_argument(
"--gas-price",
type=int,
help="Set the gas price to use for computation to reflect mainnet cost",
)
args = parser.parse_args()
main(args.csv_path, args.output_dir, args.gas_price)