問題描述
對于單元測試,我們使用 grunt/karma-runner/jasmine/phantom.js.因為我們試圖涵蓋任何新功能,所以許多單元測試迅速增長.而且,不幸的是,測試時間也在增加.現在它并不重要,1000 次測試需要 10 秒,但它會越來越差.
For unit-tests we use grunt/karma-runner/jasmine/phantom.js. Because we try to cover any new functionality, a number of unit-tests grows up rapidly. And, unfortunately, time for tests also increasing. Now it is not critical and takes 10 seconds for 1000 tests, but it goes worse and worse.
還有問題:
我知道,有些測試寫得不好(很費時間),但我應該優化哪一個?是否存在一些 karma/jasmine profiler 來測量每個測試的執行時間?
I know, some of the tests are bad-written (a lot of time consuming), but which one I should optimize? Is it exist some karma/jasmine profiler which measure time execution for each test?
我可以啟動更多的 karma-runner 線程/進程,因為 CPU 只使用了 5%-10%?單元測試真正獨立.
Can I start more karma-runner threads/processes, because CPU used only for 5%-10%? Unit-tests really independent.
每次我保存文件時,karma:watch 都會啟動所有測試,可能存在 karma-runner 的某些選項,它僅重新啟動當前文件夾的測試(我們使用規則: 單元測試 file.spec.js 存儲在與源 file.js 相同的文件夾中)?
Each time when I save file, karma:watch starts all tests, may be exist some option for karma-runner, which re-starts only tests for current folder (we use rule: unit tests file.spec.js stored in the same folder as source file.js)?
謝謝,
update1:??有人建議我使用 iit/ddescribe for jasmine(與 .only 用于 mocha 相同)和這是開發/調試的絕佳選擇,但可能以其他方式存在?
update1: Someone suggest me use iit/ddescribe for jasmine (the same as .only for mocha) and it is great option for develop/debugging, but may be exist some other way ?
我將問題發布到業力用戶論壇此處.
I post the question to karma-user forum here.
推薦答案
尋找慢速測試
reportSlowerThan
(點或進度報告器將以毫秒為單位報告所有比給定數字慢的測試),
reportSlowerThan
(dots or progress reporter will report all the tests that are slower than given number in ms),
您可以跳到瀏覽器并以與應用程序相同的方式分析您的測試(例如.flamecharts 在 Chrome 中很棒).
you can jump to the browser and profile your tests in the same way you would do it with an app (eg. flamecharts in Chrome are awesome).
加快測試速度
并行化:
我們正在努力解決這個問題.您將能夠跨多個瀏覽器并行化測試.當然,這些瀏覽器不必在同一臺機器上運行,因此您也可以使用多臺機器.
We are working on this. You will be able to parallelize the tests across multiple browsers. Of course, these browsers don't have to run on the same machine so you will be able to use multiple machines too.
我認為運行多個 Karma 實例本身并沒有太大幫助,因為它是以非常非阻塞"的方式編寫的,而且我還沒有看到它是瓶頸.然而,我們應該優化 CPU/內存使用.v0.12 將在這方面進行大量改進.
I don't think running multiple instances of Karma itself would help that much, as it is written in a very "non-blocking" way and I haven't seen it to be the bottleneck. We should however optimize the CPU/memory usage. A lot of improvements in this area is coming in v0.12.
只運行一部分測試:
你應該完全使用 iit
/ddescribe
(Jasmine).為什么這對你不起作用?順便提一句.Mocha 的 it.only()
和 describe.only()
的實現有問題,所以我推薦 Jasmine(或修復 Mocha).
You should totally use iit
/ddescribe
(Jasmine). Any reason why this does not work for you? Btw. Mocha's implementation of it.only()
and describe.only()
has issues, so I recommend Jasmine (or fixing Mocha).
還有一個 Google Closure plugin 的原型,它只加載你真正需要的文件需要(假設您 iit
進行一次測試,它只會加載/解析/評估您真正需要的 iit
文件).這顯著縮短了大型項目的啟動時間——我們應該為其他模塊加載器(RequireJS、CommonJS)做類似的技巧.
There is also a prototype of Google Closure plugin which only loads the files you really need (say you iit
one test, it will only load/parse/evaluate the files you really need for that iit
). This improves start time on huge projects significantly - we should do similar tricks for other module loaders (RequireJS, CommonJS).
編寫更快的代碼:
你可能已經知道了... ;-)
You probably know this already... ;-)
DOM 很慢.邏輯/DOM 的良好分離將允許您在不實際接觸 DOM 的情況下測試更多內容.如果您使用 Angular,您可以查看 測試指令示例.
DOM is slow. A good separation of logic/DOM will allow you to test more stuff without actually touching DOM. If you use Angular, you might check out the testing directives example.
使用依賴注入模式有很大幫助,因為您基本上可以最大限度地減少為每個測試準備(以及拆除)所需的環境.這是一個例子.
Using Dependency Injection pattern helps a lot as you basically minimize the environment you need to prepare (and also tear down) for each test. Here's an example.
一般來說,較低的測試速度更快.我認為最好用盡可能少的測試來涵蓋每個功能/問題.
In general, lower lower tests are faster. I think it's good to cover each functionality/issue with lowest possible test.
消除內存泄漏:
如果您的測試泄漏內存,瀏覽器會變得越來越慢,因此測試編號 1000 會非常慢,即使它寫得很好.確保每個測試都釋放所有引用.您可以使用 http://localhost:9876/debug.html
并分析內存.在執行之前檢查內存(在 Jasmine 執行所有 describe()
塊并收集測試之后)然后在執行測試之后 - 它應該是相同的.
If your tests leak memory, the browser will get slower and slower and so the test number 1000 will be pretty slow even though it's well written. Make sure each test frees up all the references. You can use http://localhost:9876/debug.html
and profile the memory. Check the memory before executing (after Jasmine executed all the describe()
blocks and collected the tests) and then after executing the tests - it should be the same.
發現一些內存泄漏比其他的更難,但它可以顯著提高速度 - 我已經看到僅通過消除內存泄漏從大約 5 分鐘到不到 1 分鐘.
Finding some memory leaks is harder than others, but it can significantly improve the speed - I've seen stuff like from ~5mins to under a minute just by eliminating memory leaks.
這篇關于單元測試業力跑步者/茉莉花分析的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!