-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbench.js
More file actions
103 lines (87 loc) · 2.33 KB
/
bench.js
File metadata and controls
103 lines (87 loc) · 2.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import 'dotenv/config'
import util from 'node:util'
import pg from 'pg'
import assert from 'node:assert/strict'
import Benchmark from 'benchmark'
import PGPubSub from './src/pg-notify.js'
const suite = new Benchmark.Suite()
const sleep = util.promisify(setTimeout)
const iterations = 100
async function runPgNotifyBench () {
const pubsub = new PGPubSub({
reconnectMaxRetries: 100000,
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
ssl: process.env.DB_SSL === 'true' ? { rejectUnauthorized: false } : false
})
await pubsub.connect()
const state = { expected: iterations, actual: 0 }
pubsub.on('test', (payload) => {
state.actual++
assert.equal(payload, 'payload')
})
for (let i = 0; i < state.expected; i++) {
await pubsub.emit('test', 'payload')
}
while (true) {
if (state.actual === state.expected) {
break
}
await sleep(1)
}
await pubsub.close()
}
async function runPgRawBench () {
const client = new pg.Client({
host: process.env.DB_HOST,
port: process.env.DB_PORT,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
ssl: process.env.DB_SSL === 'true' ? { rejectUnauthorized: false } : false
})
await client.connect()
const state = { expected: iterations, actual: 0 }
await client.query('LISTEN test')
client.on('notification', (notification) => {
state.actual++
assert.equal(notification.payload, 'payload')
})
for (let i = 0; i < state.expected; i++) {
await client.query("NOTIFY test, 'payload'")
}
while (true) {
if (state.actual === state.expected) {
break
}
await sleep(1)
}
await client.end()
}
(async () => {
suite
.add('pg', {
defer: true,
minSamples: 50,
fn (deferred) {
runPgRawBench().then(() => deferred.resolve())
}
})
.add('pg-notify', {
defer: true,
minSamples: 50,
fn (deferred) {
runPgNotifyBench().then(() => deferred.resolve())
}
})
.on('cycle', function (event) {
console.log(String(event.target))
})
.on('complete', function () {
console.log('Fastest is ' + this.filter('fastest').map('name'))
})
.run({ async: true })
})()