react-docker-optimization/benchmark.nu
2025-01-12 21:03:07 +01:00

70 lines
1.9 KiB
Text

def main [benchmark_size:int=5] {
let date = date now | format date '%F_%H-%M-%S'
let res_dir = $'build_results_($date)'
mkdir $res_dir
let bench = [
['tag', 'image'];
['simple-dockerfile', 'react-simple'],
['multi-stage-dockerfile', 'react-multistage'],
['node-alpine-dockerfile', 'react-multistage-alpine'],
['nginx-dockerfile', 'react-nginx'],
['nginx-alpine-dockerfile', 'react-nginx-alpine'],
['bun-rspack-dockerfile', 'react-bun-rspack'],
['nginx-distroless-dockerfile', 'react-nginx-distroless']
]
let images = [
'node:18',
'node:18-alpine'
'nginx',
'nginx:stable-alpine',
'oven/bun',
'cgr.dev/chainguard/nginx'
]
echo 'Pulling all docker images in advance'
echo ''
for entry in $images {
docker pull $entry
echo ''
}
echo ''
for entry: record<tag: string, image:string> in $bench {
echo $'Switching to branch ($entry.tag)'
echo ''
git checkout $entry.tag
mut sum = 0ns
mut max = 0ns
mut min = 0ns
mut csv_results = []
for i in 1..$benchmark_size {
echo $'Build ($i) out of ($benchmark_size) for branch ($entry.tag)'
let time = $'(timeit {docker build --no-cache -t $entry.image .})' | into duration
if $i < $benchmark_size {
docker image rm $'($entry.image)'
docker builder prune -af
}
$sum += $time
if $max == 0ns or $time >= $max {
$max = $time
}
if $min == 0ns or $time <= $min {
$min = $time
}
$csv_results = ($csv_results ++ [['tag', 'image', 'time']; [$entry.tag, $entry.image, $time]])
}
docker image rm $'($entry.image)'
docker builder prune -af
echo $'Finieshed building ($benchmark_size) images for branch ($entry.tag)'
echo $'Results for branch ($entry.tag):'
echo $csv_results
$csv_results | to csv | save -f $'./($res_dir)/($entry.tag).csv'
echo ''
}
git checkout main
cp -rf $'./($res_dir)/' $'./build_results'
}