我想对Clojure文件的加载速度进行一些计时。 有没有办法钩入装载机以获取此信息?
我想获得这样的信息:
ie. A depends on B depends on C
A -> 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 50ms
B -> loaded at T(b1), time taken 100ms
A -> loaded at T(a1), time taken 200ms
答案 0 :(得分:5)
您可以用包装功能(将每次调用记录/计时)替换clojure.core/load-lib
(或类似的功能,例如load-one
或load
)
(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))))
然后,无论何时使用ns
声明加载文件,或使用require
/ use
等,您都会看到如下输出:
(require 'clojure.spec.test.alpha)
clojure.spec.test.alpha loading...
clojure.pprint loading...
clojure.pprint loaded in 0.159361 ms
clojure.spec.alpha loading...
clojure.spec.alpha loaded in 0.108056 ms
clojure.spec.gen.alpha loading...
clojure.spec.gen.alpha loaded in 0.116731 ms
clojure.string loading...
clojure.string loaded in 0.241387 ms
clojure.spec.test.alpha loaded in 103.37399 ms
=> nil
clojure.tools.namespace可能对此也有一些有用的功能。