I want to do some timings of how quickly clojure files load.Is there a way to hook into the loader to get this information?
I would like to obtain information like this:
ie. A depends on B depends on CA -> starting to load at T(a0)B -> starting to load at T(b0)C -> starting to load at T(c0)C -> loaded at T(c1), time taken 50msB -> loaded at T(b1), time taken 100msA -> loaded at T(a1), time taken 200ms- Mount works out dependencies by namespace loading, so you could looks at its source code.Chris Murphy– Chris Murphy2018-09-01 14:22:37 +00:00CommentedSep 1, 2018 at 14:22
1 Answer1
You could replaceclojure.core/load-lib (or a similar function likeload-one orload) with a wrapping function that logs/times each invocation:
(alter-var-root #'clojure.core/load-lib (fn [f] (fn [prefix lib & options] (println lib "loading...") (let [start (System/nanoTime) result (apply f prefix lib options) elapsed (double (/ (- (System/nanoTime) start) 1000000))] (println lib "loaded in" elapsed "ms") result))))Then whenever you load a file with ans declaration, or userequire/use, etc., you'll see output like this:
(require 'clojure.spec.test.alpha)clojure.spec.test.alpha loading...clojure.pprint loading...clojure.pprint loaded in 0.159361 msclojure.spec.alpha loading...clojure.spec.alpha loaded in 0.108056 msclojure.spec.gen.alpha loading...clojure.spec.gen.alpha loaded in 0.116731 msclojure.string loading...clojure.string loaded in 0.241387 msclojure.spec.test.alpha loaded in 103.37399 ms=> nilclojure.tools.namespace might also have some useful functionality for this.
Sign up to request clarification or add additional context in comments.
Comments
Explore related questions
See similar questions with these tags.
