2017-07-28 21 views
4

我正在用Jest測試一個定製變壓器的性能。目前,變壓器只會返回從Jest獲得的代碼。變壓器已實施getCacheKey功能。Jest定製變壓器 - 可以改善其性能?

下面是變壓器的全部代碼:

function process(src, path, config, transformOptions) { 
    return src; 
} 
exports.process = process; 

function getCacheKey(fileData, filePath, configStr, options) { 
    return crypto.createHash('md5') 
    .update(fileData + filePath + configStr, 'utf8') 
    .digest('hex'); 
} 
exports.getCacheKey = getCacheKey; 

Link to the transformer

的笑話配置,在package.json如下:

"jest": { 
    "transform": { 
    "^.+\\.tsx?$": "<rootDir>/ts-transformer.js" 
    }, 
    "testMatch": [ 
    "<rootDir>/test-jest/**/*.ts" 
    ], 
    "moduleFileExtensions": [ 
    "ts", 
    "tsx", 
    "js", 
    "json" 
    ] 
} 

Link to package.json

當測試此設置與Jest,它需要在有和沒有--no-cache(約9秒)的情況下的相同時間量

當使用Mocha測試此設置時,第一次運行約需7秒,後續運行約需4秒。

在兩種情況下(包括笑話和摩卡),後續運行都進行了測試,而不更改任何源文件或測試文件。

我的問題:

  • 不宜隨後玩笑運行更快由於緩存?
  • 變壓器有什麼東西阻礙了測試持續時間的改善?
  • Jest發生的最小開銷是什麼造成了這個問題?

回答

0

它可能更快地更新件(fileData,filePath,configStr)分開,因此不必是連接文件內容的副本。

function getCacheKey(fileData, filePath, configStr, options) { 
    const hash = crypto.createHash('md5'); 
    hash.update(fileData); 
    hash.update(filePath); 
    hash.update(configStr); 
    return hash.digest('hex'); 
} 

注意:如果未提供編碼,並且數據是字符串,則會強制執行'utf8'的編碼。

+0

這樣做對持續時間沒有任何顯着影響 – Kul