Skip to content

Commit 53d15b4

Browse files
committedJan 15, 2024
fix: rewrite log
1 parent bd1d01b commit 53d15b4

File tree

10 files changed

+42
-381
lines changed

10 files changed

+42
-381
lines changed
 

‎common/scripts/checkToday.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ const { temExe } = require("../utils/temExe");
33
/**
44
* 检查今天的题目
55
*/
6-
const today = readStore("today-tag");
6+
const questionInfo = readStore("today-question-info")
77
// const today = 2645;
8-
console.log("今日的题目编号/名称为:" + today);
9-
temExe('node ./src/{0}/index.js', today)
10-
.then(res => console.log(`执行结果:\n${res}`))
8+
console.log(`今日题目: ${questionInfo.id}.${questionInfo.title}`);
9+
temExe('node ./src/{0}/index.js', questionInfo.id)
10+
.then(res => console.log(`${res}`))
1111
.catch(e => console.log("执行报错: ", e));

‎common/template/template.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { withTimeLog } = require("../../common/utils/withTimeLog");
1+
const { showLogs } = require("../../common/utils/withTimeLog");
22
/**
33
* @题目
44
* @描述

‎common/utils/getTestCase.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ function getTestCase(question){
99
const cases = question.detail.match(/<strong><\/strong>.+\n/g).map(str=>str.replaceAll("<strong>输入:","").replaceAll("</strong>","").replace("\n",""))
1010
const expires = question.detail.match(/<strong><\/strong>.+\n/g).map(str=>str.replaceAll("<strong>输出:</strong>","").replace("\n",""))
1111
const functionName = question.jsCode.match(/var.+=/g)[0].replace("var ","").replace(" =","");
12-
return cases.map((c,i)=>`withTimeLog(() => ${functionName}(${c}),${expires[i]});\n`).join("");
12+
return `showLogs(${functionName}, [${cases}], [${expires}])`
1313
}
1414
module.exports = {getTestCase}

‎common/utils/withTimeLog.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,35 @@ const v8 = require("v8");
22
const { getFileSize } = require("./sizeUtil");
33
const { isSameData } = require("./isSameData");
44

5-
function withTimeLog(callback, compare) {
5+
function withTimeLog(fnName, param, compare) {
66
// 记录开始时间
77
const startTime = performance.now();
88
// 获取函数执行前的内存使用情况
99
const startHeapStatsArray = v8.getHeapSpaceStatistics()
10-
const callVal = callback()
10+
const callVal = fnName(param)
1111
// 获取函数执行后的内存使用情况
1212
const endHeapStatsArray = v8.getHeapSpaceStatistics()
1313
// 记录结束时间
1414
const endTime = performance.now();
1515
const startHeapStats = startHeapStatsArray.reduce((prev, curr) => prev += curr.space_used_size, 0)
1616
const endHeapStats = endHeapStatsArray.reduce((prev, curr) => prev += curr.space_used_size, 0)
1717

18-
console.table({
19-
'函数执行结果': JSON.stringify(callVal),
20-
'函数执行用时': Number(endTime - startTime).toFixed(4) + 'ms',
21-
'内存占用': getFileSize(endHeapStats - startHeapStats),
22-
[`预期值${JSON.stringify(compare)}`]: isSameData(callVal, compare) ? "测试通过!" : "测试未通过!"
18+
return {
19+
'测试结果': isSameData(callVal, compare) ? '通过' : '未通过',
20+
'预期结果': JSON.stringify(compare),
21+
'执行结果': JSON.stringify(callVal),
22+
'执行用时': Number(endTime - startTime).toFixed(4) + 'ms',
23+
'内存占用': getFileSize(endHeapStats - startHeapStats)
24+
}
25+
}
26+
27+
function showLogs(fnName, paramArr, compareArr) {
28+
let logsItems = []
29+
paramArr.forEach((param, index) => {
30+
const logItem = withTimeLog(fnName, param, compareArr[index])
31+
logsItems.push(logItem)
2332
})
24-
console.log('--------------------------')
33+
console.table(logsItems)
2534
}
2635

27-
module.exports = { withTimeLog };
36+
module.exports = { showLogs };

‎src/2085/index.js

-59
This file was deleted.

0 commit comments

Comments
 (0)