Skip to content

Instantly share code, notes, and snippets.

@death
Created December 9, 2019 14:02
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 death/4471dee58e52258fea2eb614fc52b59c to your computer and use it in GitHub Desktop.
Save death/4471dee58e52258fea2eb614fc52b59c to your computer and use it in GitHub Desktop.
asdf system durations
(defpackage #:snippets/system-durations
(:documentation "Measure the duration it takes to load each system.
Note that this script is not referenced in snippets.asd because it is
intended to be loaded prior to it.")
(:use #:cl)
(:import-from #:asdf)
(:import-from #:monotonic-clock
#:monotonic-now/ms)
(:import-from #:alexandria
#:hash-table-alist)
(:export
#:list-system-durations))
(in-package #:snippets/system-durations)
(defvar *system-durations*
(make-hash-table :test 'equal)
"Mapping from system names to load durations in milliseconds.")
(defmethod asdf:perform :around ((o asdf:load-op) (c asdf:component))
"The hook to do the actual measurement. It determines the duration
it took to load the supplied component and adds it to the proper
system entry in the system durations map."
(let ((start-time (monotonic-now/ms)))
(multiple-value-prog1 (call-next-method)
(let ((duration (- (monotonic-now/ms) start-time)))
(when (plusp duration)
(let ((system (parent-system c)))
(when system
(incf (gethash (asdf:component-name system)
*system-durations*
0)
duration))))))))
(defun parent-system (component)
"Return the parent system of a component, unless the component
itself is a system, in which case it is returned as-is."
(if (typep component 'asdf:system)
component
(if (typep component 'asdf:child-component)
(parent-system (asdf:component-parent component))
nil)))
(defun list-system-durations ()
"Return a list of system load durations. Each element in the list
is a cons whose car is the system's name and cdr the duration it took
to load in milliseconds."
(sort (hash-table-alist *system-durations*) #'< :key #'cdr))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment