Skip to content
Adam Spiers edited this page Jul 16, 2020 · 3 revisions

Description

The following hydra allows easy use of emacs' built-in profiler.el for profiling the performance of elisp. Note that it depends on the excellent magnars/s.el and magnars/s.el libraries, which you probably already have installed because so many other packages also depend on them.

Alternative version

Instead of the version below, you may prefer to use the version at https://github.com/aspiers/emacs/blob/master/.emacs.d/init.d/as-profiling.el for any of the following reasons:

  • It may be updated more regularly than this wiki page. (PRs are welcome!)
  • It is conveniently wrapped in a use-package sexp.
  • It adds a . binding to profiler-report-mode-map for conveniently stopping the profiler while viewing a report.

Hydra

  (defun profiler-running-modes ()
    (let ((running-modes
           (-non-nil (list (if (profiler-cpu-running-p) "cpu")
                           (if (profiler-memory-running-p) "mem")))))
      (if running-modes
          (s-join "+" running-modes)
        "stopped")))

  (defhydra hydra-profiler
    (:color red :hint nil)
    "
elisp profiling (currently %s(profiler-running-modes))

^^Start / stop                          Reporting
^-^----------------------------------   ^-^----------------------------
_s_: start (prompt for mode)            _r_: show report
_c_: start CPU profiling
_m_: start memory profiling             _f_: find profile
_b_: start both CPU+memory profiling    _4_: find profile other window
_._: stop profiling                     _5_: find profile other frame
_R_: reset profiler logs

_q_: quit
_C_: customize profiler options
"
    ("s" hydra-profiler/profiler-start)
    ("c" (hydra-profiler/profiler-start 'cpu))
    ("m" (hydra-profiler/profiler-start 'mem))
    ("b" (hydra-profiler/profiler-start 'cpu+mem))
    ("." hydra-profiler/profiler-stop)
    ("R" profiler-reset)
    ("q" nil)
    ("C" (customize-group "profiler"))
    ("r" profiler-report :color blue)
    ("f" profiler-find-profile)
    ("4" profiler-find-profile-other-window)
    ("5" profiler-find-profile-other-frame))
Clone this wiki locally