diff --git a/README.md b/README.md index 722b82ed..78a7a5f1 100644 --- a/README.md +++ b/README.md @@ -121,10 +121,10 @@ npm install npm test # To view performance: -node test/performance.js +npm run test:performance # To view test coverage: -npm run test:coverage +npm run test:cov open coverage/index.html ``` diff --git a/package.json b/package.json index 47031cfa..74e8b995 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,7 @@ "test": "mocha --reporter spec --check-leaks --grep .", "test:ci": "nyc --exclude test --reporter=lcovonly --reporter=text npm test", "test:cov": "nyc --exclude test --reporter=html --reporter=text npm test", - "test:performance": "node performance/index.js", + "test:performance": "node --allow-natives-syntax performance/index.js", "test:tap": "mocha --reporter tap --check-leaks --grep .", "test:webpack": "npm pack && mv iconv-lite-*.tgz test/webpack/iconv-lite.tgz && cd test/webpack && npm install && npm run test && rm iconv-lite.tgz" }, @@ -34,6 +34,7 @@ }, "devDependencies": { "async": "^3.2.0", + "bench-node": "^0.10.0", "errto": "^0.2.1", "iconv": "^2.3.5", "mocha": "^6.2.2", diff --git a/performance/index.js b/performance/index.js index 4e5f541b..156b4fff 100644 --- a/performance/index.js +++ b/performance/index.js @@ -1,67 +1,41 @@ -var iconv = require('iconv'); -var iconv_lite = require("../lib"); - -var encoding = process.argv[2] || "windows-1251"; -var convertTimes = 10000; - -var encodingStrings = { +const iconv = require('iconv'); +const iconv_lite = require("../lib"); +const { Suite } = require('bench-node'); + +const suite = new Suite({ + pretty: true, + reporterOptions: { + printHeader: true // Set to false to hide system info header + } +}); + +const encodingStrings = { 'windows-1251': 'This is a test string 32 chars..', - 'gbk': '这是中文字符测试。。!@¥%12', + 'gbk': '这是中文字符测试。。!@¥%12', 'utf8': '这是中文字符测试。。!@¥%12This is a test string 48 chars..', }; -// Test encoding. -var str = encodingStrings[encoding]; -if (!str) { - throw new Error('Don\'t support ' + encoding + ' performance test.'); -} -for (var i = 0; i < 13; i++) { - str = str + str; -} - -console.log('\n' + encoding + ' charset performance test:'); -console.log("\nEncoding "+str.length+" chars "+convertTimes+" times:"); - -var start = Date.now(); -var converter = new iconv.Iconv("utf8", encoding); -for (var i = 0; i < convertTimes; i++) { - var b = converter.convert(str); -} - -var duration = Date.now() - start; -var mbs = convertTimes*b.length/duration/1024; - -console.log("iconv: "+duration+"ms, "+mbs.toFixed(2)+" Mb/s."); -var start = Date.now(); -for (var i = 0; i < convertTimes; i++) { - var b = iconv_lite.encode(str, encoding); +for (const [encoding, string] of Object.entries(encodingStrings)) { + suite.add(`${encoding}/encode/iconv-lite`, function () { + iconv_lite.encode(string, encoding); + }) + suite.add(`${encoding}/encode/iconv`, function () { + const converter = new iconv.Iconv("utf8", encoding); + converter.convert(string); + }) + suite.add(`${encoding}/decode/iconv-lite`, function (timer) { + const buffer = iconv_lite.encode(string, encoding) + timer.start(); + iconv_lite.decode(buffer, encoding); + timer.end(); + }) + suite.add(`${encoding}/decode/iconv`, function (timer) { + const buffer = iconv_lite.encode(string, encoding) + timer.start(); + const converter = new iconv.Iconv(encoding, "utf8"); + converter.convert(buffer).toString(); + timer.end(); + }) } -var duration = Date.now() - start; -var mbs = convertTimes*b.length/duration/1024; - -console.log("iconv-lite: "+duration+"ms, "+mbs.toFixed(2)+" Mb/s."); - - -// Test decoding. -var buf = iconv_lite.encode(str, encoding); -console.log("\nDecoding "+buf.length+" bytes "+convertTimes+" times:"); - -var start = Date.now(); -var converter = new iconv.Iconv(encoding, "utf8"); -for (var i = 0; i < convertTimes; i++) { - var s = converter.convert(buf).toString(); -} -var duration = Date.now() - start; -var mbs = convertTimes*buf.length/duration/1024; - -console.log("iconv: "+duration+"ms, "+mbs.toFixed(2)+" Mb/s."); - -var start = Date.now(); -for (var i = 0; i < convertTimes; i++) { - var s = iconv_lite.decode(buf, encoding); -} -var duration = Date.now() - start; -var mbs = convertTimes*buf.length/duration/1024; - -console.log("iconv-lite: "+duration+"ms, "+mbs.toFixed(2)+" Mb/s."); +suite.run()