diff --git a/README.md b/README.md index 45dbc8d..b684f9f 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,11 @@ Run #1000: 0.4652369999999948 ms ... Run #10000: 0.4628829999999766 ms ``` + +### Running the renderServer test +``` +$ node test/renderServer.test.js +``` ### Machine Info These tests were run using a MacBook Pro (Retina, 15-inch, Mid 2014) with 2.8 GHz Intel Core i7 processor and 16 GB 1600 MHz DDR3 of memory. diff --git a/ReactMicroBenchmark.js b/ReactMicroBenchmark.js index 4100aad..38c9ed7 100644 --- a/ReactMicroBenchmark.js +++ b/ReactMicroBenchmark.js @@ -107,18 +107,24 @@ var renderServer = function (comments) { ); }; -log = console.log.bind(console); -timestamp = function() { return process.hrtime()[1] / 1e6; }; +module.exports = { + renderServer: renderServer +}; -var comments = []; -var NUM_COMMENTS = 10; -for (var i = 0; i < NUM_COMMENTS; ++i) { - comments.push({author:"Name "+i, text:"This is comment #"+i+"."}); -} -for (i = 0; i < 10000; ++i) { - var start, stop; - start = timestamp(); - renderServer(comments); - stop = timestamp(); - log('Run #' + (i + 1) + ':', (stop - start), 'ms'); +var log = console.log.bind(console); +var timestamp = function() { return process.hrtime()[1] / 1e6; }; + +if (require.main === module) { + var comments = []; + var NUM_COMMENTS = 10; + for (var i = 0; i < NUM_COMMENTS; ++i) { + comments.push({author:"Name "+i, text:"This is comment #"+i+"."}); + } + for (i = 0; i < 10000; ++i) { + var start, stop; + start = timestamp(); + renderServer(comments); + stop = timestamp(); + log('Run #' + (i + 1) + ':', (stop - start), 'ms'); + } } diff --git a/test/renderServer.test.js b/test/renderServer.test.js new file mode 100644 index 0000000..3800654 --- /dev/null +++ b/test/renderServer.test.js @@ -0,0 +1,13 @@ +const assert = require('assert'); +const { renderServer } = require('../ReactMicroBenchmark'); + +const result = renderServer([ + { author: 'Alice', text: 'Hello' }, + { author: 'Bob', text: 'Hi there' } +]); + +assert.strictEqual(typeof result, 'string', 'renderServer should return a string'); +assert.ok(result.length > 0, 'renderServer result should not be empty'); +assert.ok(result.includes('