|
| 1 | +/** |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +// Make sure to run node with --expose-gc option! |
| 9 | + |
| 10 | +// The times are reliable if about 1% relative mean error if you run it: |
| 11 | + |
| 12 | +// * immediately after restart |
| 13 | +// * with 100% battery charge |
| 14 | +// * not connected to network |
| 15 | + |
| 16 | +/* eslint import/no-extraneous-dependencies: "off" */ |
| 17 | + |
| 18 | +const Benchmark = require('benchmark'); |
| 19 | + |
| 20 | +const diffBaseline = require('diff').diffLines; |
| 21 | +const diffImproved = require('../build/index.js').default; |
| 22 | + |
| 23 | +const testBaseline = (a, b) => { |
| 24 | + const benchmark = new Benchmark({ |
| 25 | + fn() { |
| 26 | + diffBaseline(a, b); |
| 27 | + }, |
| 28 | + name: 'baseline', |
| 29 | + onCycle() { |
| 30 | + global.gc(); // after run cycle |
| 31 | + }, |
| 32 | + onStart() { |
| 33 | + global.gc(); // when benchmark starts |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + benchmark.run({async: false}); |
| 38 | + |
| 39 | + return benchmark.stats; |
| 40 | +}; |
| 41 | + |
| 42 | +const testImproved = function(a, b) { |
| 43 | + const benchmark = new Benchmark({ |
| 44 | + fn() { |
| 45 | + // Split string arguments to make fair comparison with baseline. |
| 46 | + const aItems = a.split('\n'); |
| 47 | + const bItems = b.split('\n'); |
| 48 | + |
| 49 | + const isCommon = (aIndex, bIndex) => aItems[aIndex] === bItems[bIndex]; |
| 50 | + |
| 51 | + // This callback obviously does less than baseline `diff` package, |
| 52 | + // but avoiding double work and memory churn is the goal. |
| 53 | + // For example, `jest-diff` has had to split strings that `diff` joins. |
| 54 | + const foundSubsequence = () => {}; |
| 55 | + |
| 56 | + diffImproved(aItems.length, bItems.length, isCommon, foundSubsequence); |
| 57 | + }, |
| 58 | + name: 'improved', |
| 59 | + onCycle() { |
| 60 | + global.gc(); // after run cycle |
| 61 | + }, |
| 62 | + onStart() { |
| 63 | + global.gc(); // when benchmark starts |
| 64 | + }, |
| 65 | + }); |
| 66 | + |
| 67 | + benchmark.run({async: false}); |
| 68 | + |
| 69 | + return benchmark.stats; |
| 70 | +}; |
| 71 | + |
| 72 | +const writeHeading2 = () => { |
| 73 | + console.log('## Benchmark time for `diff-sequences` versus `diff`\n'); |
| 74 | + console.log('A ratio less than 1.0 means `diff-sequences` is faster.'); |
| 75 | +}; |
| 76 | + |
| 77 | +const writeHeading3 = n => { |
| 78 | + console.log(`\n### n = ${n}\n`); |
| 79 | + console.log('| name | % | ratio | improved | rme | baseline | rme |'); |
| 80 | + console.log('| :--- | ---: | :--- | :--- | ---: | :--- | ---: |'); |
| 81 | +}; |
| 82 | + |
| 83 | +const writeRow = (name, percent, statsImproved, statsBaseline) => { |
| 84 | + const {mean: meanImproved, rme: rmeImproved} = statsImproved; |
| 85 | + const {mean: meanBaseline, rme: rmeBaseline} = statsBaseline; |
| 86 | + const ratio = meanImproved / meanBaseline; |
| 87 | + |
| 88 | + console.log( |
| 89 | + `| ${name} | ${percent}% | ${ratio.toFixed( |
| 90 | + 4, |
| 91 | + )} | ${meanImproved.toExponential(4)} | ${rmeImproved.toFixed( |
| 92 | + 2, |
| 93 | + )}% | ${meanBaseline.toExponential(4)} | ${rmeBaseline.toFixed(2)}% |`, |
| 94 | + ); |
| 95 | +}; |
| 96 | + |
| 97 | +const testDeleteInsert = (tenths, more, less) => { |
| 98 | + // For improved `diff-sequences` package, delete is often slower than insert. |
| 99 | + const statsDeleteImproved = testImproved(more, less); |
| 100 | + const statsDeleteBaseline = testBaseline(more, less); |
| 101 | + writeRow('delete', tenths * 10, statsDeleteImproved, statsDeleteBaseline); |
| 102 | + |
| 103 | + // For baseline `diff` package, many insertions is serious perf problem. |
| 104 | + // However, the benchmark package cannot accurately measure for large n. |
| 105 | + const statsInsertBaseline = testBaseline(less, more); |
| 106 | + const statsInsertImproved = testImproved(less, more); |
| 107 | + writeRow('insert', tenths * 10, statsInsertImproved, statsInsertBaseline); |
| 108 | +}; |
| 109 | + |
| 110 | +const testChange = (tenths, expected, received) => { |
| 111 | + const statsImproved = testImproved(expected, received); |
| 112 | + const statsBaseline = testBaseline(expected, received); |
| 113 | + writeRow('change', tenths * 10, statsImproved, statsBaseline); |
| 114 | +}; |
| 115 | + |
| 116 | +const getItems = (n, callback) => { |
| 117 | + const items = []; |
| 118 | + |
| 119 | + for (let i = 0; i !== n; i += 1) { |
| 120 | + const item = callback(i); |
| 121 | + if (typeof item === 'string') { |
| 122 | + items.push(item); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return items.join('\n'); |
| 127 | +}; |
| 128 | + |
| 129 | +// Simulate change of property name which is usually not same line. |
| 130 | +// Expected: 0 1 2 3 4 5 6 7 8 9 and so on |
| 131 | +// Received: 1 2 3 4 x0 5 6 7 8 9 and so on |
| 132 | +const change2 = i => { |
| 133 | + const j = i % 10; |
| 134 | + return j === 4 ? `x${i - 4}` : j < 4 ? `${i + 1}` : `${i}`; |
| 135 | +}; |
| 136 | + |
| 137 | +const testLength = n => { |
| 138 | + const all = getItems(n, i => `${i}`); |
| 139 | + |
| 140 | + writeHeading3(n); |
| 141 | + |
| 142 | + [2, 4, 8].forEach(tenth => { |
| 143 | + testDeleteInsert(tenth, all, getItems(n, i => i % 10 >= tenth && `${i}`)); |
| 144 | + }); |
| 145 | + testChange(1, all, getItems(n, i => (i % 10 === 0 ? `x${i}` : `${i}`))); |
| 146 | + testChange(2, all, getItems(n, change2)); |
| 147 | + testChange(5, all, getItems(n, i => (i % 2 === 0 ? `x${i}` : `${i}`))); |
| 148 | + testChange(10, all, getItems(n, i => `x${i}`)); // simulate TDD |
| 149 | +}; |
| 150 | + |
| 151 | +writeHeading2(); |
| 152 | + |
| 153 | +testLength(20); |
| 154 | +testLength(200); |
| 155 | +testLength(2000); |
0 commit comments