forked from KCEE0901/trustchain-escrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgas-profile.sh
More file actions
executable file
·121 lines (95 loc) · 4.23 KB
/
Copy pathgas-profile.sh
File metadata and controls
executable file
·121 lines (95 loc) · 4.23 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#!/usr/bin/env bash
# gas-profile.sh
#
# Runs gas profiling tests for all contracts and writes gas-report.json.
#
# Usage:
# bash scripts/gas-profile.sh # run + write report
# bash scripts/gas-profile.sh --compare # compare against previous report
#
# Output:
# gas-report.json — latest results (committed or gitignored as preferred)
set -euo pipefail
REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
REPORT="$REPO_ROOT/gas-report.json"
PREV_REPORT="$REPO_ROOT/gas-report.prev.json"
COMPARE=false
for arg in "$@"; do
[[ "$arg" == "--compare" ]] && COMPARE=true
done
# ── collect raw GAS_PROFILE lines from both contracts ────────────────────────
collect() {
local dir="$1"
(cd "$dir" && cargo test gas_profiling -- --nocapture 2>/dev/null) \
| grep "^GAS_PROFILE" || true
}
echo "⛽ Profiling escrow_contract…"
ESCROW_LINES=$(collect "$REPO_ROOT/contracts/escrow_contract")
echo "⛽ Profiling insurance_contract…"
INSURANCE_LINES=$(collect "$REPO_ROOT/contracts/insurance_contract")
ALL_LINES=$(printf '%s\n%s\n' "$ESCROW_LINES" "$INSURANCE_LINES")
# ── build JSON ────────────────────────────────────────────────────────────────
TIMESTAMP=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
{
echo "{"
echo " \"generated_at\": \"$TIMESTAMP\","
echo " \"results\": ["
first=true
while IFS= read -r line; do
[[ -z "$line" ]] && continue
# GAS_PROFILE | <contract> | <function> | cpu=<n> | mem=<n>
contract=$(echo "$line" | awk -F' \\| ' '{print $2}' | xargs)
function=$(echo "$line" | awk -F' \\| ' '{print $3}' | xargs)
cpu=$(echo "$line" | awk -F' \\| ' '{print $4}' | sed 's/cpu=//')
mem=$(echo "$line" | awk -F' \\| ' '{print $5}' | sed 's/mem=//')
[[ "$first" == "false" ]] && echo " ,"
echo " {\"contract\": \"$contract\", \"function\": \"$function\", \"cpu_instructions\": $cpu, \"memory_bytes\": $mem}"
first=false
done <<< "$ALL_LINES"
echo " ]"
echo "}"
} > "$REPORT"
echo ""
echo "✅ Report written to gas-report.json"
echo ""
# ── pretty-print table ────────────────────────────────────────────────────────
printf "%-30s %-30s %18s %18s\n" "CONTRACT" "FUNCTION" "CPU INSTRUCTIONS" "MEMORY BYTES"
printf '%s\n' "$(printf '%.0s-' {1..100})"
while IFS= read -r line; do
[[ -z "$line" ]] && continue
contract=$(echo "$line" | awk -F' \\| ' '{print $2}' | xargs)
function=$(echo "$line" | awk -F' \\| ' '{print $3}' | xargs)
cpu=$(echo "$line" | awk -F' \\| ' '{print $4}' | sed 's/cpu=//')
mem=$(echo "$line" | awk -F' \\| ' '{print $5}' | sed 's/mem=//')
printf "%-30s %-30s %18s %18s\n" "$contract" "$function" "$cpu" "$mem"
done <<< "$ALL_LINES"
echo ""
# ── optional comparison ───────────────────────────────────────────────────────
if [[ "$COMPARE" == "true" && -f "$PREV_REPORT" ]]; then
echo "📊 Comparing against previous report…"
echo ""
printf "%-30s %-30s %12s %12s %10s\n" "CONTRACT" "FUNCTION" "CPU (prev)" "CPU (now)" "DELTA %"
printf '%s\n' "$(printf '%.0s-' {1..100})"
while IFS= read -r line; do
[[ -z "$line" ]] && continue
contract=$(echo "$line" | awk -F' \\| ' '{print $2}' | xargs)
function=$(echo "$line" | awk -F' \\| ' '{print $3}' | xargs)
cpu_now=$(echo "$line" | awk -F' \\| ' '{print $4}' | sed 's/cpu=//')
cpu_prev=$(python3 -c "
import json, sys
data = json.load(open('$PREV_REPORT'))
for r in data['results']:
if r['contract'] == '$contract' and r['function'] == '$function':
print(r['cpu_instructions'])
sys.exit(0)
print('N/A')
" 2>/dev/null || echo "N/A")
if [[ "$cpu_prev" != "N/A" ]]; then
delta=$(python3 -c "print(f'{(($cpu_now - $cpu_prev) / $cpu_prev * 100):+.1f}%')" 2>/dev/null || echo "?")
else
delta="new"
fi
printf "%-30s %-30s %12s %12s %10s\n" "$contract" "$function" "$cpu_prev" "$cpu_now" "$delta"
done <<< "$ALL_LINES"
echo ""
fi