我正在用Jest测试定制变压器的性能。当前,转换器不执行任何操作,仅返回从Jest获取的代码。变压器实现了该getCacheKey功能。
这是变压器的完整代码:
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;
Run Code Online (Sandbox Code Playgroud)
开玩笑的配置,package.json如下所示:
"jest": {
"transform": {
"^.+\\.tsx?$": "<rootDir>/ts-transformer.js"
},
"testMatch": [
"<rootDir>/test-jest/**/*.ts"
],
"moduleFileExtensions": [
"ts",
"tsx",
"js",
"json"
]
}
Run Code Online (Sandbox Code Playgroud)
使用Jest测试此设置时,无论有无,它花费的时间都是相同的--no-cache(大约9秒)
使用Mocha测试此设置时,第一次运行大约需要7秒钟,随后的运行大约需要4秒钟。
在这两种情况下(带有笑话和摩卡),都在不更改任何源文件或测试文件的情况下测试了后续运行。
我的问题:
我使用简单的阶乘函数执行了此测试(借用http://avelino.xxx/2014/03/golang-c-and-python-the-benchmark-time中的逻辑)
常规PHP代码
function fact($n){
if($n===0)return 1;
return $n*fact($n-1);
}
function calc(){
$t = 0;
for($i=0; $i<100000; $i++){
for($j=0; $j<8; $j++){
$t += fact($j);
}
}
return $t;
}
$result = calc();
echo $result."\n";
Run Code Online (Sandbox Code Playgroud)
PHP使用Zephir
$fact = new Utils\Fact();
$result = $fact->calc();
echo $result."\n";
Run Code Online (Sandbox Code Playgroud)
Zephir代码
namespace Utils;
class Fact{
public function fact(int n) -> int{
if(n==0){
return 1;
}
return n*this->fact(n - 1);
}
public function calc() -> int{
int i,j,total;
let total = 0;
for i in range(0,99999){ …Run Code Online (Sandbox Code Playgroud)