improve erc-sasl
[Emacs.git] / lisp / init-benchmarking.el
blobebc2f3e101fa982bda6819fc3e656d3bd4077814
1 ;;; init-benchmarking.el --- Measure startup and require times -*- lexical-binding: t -*-
2 ;;; Commentary:
3 ;;; Code:
5 (defun sanityinc/time-subtract-millis (b a)
6 (* 1000.0 (float-time (time-subtract b a))))
9 (defvar sanityinc/require-times nil
10 "A list of (FEATURE LOAD-START-TIME LOAD-DURATION).
11 LOAD-DURATION is the time taken in milliseconds to load FEATURE.")
13 (defun sanityinc/require-times-wrapper (orig feature &rest args)
14 "Note in `sanityinc/require-times' the time taken to require each feature."
15 (let* ((already-loaded (memq feature features))
16 (require-start-time (and (not already-loaded) (current-time))))
17 (prog1
18 (apply orig feature args)
19 (when (and (not already-loaded) (memq feature features))
20 (let ((time (sanityinc/time-subtract-millis (current-time) require-start-time)))
21 (add-to-list 'sanityinc/require-times
22 (list feature require-start-time time)
23 t))))))
25 (advice-add 'require :around 'sanityinc/require-times-wrapper)
28 (define-derived-mode sanityinc/require-times-mode tabulated-list-mode "Require-Times"
29 "Show times taken to `require' packages."
30 (setq tabulated-list-format
31 [("Start time (ms)" 20 sanityinc/require-times-sort-by-start-time-pred)
32 ("Feature" 30 t)
33 ("Time (ms)" 12 sanityinc/require-times-sort-by-load-time-pred)])
34 (setq tabulated-list-sort-key (cons "Start time (ms)" nil))
35 ;; (setq tabulated-list-padding 2)
36 (setq tabulated-list-entries #'sanityinc/require-times-tabulated-list-entries)
37 (tabulated-list-init-header)
38 (when (fboundp 'tablist-minor-mode)
39 (tablist-minor-mode)))
41 (defun sanityinc/require-times-sort-by-start-time-pred (entry1 entry2)
42 (< (string-to-number (elt (nth 1 entry1) 0))
43 (string-to-number (elt (nth 1 entry2) 0))))
45 (defun sanityinc/require-times-sort-by-load-time-pred (entry1 entry2)
46 (> (string-to-number (elt (nth 1 entry1) 2))
47 (string-to-number (elt (nth 1 entry2) 2))))
49 (defun sanityinc/require-times-tabulated-list-entries ()
50 (cl-loop for (feature start-time millis) in sanityinc/require-times
51 with order = 0
52 do (cl-incf order)
53 collect (list order
54 (vector
55 (format "%.3f" (sanityinc/time-subtract-millis start-time before-init-time))
56 (symbol-name feature)
57 (format "%.3f" millis)))))
59 (defun sanityinc/require-times ()
60 "Show a tabular view of how long various libraries took to load."
61 (interactive)
62 (with-current-buffer (get-buffer-create "*Require Times*")
63 (sanityinc/require-times-mode)
64 (tabulated-list-revert)
65 (display-buffer (current-buffer))))
69 (defun sanityinc/show-init-time ()
70 (message "init completed in %.2fms"
71 (sanityinc/time-subtract-millis after-init-time before-init-time)))
73 (add-hook 'after-init-hook 'sanityinc/show-init-time)
76 (provide 'init-benchmarking)
77 ;;; init-benchmarking.el ends here