Skip to content

Instantly share code, notes, and snippets.

@lmb
Created November 10, 2023 09:59
Show Gist options
  • Save lmb/cdf857f41635a36782a1a4094f709990 to your computer and use it in GitHub Desktop.
Save lmb/cdf857f41635a36782a1a4094f709990 to your computer and use it in GitHub Desktop.
Benchmark Go code on a single core of an Intel CPU
#!/bin/bash
# Fiddle with frequency scaling settings to reduce variance when running Go benchmarks.
# Also pins the benchmark to a single CPU.
CPU=${CPU:-1}
original_governor=$(cat /sys/devices/system/cpu/cpu$CPU/cpufreq/scaling_governor)
original_turbo=$(cat /sys/devices/system/cpu/intel_pstate/no_turbo)
disable_turbo() {
echo 1 | sudo tee /sys/devices/system/cpu/intel_pstate/no_turbo > /dev/null
}
restore_turbo() {
echo $original_turbo | sudo tee /sys/devices/system/cpu/intel_pstate/no_turbo > /dev/null
}
set_performance_governor() {
echo performance | sudo tee /sys/devices/system/cpu/cpu$CPU/cpufreq/scaling_governor > /dev/null
}
restore_governor() {
echo $original_governor | sudo tee /sys/devices/system/cpu/cpu$CPU/cpufreq/scaling_governor > /dev/null
}
execute_on_cpu() {
taskset -c $CPU $@
return $?
}
cleanup() {
restore_turbo
restore_governor
}
trap cleanup EXIT
echo "core: $CPU"
disable_turbo
set_performance_governor
execute_on_cpu $@
exit_status=$?
exit $exit_status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment