Skip to content

Commit 2a245e9

Browse files
committed
j2x:add performance test
1 parent 1c3faf8 commit 2a245e9

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

benchmark/perfTest4.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
var Benchmark = require('benchmark');
2+
var suite = new Benchmark.Suite("XML Parser benchmark");
3+
4+
var Parser = require("../src/j2x");
5+
var parser = new Parser({
6+
format: true,
7+
//supressEmptyNode: true
8+
});
9+
var xml2js = require("xml2js");
10+
var builder = new xml2js.Builder();
11+
//var jsonxml = require('jsontoxml');
12+
13+
var fs = require("fs");
14+
var path = require("path");
15+
var fileNamePath = path.join(__dirname, "../spec/assets/test.json");//1.5k
16+
var jsonData = JSON.parse(fs.readFileSync(fileNamePath).toString());
17+
//xmlData=`<root>${xmlData.repeat(1000)}</root>`;
18+
19+
20+
/* console.log(parser.parse(jsonData));
21+
console.log("----------------")
22+
console.log(builder.buildObject(jsonData));
23+
console.log("----------------")
24+
console.log(jsonxml(jsonData)); */
25+
26+
27+
suite
28+
.add('j2x', function() {
29+
parser.parse(jsonData);
30+
//parser.parse(jsonData,{format:true});
31+
})
32+
.add('xml2js ', function() {
33+
var xml = builder.buildObject(jsonData);
34+
})
35+
/* .add('jsontoxml ', function() {
36+
var xml = jsonxml(jsonData);
37+
}) */
38+
39+
.on('start',function(){
40+
console.log("Running Suite: " + this.name);
41+
})
42+
.on('error',function(e){
43+
console.log("Error in Suite: ",e);
44+
})
45+
.on('abort',function(e){
46+
console.log("Aborting Suite: " + this.name);
47+
})
48+
//.on('cycle',function(event){
49+
// console.log("Suite ID:" + event.target.id);
50+
//})
51+
// add listeners
52+
.on('complete', function() {
53+
for (var j = 0; j < this.length; j++) {
54+
console.log(this[j].name + " : " + this[j].hz + " requests/second");
55+
}
56+
})
57+
// run async
58+
.run({ 'async': true });

spec/assets/test.json

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"any_name": {
3+
"person": [
4+
{
5+
"phone": [
6+
"+122233344550",
7+
"+122233344551"
8+
],
9+
"name": "Jack",
10+
"age": "33",
11+
"emptyNode": "",
12+
"booleanNode": [
13+
"false",
14+
"true"
15+
],
16+
"selfclosing": [
17+
"",
18+
{
19+
"_with": "value"
20+
}
21+
],
22+
"married": {
23+
"_firstTime": "No",
24+
"_attr": "val 2",
25+
"__text": "Yes"
26+
},
27+
"birthday": "Wed, 28 Mar 1979 12:13:14 +0300",
28+
"address": [
29+
{
30+
"city": "New York",
31+
"street": "Park Ave",
32+
"buildingNo": "1",
33+
"flatNo": "1"
34+
},
35+
{
36+
"city": "Boston",
37+
"street": "Centre St",
38+
"buildingNo": "33",
39+
"flatNo": "24"
40+
}
41+
],
42+
"_id": "101"
43+
},
44+
{
45+
"phone": [
46+
"+122233344553",
47+
"+122233344554"
48+
],
49+
"name": "Boris",
50+
"age": "34",
51+
"married": {
52+
"_firstTime": "Yes",
53+
"__text": "Yes"
54+
},
55+
"birthday": "Mon, 31 Aug 1970 02:03:04 +0300",
56+
"address": [
57+
{
58+
"city": "Moscow",
59+
"street": "Kahovka",
60+
"buildingNo": "1",
61+
"flatNo": "2"
62+
},
63+
{
64+
"city": "Tula",
65+
"street": "Lenina",
66+
"buildingNo": "3",
67+
"flatNo": "78"
68+
}
69+
],
70+
"_id": "102"
71+
}
72+
],
73+
"_attr": "https://example.com/somepath"
74+
}
75+
}

0 commit comments

Comments
 (0)