-
Notifications
You must be signed in to change notification settings - Fork 445
158 lines (130 loc) · 6.01 KB
/
perf-test.yml
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
name: Performance Test
on:
push:
branches:
- master
pull_request:
branches:
- "**"
jobs:
performance_test:
name: Run performance tests
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: ipfs/aegir/actions/cache-node-modules@master
with:
directories: ./packages/perf/node_modules
- name: Install playwright dependencies
uses: ipfs/aegir/actions/cache-node-modules@master
- run: npx playwright install-deps
- name: Build perf
working-directory: packages/perf
run: (npm i && npm run build)
- name: Run tests and store in test_output
working-directory: packages/perf
run: |
npm run test > test_output.txt
- name: Extract bandwidth from test output
id: extract_bandwidths
working-directory: packages/perf
shell: bash
run: |
bandwidth_file="test_output.txt"
bandwidth_object="{"
# Function to assign bandwidth values to the object
assign_bandwidth() {
local environment=$1
local current_upload_bandwidth=$2
local current_download_bandwidth=$3
if [[ ${#bandwidth_object} -gt 1 ]]; then
bandwidth_object+=", "
fi
bandwidth_object+="\"$environment\": { \"currentUploadBandwidth\": $current_upload_bandwidth, \"currentDownloadBandwidth\": $current_download_bandwidth }"
}
# Read the bandwidth values from the file
upload_count=0
download_count=0
upload_bandwidths=()
download_bandwidths=()
while IFS= read -r line; do
if [[ $line == *"Upload bandwidth"* ]]; then
current_upload_bandwidth=$(grep -o 'Upload bandwidth: [0-9]*' <<< "$line" | grep -o '[0-9]*')
upload_count=$((upload_count + 1))
elif [[ $line == *"Download bandwidth"* ]]; then
current_download_bandwidth=$(grep -o 'Download bandwidth: [0-9]*' <<< "$line" | grep -o '[0-9]*')
download_count=$((download_count + 1))
fi
if [[ $upload_count -eq 1 && $download_count -eq 1 ]]; then
upload_bandwidths[0]=$current_upload_bandwidth
download_bandwidths[0]=$current_download_bandwidth
elif [[ $upload_count -eq 2 && $download_count -eq 2 ]]; then
upload_bandwidths[1]=$current_upload_bandwidth
download_bandwidths[1]=$current_download_bandwidth
elif [[ $upload_count -eq 3 && $download_count -eq 3 ]]; then
upload_bandwidths[2]=$current_upload_bandwidth
download_bandwidths[2]=$current_download_bandwidth
break # Exit the loop after processing the third instance
fi
done < "$bandwidth_file"
# Construct the bandwidth object
assign_bandwidth "node" "${upload_bandwidths[0]}" "${download_bandwidths[0]}"
assign_bandwidth "browser" "${upload_bandwidths[1]}" "${download_bandwidths[1]}"
assign_bandwidth "webworker" "${upload_bandwidths[2]}" "${download_bandwidths[2]}"
# Remove trailing comma if there are values in the object
if [[ ${#bandwidth_object} -gt 1 ]]; then
bandwidth_object="${bandwidth_object#, }"
fi
bandwidth_object+="}"
current_bandwidths=$bandwidth_object
echo "::set-output name=current_bandwidths::$current_bandwidths"
# Read the previous bandwidth values from the file
previous_bandwidths=$(cat previousPerf.json | jq -r '.')
# Iterate over the current bandwidth object and merge with previous values
merged_object=$(jq --argjson previous "$previous_bandwidths" '. as $current | reduce keys[] as $key ({}; .[$key] = ($current[$key] + $previous[$key]))' <<< "$current_bandwidths")
# Set the merged bandwidth object as environment variable
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "BANDWIDTH_TEST_RESULTS<<$EOF" >> "$GITHUB_ENV"
echo $merged_object >> "$GITHUB_ENV"
echo "$EOF" >> "$GITHUB_ENV"
- name: Generate Test Report
id: generate_report
working-directory: packages/perf
run: npm run renderResults > test_report.md
- name: Upload Test Report
id: upload_report
uses: actions/upload-artifact@v3
with:
name: test_report
path: packages/perf/test_report.md
- name: Show Report Output
working-directory: packages/perf
run: cat test_report.md >> $GITHUB_STEP_SUMMARY
- name: Save current performance data
id: save_performance_data
working-directory: packages/perf
shell: bash
run: |
current_bandwidths=$(echo '${{ steps.extract_bandwidths.outputs.current_bandwidths }}' | jq -r '.')
# Parse the input object using jq, modify the keys, and store the result in a new variable
updated_object=$(echo "$current_bandwidths" | jq 'map_values(.previousUploadBandwidth = .currentUploadBandwidth | del(.currentUploadBandwidth) | .previousDownloadBandwidth = .currentDownloadBandwidth | del(.currentDownloadBandwidth))')
# Output the updated object
echo $updated_object > previousPerf.json
- name: Commit and push changes
uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Update performance data
file_pattern: "*.json"
branch: ${{ github.ref }}
- name: Check CI job status
id: check_ci
working-directory: packages/perf
run: |
# Check if the file contains the "❌" symbol
if grep -q "❌" test_report.md; then
echo "Failure: There was a performance degradation of over 5%."
exit 1 # Exit with a non-zero status code for failure
else
echo "Success: Performance benchmarks passed."
exit 0 # Exit with a zero status code for pass
fi