Last active
March 16, 2023 08:45
-
-
Save Hendekagon/75139bb285ccd8cc183fe69415ccc66f to your computer and use it in GitHub Desktop.
profile all functions in a namespace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{:paths ["."] :deps {robert/hooke {:mvn/version "1.3.0"} | |
com.taoensso/tufte {:mvn/version "2.4.5"}}} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns profile-ns | |
(:require [taoensso.tufte :as tufte :refer [p]] | |
[robert.hooke :as hooke])) | |
(defn profile-ns | |
([namespace-name] | |
(profile-ns {:enable true} namespace-name)) | |
([{profile? :enable} namespace-name] | |
(doseq [[_ v] | |
(filter | |
(fn [[_ x]] | |
(if (and (var? x) (fn? @x)) (-> x meta :ns ns-name #{namespace-name}) false)) | |
(ns-map namespace-name))] | |
(hooke/remove-hook v :p) | |
(when profile? | |
(hooke/add-hook v :p | |
(fn [f & args] | |
(p (keyword (name namespace-name) (name (:name (meta v)))) | |
(apply f args)))))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You want to profile all functions in a namespace, this uses Tufte and Robert Hooke to make that so:
(use 'profile-ns)
(require '[taoensso.tufte :as tufte])
(profile-ns 'my-name-space)
(tufte/format-pstats @(last (tufte/profiled {} (my-function))))
profile-ns
all the namespaces to the level of detail you want to be included in the results(profile-ns {:enable false} 'my-name-space)
to disable profiling from a namespace