Skip to content

Instantly share code, notes, and snippets.

@Hendekagon
Last active March 16, 2023 08:45
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Hendekagon/75139bb285ccd8cc183fe69415ccc66f to your computer and use it in GitHub Desktop.
profile all functions in a namespace
{:paths ["."] :deps {robert/hooke {:mvn/version "1.3.0"}
com.taoensso/tufte {:mvn/version "2.4.5"}}}
(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))))))))
@Hendekagon
Copy link
Author

Hendekagon commented May 15, 2019

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

:profiling
    {
       :extra-deps
       {
          profile-ns {:git/url "https://gist.github.com/Hendekagon/75139bb285ccd8cc183fe69415ccc66f" 
                            :sha "8fa1a6a475332a574860f736e85155cea29129e4"}
       }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment