|
| 1 | +const queries = [ |
| 2 | + 'select', |
| 3 | + 'select_arg', |
<
2364
td data-grid-cell-id="diff-e727e4bdf3657fd1d798edcd6b099d6e092f8573cba266154583a746bba0f346-empty-4-0" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">
4 | + 'select_args', |
| 5 | + 'select_where' |
| 6 | + ] |
| 7 | + , clients = [ |
| 8 | + 'postgres', |
| 9 | + 'pg-promise', |
| 10 | + 'pg', |
| 11 | + 'pg-native', |
| 12 | + //'slonik' |
| 13 | + ] |
| 14 | + , warmup = 3 |
| 15 | + , iterations = 10000 |
| 16 | + , rounds = 5 |
| 17 | + , variance = false |
| 18 | + |
| 19 | +const series = (fn, xs) => { |
| 20 | + const results = [] |
| 21 | + return xs.reduce((acc, x) => acc.then(() => fn(x).then(r => results.push(r))), Promise.resolve({})).then(() => results) |
| 22 | +} |
| 23 | + |
| 24 | +series(test, clients).then(results => { |
| 25 | + results.forEach(x => { |
| 26 | + x.queries = Object.values(x.queries).filter(q => q.times).map(q => { |
| 27 | + q.times = q.times.slice(warmup).map(x => x[0] + x[1] / 1e9) |
| 28 | + const avg = (q.times.reduce((a, b) => a + b) / q.times.length) |
| 29 | + , sorted = q.times.slice().sort((a, b) => a - b) |
| 30 | + |
| 31 | + return { |
| 32 | + time: avg, |
| 33 | + variance: 1 / sorted[0] * (sorted[sorted.length - 1] - sorted[0]) * 100 |
| 34 | + } |
| 35 | + }) |
| 36 | + }) |
| 37 | + |
| 38 | + queries.forEach((q, i) => |
| 39 | + results.map(x => x.queries[i]).sort((a, b) => b.time - a.time).forEach((x, i, xs) => { |
| 40 | + x.diff = xs[0].time / x.time |
| 41 | + }) |
| 42 | + ) |
| 43 | + |
| 44 | + console.log([pad(1, 10, ' ')('client')].concat(queries.map(pad(0, 14, ' '))).join(' | ')) |
| 45 | + console.log([pad(1, 10, '-')(':')].concat(queries.map(x => ':').map(pad(0, 14, '-'))).join(' | ')) |
| 46 | + results.forEach(x => |
| 47 | + console.log( |
| 48 | + [pad(1, 10, ' ')(x.client)].concat( |
| 49 | + x.queries.map(x => |
| 50 | + x.time.toFixed(3) + 's' + ' (' + x.diff.toFixed(1) + 'x)' |
| 51 | + ).map(pad(0, 14, ' ')) |
| 52 | + ).join(' | ') |
| 53 | + ) |
| 54 | + ) |
| 55 | + |
| 56 | + console.log(['client'].concat(queries).join(';')) |
| 57 | + results.forEach(x => |
| 58 | + console.log([x.client].concat(x.queries.map(x => x.time.toFixed(3))).join(';').replace(/\./g, ',')) |
| 59 | + ) |
| 60 | + |
| 61 | + process.exit() |
| 62 | +}) |
| 63 | + |
| 64 | +function pad(p, n, c) { |
| 65 | + return x => { |
| 66 | + while (x.length < n) |
| 67 | + x = p ? x + c : c + x |
| 68 | + return x |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +async function test(clientName, done) { |
| 73 | + return new Promise(async resolve => { |
| 74 | + const client = await Promise.resolve(require('./' + clientName)) |
| 75 | + const q = queries.flatMap(x => Array(rounds + warmup).fill(x)) |
| 76 | + |
| 77 | + async function run(x) { |
| 78 | + x.start = process.hrtime() |
| 79 | + let j = 0 |
| 80 | + for(let i = 0; i < iterations; i++) |
| 81 | + x().then(() => ++j === iterations && next(x)) |
| 82 | + } |
| 83 | + |
| 84 | + function next(x) { |
| 85 | + x && (x.times = (x.times || []).concat([process.hrtime(x.start)])) |
| 86 | + q.length |
| 87 | + ? setTimeout(() => run(client.queries[q.pop()]), 100) |
| 88 | + : Promise.resolve(client.end()).then(() => resolve({ client: clientName, queries: client.queries })) |
| 89 | + } |
| 90 | + |
| 91 | + next() |
| 92 | + }) |
| 93 | +} |
0 commit comments