Skip to content

Commit 7b4541a

Browse files
committed
Added ability to run individual performance tests
1 parent e9f5daa commit 7b4541a

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

perf/perf.coffee

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,11 @@ xmlNodeCount = (obj) ->
6767
total += xmlNodeCount item
6868
return total
6969

70-
doTest = (file, rep) ->
71-
rep ?= 100
70+
doTest = (file, options) ->
71+
options = _.extend { repeat: 10, time: true, mem: true }, options
7272
console.log ' XMLBuilder v' + require('../package.json').version
7373
console.log ' Testing: ' + file
74-
console.log ' Repeats: ' + rep
74+
console.log ' Repeats: ' + options.repeat
7575

7676
data = fs.readFileSync file, { encoding: 'utf8' }
7777
obj = JSON.parse data
@@ -85,25 +85,25 @@ doTest = (file, rep) ->
8585
stringMem = []
8686
nodeCount = 0
8787
strSize = 0
88-
i = rep
88+
i = options.repeat
8989
while i--
9090
xml = null
9191
str = null
92-
memwatch.gc()
92+
memwatch.gc() if options.mem
9393

94-
hd = new memwatch.HeapDiff()
95-
elapsed()
94+
hd = new memwatch.HeapDiff() if options.mem
95+
elapsed() if options.time
9696
xml = xmlbuilder.create(obj)
97-
buildTime.push elapsed()
98-
hde = hd.end()
99-
buildMem.push hde.change.size_bytes
97+
buildTime.push elapsed() if options.time
98+
hde = hd.end() if options.mem
99+
buildMem.push hde.change.size_bytes if options.mem
100100

101-
hd = new memwatch.HeapDiff()
102-
elapsed()
101+
hd = new memwatch.HeapDiff() if options.mem
102+
elapsed() if options.time
103103
str = xml.end({pretty: true})
104-
stringTime.push elapsed()
105-
hde = hd.end()
106-
stringMem.push hde.change.size_bytes
104+
stringTime.push elapsed() if options.time
105+
hde = hd.end() if options.mem
106+
stringMem.push hde.change.size_bytes if options.mem
107107

108108
if not nodeCount
109109
nodeCount = xmlNodeCount xml.root()
@@ -112,13 +112,12 @@ doTest = (file, rep) ->
112112
strSize = Buffer.byteLength str, 'utf8'
113113
console.log ' XML String Size: ' + fmtSize(strSize)
114114

115+
console.log ' Build XML:' if options.time or options.mem
116+
console.log ' Time: ' + fmtArr(buildTime, fmtTime) if options.time
117+
console.log ' Memory: ' + fmtArr(buildMem, fmtSize)if options.mem
118+
console.log ' Convert to String:' if options.time or options.mem
119+
console.log ' Time: ' + fmtArr(stringTime, fmtTime) if options.time
120+
console.log ' Memory: ' + fmtArr(stringMem, fmtSize) if options.mem
115121

116-
console.log ' Build XML:'
117-
console.log ' Time: ' + fmtArr(buildTime, fmtTime)
118-
console.log ' Memory: ' + fmtArr(buildMem, fmtSize)
119-
console.log ' Convert to String:'
120-
console.log ' Time: ' + fmtArr(stringTime, fmtTime)
121-
console.log ' Memory: ' + fmtArr(stringMem, fmtSize)
122-
123-
doTest __dirname + '/test.json', 2
122+
doTest __dirname + '/test.json', { repeat: 2 }
124123

0 commit comments

Comments
 (0)