Skip to content

Commit

Permalink
TEMP: add benchmatk to demo performance difference
Browse files Browse the repository at this point in the history
  • Loading branch information
ascrazy committed Jan 9, 2025
1 parent 7d15c49 commit 29e6af9
Show file tree
Hide file tree
Showing 4 changed files with 27,889 additions and 0 deletions.
70 changes: 70 additions & 0 deletions bench/bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/bin/bash

# Function to run a script multiple times and measure performance
run_benchmark() {
script_name=$1
runs=$2

# Check if script exists and is executable
if [ ! -x "$script_name" ]; then
echo "Error: $script_name not found or not executable"
exit 1
fi

total_time=0
total_cpu=0
max_memory=0

echo "Running $script_name $runs times..."

for ((i=1; i<=$runs; i++)); do
# Backup yarn.lock file before running the script
cp yarn.lock yarn.lock.backup

# Use time command to get execution statistics
# Redirect script output to /dev/null to keep benchmark output clean
stats=$( { /usr/bin/time -f "%e,%S,%U,%M" ./"$script_name" > /dev/null; } 2>&1 )

# cleanup
mv yarn.lock.backup yarn.lock
rm -f yarn.lock.toptal yarn.lock.temp || true

# Parse statistics
IFS=',' read -r real_time sys_time user_time memory <<< "$stats"

# Calculate CPU time (system + user time)
cpu_time=$(echo "$sys_time + $user_time" | bc)

# Update totals
total_time=$(echo "$total_time + $real_time" | bc)
total_cpu=$(echo "$total_cpu + $cpu_time" | bc)

# Update max memory if current usage is higher
if (( $(echo "$memory > $max_memory" | bc -l) )); then
max_memory=$memory
fi

echo -ne "\rProgress: $i/$runs"
done
echo # New line after progress

# Calculate averages
avg_time=$(echo "scale=3; $total_time / $runs" | bc)
avg_cpu=$(echo "scale=3; $total_cpu / $runs" | bc)

echo "Results for $script_name:"
echo " Average real time: ${avg_time}s"
echo " Average CPU time: ${avg_cpu}s"
echo " Peak memory: ${max_memory}KB"
echo "------------------------"
}

# Number of times to run each script
runs=5

echo "Performance Comparison"
echo "========================"

# Run benchmarks for both scripts
run_benchmark "new.sh" $runs
run_benchmark "old.sh" $runs
29 changes: 29 additions & 0 deletions bench/new.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#!/bin/bash

PUBLIC_YARN_REGISTRY="https://registry.yarnpkg.com/"
PUBLIC_NPM_REGISTRY="https://registry.npmjs.org/"
TOPTAL_NPM_REGISTRY="https://us-central1-npm.pkg.dev/toptal-ci/npm-registry/"

temp_yarn_lock="yarn.lock.temp"

echo "Updating registry URLs in yarn.lock file and writing output to $temp_yarn_lock"
# NOTE: setting IFS= (empty) for `read -r line` ensures that:
# 1. Leading and trailing whitespace in each line is preserved
# 2. The line is read exactly as-is, without any field splitting
# 3. Any whitespace characters within the line remain untouched
while IFS= read -r line; do
if [[ $line =~ /@toptal|/@topkit ]]; then
# For @toptal and @topkit packages, keep the original registry
echo "$line" >> "$temp_yarn_lock"
else
# For all other packages, replace with new registry
modified_line="${line//$PUBLIC_YARN_REGISTRY/$TOPTAL_NPM_REGISTRY}"
modified_line="${modified_line//$PUBLIC_NPM_REGISTRY/$TOPTAL_NPM_REGISTRY}"
echo "$modified_line" >> "$temp_yarn_lock"
fi
done < yarn.lock

echo "Replacing yarn.lock file with $temp_yarn_lock"
mv $temp_yarn_lock yarn.lock

echo "Registry URLs updated while preserving @toptal and @topkit package sources"
25 changes: 25 additions & 0 deletions bench/old.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/bin/bash

echo "Extracting @toptal and @topkit entries"
touch yarn.lock.toptal
if grep -q '/@toptal\|/@topkit' yarn.lock; then
grep '/@toptal\|/@topkit' yarn.lock | awk '{print $2 " " $2}' > yarn.lock.toptal
fi

echo "Changing the URLs to the new registry for @toptal and @topkit entries, creating a TO/FROM list to be used when reverting back the URLs to the original registry"
sed -i -e "s#https://registry.yarnpkg.com/#https://us-central1-npm.pkg.dev/toptal-ci/npm-registry/#" yarn.lock.toptal
sed -i -e "s#https://registry.npmjs.org/#https://us-central1-npm.pkg.dev/toptal-ci/npm-registry/#" yarn.lock.toptal

echo "Changing the URLs to AR registry for all occurrences"
sed -i -e "s#https://registry.yarnpkg.com/#https://us-central1-npm.pkg.dev/toptal-ci/npm-registry/#g" yarn.lock
sed -i -e "s#https://registry.npmjs.org/#https://us-central1-npm.pkg.dev/toptal-ci/npm-registry/#g" yarn.lock

echo "Removing double quotes from the URLs"
sed -i -e "s/\"//g" yarn.lock.toptal

echo "Reverting the @toptal and @topkit packages to the original registry, working on revert fewer ocurrences (specific list) is faster than loop all the file"
while read -r line; do
url1="$(awk '{ print $1 }' <<<"$line")"
url2="$(awk '{ print $2 }' <<<"$line")"
sed -i -e "s~${url1}~${url2}~" yarn.lock
done < yarn.lock.toptal
Loading

0 comments on commit 29e6af9

Please sign in to comment.