forked from FuelLabs/sway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
benchmark.sh
executable file
·115 lines (97 loc) · 4.07 KB
/
benchmark.sh
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
#!/usr/bin/env bash
SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
PREPARE_FOR_COMMIT=false
while [[ $# -gt 0 ]]
do
key="$1"
case $key in
--prepare-for-commit)
PREPARE_FOR_COMMIT=true
shift # past argument
;;
*)
shift # past argument
;;
esac
done
benchmarks_dir="benchmarks"
if [ ! -d "$benchmarks_dir" ]; then
mkdir -p "$benchmarks_dir"
fi
if [ -d "$SCRIPT_DIR/target/release" ]; then
build_type="release"
elif [ -d "$SCRIPT_DIR/target/debug" ]; then
build_type="debug"
else
echo "Neither target/release nor target/debug directories found. Exiting..."
exit 1
fi
forc_path="$SCRIPT_DIR/target/$build_type/forc"
# prepare the benchmark data for commit if requested
if $PREPARE_FOR_COMMIT; then
sway_peformance_data_dir=performance-data
[email protected]:FuelLabs/sway-performance-data.git
if [ ! -d "$SCRIPT_DIR/$sway_peformance_data_dir" ]; then
echo "Directory $sway_peformance_data_dir not found. Cloning the repository..."
git clone "$sway_performance_data_repo_url" "$sway_peformance_data_dir"
echo "Repository cloned into $sway_peformance_data_dir."
else
echo "Updating sway-performance-data repository..."
git -C "$SCRIPT_DIR/$sway_peformance_data_dir" pull
fi
mkdir -p "$SCRIPT_DIR/$sway_peformance_data_dir/$GITHUB_SHA"
cp -r $benchmarks_dir/* "$SCRIPT_DIR/$sway_peformance_data_dir/$GITHUB_SHA"
else
sway_libs_dir=sway-libs
sway_libs_repo_url=https://github.com/FuelLabs/sway-libs.git
sway_libs_branch_name="benchmarks"
if [ ! -d "$SCRIPT_DIR/$sway_libs_dir" ]; then
echo "Directory $sway_libs_dir not found. Cloning the repository..."
git clone -b "$sway_libs_branch_name" "$sway_libs_repo_url" "$sway_libs_dir"
echo "Repository cloned with branch $sway_libs_branch_name into $sway_libs_dir."
fi
libs=(
"fixed_point"
"merkle_proof"
"nft"
"ownership"
"reentrancy"
"signed_integers"
"storagemapvec"
"strings/storage_string"
"strings/string"
)
sway_apps_dir=sway-applications
sway_apps_repo_url=https://github.com/FuelLabs/sway-applications.git
sway_apps_branch_name="master"
if [ ! -d "$SCRIPT_DIR/$sway_apps_dir" ]; then
echo "Directory $sway_apps_dir not found. Cloning the repository..."
git clone -b "$sway_apps_branch_name" "$sway_apps_repo_url" "$sway_apps_dir"
echo "Repository cloned with branch $sway_apps_branch_name into $sway_apps_dir."
fi
sway_libs_revision=$(git -C $SCRIPT_DIR/$sway_libs_dir rev-parse HEAD)
sway_apps_revision=$(git -C $SCRIPT_DIR/$sway_apps_dir rev-parse HEAD)
sway_git_revision=$(git rev-parse HEAD)
for lib in "${libs[@]}"; do
echo "Benchmarking $lib..."
project_name=$(basename "$lib")
metrics_json_file="$benchmarks_dir/$project_name.json"
output=$(/usr/bin/time -f '{"elapsed": "%e", "cpu_usage": "%P", "memory": "%MKB"}' \
$forc_path build --path "$SCRIPT_DIR/sway-libs/libs/$lib" \
--metrics-outfile="$metrics_json_file" 2>&1)
exit_status=$?
if [ $exit_status -ne 0 ]; then
echo " Failed, ignoring."
continue
fi
# filter out forc warnings by only matching on the JSON metrics data
json_stats=$(echo "$output" | grep -Eo '^\s*{[^}]*}')
metrics_json=$(cat "$metrics_json_file" | jq '{phases: .}')
merged_json=$(jq -s '.[0] * .[1]' <(echo "$metrics_json") <(echo "$json_stats"))
merged_json=$(jq --arg bt "$build_type" '. + {build_type: $bt}' <<< "$merged_json")
merged_json=$(jq --arg gr "$sway_apps_revision" '. + {sway_apps_revision: $gr}' <<< "$merged_json")
merged_json=$(jq --arg gr "$sway_libs_revision" '. + {sway_libs_revision: $gr}' <<< "$merged_json")
merged_json=$(jq --arg gr "$sway_git_revision" '. + {sway_git_revision: $gr}' <<< "$merged_json")
echo "$merged_json" > $metrics_json_file
done
fi