-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
subs.js
164 lines (145 loc) · 4 KB
/
subs.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { check } from 'k6'
import ws from 'k6/ws'
import { Trend, Counter } from 'k6/metrics'
import { getRandomInt, scenario, trends } from './common.js'
export { handleSummary } from './summary.js'
const token = __ENV.MP_TOKEN
const socketURI = __ENV.MP_URI
? __ENV.MP_URI
: 'wss://woopuegececriuknbjus.realtime-qa.abc3.dev/socket/websocket'
const URL = `${socketURI}?log_level=info&apikey=${token}`
const conns = __ENV.CONNS
const baseDuration = __ENV.DURATION ? __ENV.DURATION : 60
const duration = parseInt(baseDuration) + 15
const rooms = []
rooms.push(`room${getRandomInt(0, __ENV.ROOMS)}`)
const randomRoom = `fGwer43Fge${Math.random().toString(36).slice(2)}`
const latencyTrend = new Trend('latency_trend')
const counterReceived = new Counter('received_updates')
const to = {}
export const options = {
vus: 1,
thresholds: to,
summaryTrendStats: trends,
scenarios: {
replication: scenario(baseDuration, conns),
},
}
export default () => {
const res = ws.connect(URL, {}, (socket) => {
socket.on('open', () => {
// Join channel
socket.send(
JSON.stringify({
topic: `realtime:${randomRoom}`,
event: 'phx_join',
payload: {
config: {
broadcast: {
self: true,
},
presence: {
key: '',
},
postgres_changes: [],
},
},
ref: '1',
join_ref: '1',
})
)
rooms.map((room) =>
socket.send(
JSON.stringify({
topic: 'realtime:any',
event: 'phx_join',
payload: {
config: {
postgres_changes: [
{
event: 'INSERT',
schema: 'public',
table: 'load_messages',
// filter: `room_id=eq.${room}`,
},
],
},
},
ref: '2',
join_ref: '2',
})
)
)
socket.send(
JSON.stringify({
topic: `realtime:${randomRoom}`,
event: 'access_token',
payload: {
access_token: token,
},
ref: '3',
})
)
rooms.map((room) =>
socket.send(
JSON.stringify({
topic: `realtime:any`,
event: 'access_token',
payload: {
access_token: token,
},
ref: '4',
})
)
)
socket.setInterval(() => {
// Send heartbeat to server (timeout is probably around 1m)
socket.send(
JSON.stringify({
topic: 'phoenix',
event: 'heartbeat',
payload: {},
ref: 0,
})
)
}, 30 * 1000)
})
socket.on('message', (msg) => {
const now = Date.now()
// console.log('----------------')
// console.log(msg)
// console.log('----------------')
msg = JSON.parse(msg)
if (msg.event === 'system') {
check(msg, {
'subscribed to realtime': (msg) =>
msg.topic === 'realtime:any' && msg.payload.status === 'ok',
})
}
if (msg.event !== 'postgres_changes') {
return
}
const type = msg.payload.type
let updated = 0
if (msg.payload.data.record) {
updated = Date.parse(msg.payload.data.record.created_at)
} else {
updated = new Date(msg.payload.data.commit_timestamp)
}
latencyTrend.add(now - updated, { type: type })
counterReceived.add(1)
check(msg, {
'got realtime notification': (msg) => msg.topic === 'realtime:any',
})
})
socket.on('error', (e) => {
if (e.error() != 'websocket: close sent') {
console.error('An unexpected error occurred: ', e.error())
}
})
socket.setTimeout(function () {
socket.close()
}, duration * 1000)
})
check(res, { 'status is 101': (r) => r && r.status === 101 })
}