1

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
askedSep 1, 2018 at 14:09
zcaudate's user avatar
1
  • Mount works out dependencies by namespace loading, so you could looks at its source code.CommentedSep 1, 2018 at 14:22

1 Answer1

5

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=> nil

clojure.tools.namespace might also have some useful functionality for this.

answeredSep 1, 2018 at 15:14
Taylor Wood's user avatar
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.