Skip to content

Instantly share code, notes, and snippets.

@ramirez7
Last active September 11, 2023 19:45
Show Gist options
  • Save ramirez7/f22f1af998aee9e31173d09965863824 to your computer and use it in GitHub Desktop.
Save ramirez7/f22f1af998aee9e31173d09965863824 to your computer and use it in GitHub Desktop.
time-log.sh
# Keep track of build times in a lightweight way
# The log file is based on branch name
function time-log-file() {
echo "$(time-log-dir)/time.$(git rev-parse --abbrev-ref HEAD | sed s#/#--#g).log"
}
function time-log-dir() {
echo "$(git rev-parse --show-toplevel)/.time-logs"
}
function time-log-init() {
mkdir -p $(time-log-dir)
touch $(time-log-file)
}
# Add a custom comment. Useful for add context re: what you were up to
function time-log-comment() {
time-log-init
echo "### $1" | ts >> $(time-log-file)
}
# Record timing for a command. We log all output of the command with
# timestamps for future analysis. This is also useful for capturing when a process
# OoMs.
function time-log-do() {
time-log-init
echo ">>> $@" | ts >> $(time-log-file)
# `time` times it. We have to do special redirect stuff to pipe the output.
# `ts` adds timestamps to each line. It's from `moreutils`.
# `tee -a` appends it to the log file while also writing to stdout.
# `cut -d' ' -f4-` removes the timestamps from each line in stdout.
{ time "$@"; } 2>&1 | ts | tee -a $(time-log-file) | cut -d' ' -f4-
}
function time-log-total() {
time-log-init
cat $(time-log-file) | cut -d' ' -f4 | awk '/^real/{split($2,a,"m|s$"); print a[1] * 60 + a[2]}' | awk '{s+=$1} END {print s}'
}
@ramirez7
Copy link
Author

ramirez7 commented Sep 7, 2023

@camkidman OH you know what I think you actually found a bug and it's not a MacOS thing.

Does your branch name have a slash in it? I need to sed those into some other character.

@ramirez7
Copy link
Author

ramirez7 commented Sep 7, 2023

Okay, it should handle slashes in branches & properly init now.

Also, time logs now go in the .time-logs directory at repo root.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment