Skip to content

Instantly share code, notes, and snippets.

@fmaclen
Created May 5, 2020 23:50
Show Gist options
  • Save fmaclen/15678a6026143e7f822748cce7a8f94b to your computer and use it in GitHub Desktop.
Save fmaclen/15678a6026143e7f822748cce7a8f94b to your computer and use it in GitHub Desktop.
Compares the time difference when running webpack between 2 sets of changes/branches
# frozen_string_literal: true
# Instructions
# 1- Copy this file to /tmp
# 2- Switch to the baseline branch
# 3- Run "ruby webpack_benchmark.rb"
# 4- Switch branches and run this script again
require 'benchmark'
aggregate_run_time = 0
number_of_runs = 10
number_of_runs.times do
# Benchmark the build script
results = Benchmark.measure do
`
cd ..
npx webpack --config webpack.production.js
`
end
# Get the results
aggregate_run_time += results.real.round(3)
end
# Calculate averages
current_average_time = (aggregate_run_time / number_of_runs).round(3)
puts "Average time after #{number_of_runs} runs: #{current_average_time}"
# Read baseline
if File.file?("webpack_benchmark_baseline.txt")
baseline_time = File.read("webpack_benchmark_baseline.txt").to_f
time_difference = (100 - ((current_average_time * 100) / baseline_time)).round(2)
puts "This run is #{time_difference}% #{ current_average_time > baseline_time ? "slower" : "faster" } than the baseline (#{baseline_time})"
# Create a baseline
else
File.open("webpack_benchmark_baseline.txt", 'w') { |file| file.write(current_average_time) }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment