Skip to content

feat(@libp2p/protocol-perf): Implement perf protocol #15

feat(@libp2p/protocol-perf): Implement perf protocol

feat(@libp2p/protocol-perf): Implement perf protocol #15

Workflow file for this run

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