繁体   English   中英

如何测量每个名称空间运行clojure测试所花费的时间?

[英]How do I measure time taken per namepace to run clojure tests?

有没有办法检测代码并找出每个命名空间需要多少时间,或者唯一的方法是使用fixture?

解决这个问题的好方法是什么?

您在命名空间列表上使用run-tests建议将起作用。

这是我写的一个函数,用于在repl中循环命名空间,重新加载它们,然后为每个ns运行测试。 您可以为您的目的修改代码。 您必须添加代码以捕获开始/结束时间并打印每个ns的已用时间。

正如docstring所说,这段代码假定命名空间的设置如下:

    myproj.myfeat      ; main namespace
tst.myproj.myfeat      ; testing namespace

所以只需根据设置要求(symbol (str "tst." curr-ns))或修改部件。 您可能希望使用all-ns来获取所有名称空间的列表,然后删除或忽略非测试名称空间。 祝好运!

(defn ^:deprecated ^:no-doc test-all
  "Convenience fn to reload a namespace & the corresponding test namespace from disk and
  execute tests in the REPL.  Assumes canonical project test file organization with
  parallel src/... & test/tst/... directories, where a 'tst.' prefix is added to all src
  namespaces to generate the cooresponding test namespace.  Example:

    (test-all 'tupelo.core 'tupelo.csv)

  This will reload tupelo.core, tst.tupelo.core, tupelo.csv, tst.tupelo.csv and
  then execute clojure.test/run-tests on both of the test namespaces."
  [& ns-list]
  (let [test-ns-list (for [curr-ns ns-list]
                       (let [curr-ns-test (symbol (str "tst." curr-ns))]
                         (println (str "testing " curr-ns " & " curr-ns-test))
                         (require curr-ns curr-ns-test :reload)
                         curr-ns-test))
        ]
    (println "-----------------------------------------------------------------------------")
    (apply clojure.test/run-tests test-ns-list)
    (println "-----------------------------------------------------------------------------")
    (newline)
    ))

您可以使用eftest:test-warn-time来测量每个NS的测试时间。

添加后,您将看到这样的彩色(未显示)输出,表示测试缓慢:

LONG TEST in foo-api.handlers.foo-test during :clojure.test/once-fixtures
Test took 12.300 seconds seconds to run

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM