Skip to content

Instantly share code, notes, and snippets.

@ramirez7
Last active September 11, 2023 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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 6, 2023

time-log.sh

Installation:

git clone git@gist.github.com:f22f1af998aee9e31173d09965863824.git -o time-log.sh /some/path

In your rc file:

source /some/path/time-log.sh/time-log.sh

Update from pulling from this gist.

Usage:

# Add notes so you can easily jump to certain points in the log
$ time-log-comment "About to do a clean rebuild"

$ time-log-do make build

Logs end up in the .time-logs/ directory in your git repo's root.

@camkidman
Copy link

Small note, ts isn't on a mac by default but does come as part of moreutils -- brew install moreutils

@camkidman
Copy link

Also also as-is I needed:

mkdir time.cam && touch `time-log-file`

@ramirez7
Copy link
Author

ramirez7 commented Sep 6, 2023

@camkidman I added a touch to the functions now.

And I forgot that ts doesn't come standard. Thanks for mentioning that.

@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