2020-05-06 06:12:57 -04:00
|
|
|
/* eslint-disable
|
|
|
|
no-return-assign,
|
|
|
|
no-unused-vars,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-05-06 06:12:55 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS101: Remove unnecessary use of Array.from
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* DS202: Simplify dynamic range loops
|
|
|
|
* DS205: Consider reworking code to avoid use of IIFEs
|
|
|
|
* DS207: Consider shorter variations of null checks
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
2021-05-25 11:56:57 -04:00
|
|
|
const DocUpdaterClient = require('../../acceptance/js/helpers/DocUpdaterClient')
|
2020-05-06 06:12:55 -04:00
|
|
|
// MockWebApi = require "../../acceptance/js/helpers/MockWebApi"
|
2024-11-08 05:21:56 -05:00
|
|
|
const assert = require('node:assert')
|
2020-05-06 06:13:07 -04:00
|
|
|
const async = require('async')
|
2016-06-17 07:17:43 -04:00
|
|
|
|
2020-05-06 06:13:07 -04:00
|
|
|
const insert = function (string, pos, content) {
|
|
|
|
const result = string.slice(0, pos) + content + string.slice(pos)
|
|
|
|
return result
|
|
|
|
}
|
2016-06-17 07:17:43 -04:00
|
|
|
|
2020-05-06 06:13:07 -04:00
|
|
|
const transform = function (op1, op2) {
|
|
|
|
if (op2.p < op1.p) {
|
|
|
|
return {
|
|
|
|
p: op1.p + op2.i.length,
|
2021-07-13 07:04:42 -04:00
|
|
|
i: op1.i,
|
2020-05-06 06:13:07 -04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return op1
|
|
|
|
}
|
|
|
|
}
|
2016-06-17 07:17:43 -04:00
|
|
|
|
2020-05-06 06:12:55 -04:00
|
|
|
class StressTestClient {
|
2020-05-06 06:13:07 -04:00
|
|
|
constructor(options) {
|
|
|
|
if (options == null) {
|
|
|
|
options = {}
|
|
|
|
}
|
|
|
|
this.options = options
|
|
|
|
if (this.options.updateDelay == null) {
|
|
|
|
this.options.updateDelay = 200
|
|
|
|
}
|
|
|
|
this.project_id = this.options.project_id || DocUpdaterClient.randomId()
|
|
|
|
this.doc_id = this.options.doc_id || DocUpdaterClient.randomId()
|
|
|
|
this.pos = this.options.pos || 0
|
|
|
|
this.content = this.options.content || ''
|
2016-06-17 07:17:43 -04:00
|
|
|
|
2020-05-06 06:13:07 -04:00
|
|
|
this.client_id = DocUpdaterClient.randomId()
|
|
|
|
this.version = this.options.version || 0
|
|
|
|
this.inflight_op = null
|
|
|
|
this.charCode = 0
|
2016-06-21 09:31:20 -04:00
|
|
|
|
2020-05-06 06:13:07 -04:00
|
|
|
this.counts = {
|
|
|
|
conflicts: 0,
|
|
|
|
local_updates: 0,
|
|
|
|
remote_updates: 0,
|
2021-07-13 07:04:42 -04:00
|
|
|
max_delay: 0,
|
2020-05-06 06:13:07 -04:00
|
|
|
}
|
2016-06-17 07:17:43 -04:00
|
|
|
|
2020-05-06 06:13:07 -04:00
|
|
|
DocUpdaterClient.subscribeToAppliedOps((channel, update) => {
|
|
|
|
update = JSON.parse(update)
|
|
|
|
if (update.error != null) {
|
|
|
|
console.error(new Error(`Error from server: '${update.error}'`))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (update.doc_id === this.doc_id) {
|
|
|
|
return this.processReply(update)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2016-06-21 09:31:20 -04:00
|
|
|
|
2020-05-06 06:13:07 -04:00
|
|
|
sendUpdate() {
|
|
|
|
const data = String.fromCharCode(65 + (this.charCode++ % 26))
|
|
|
|
this.content = insert(this.content, this.pos, data)
|
|
|
|
this.inflight_op = {
|
|
|
|
i: data,
|
2021-07-13 07:04:42 -04:00
|
|
|
p: this.pos++,
|
2020-05-06 06:13:07 -04:00
|
|
|
}
|
|
|
|
this.resendUpdate()
|
|
|
|
return (this.inflight_op_sent = Date.now())
|
|
|
|
}
|
|
|
|
|
|
|
|
resendUpdate() {
|
|
|
|
assert(this.inflight_op != null)
|
|
|
|
DocUpdaterClient.sendUpdate(this.project_id, this.doc_id, {
|
|
|
|
doc: this.doc_id,
|
|
|
|
op: [this.inflight_op],
|
|
|
|
v: this.version,
|
|
|
|
meta: {
|
2021-07-13 07:04:42 -04:00
|
|
|
source: this.client_id,
|
2020-05-06 06:13:07 -04:00
|
|
|
},
|
2021-07-13 07:04:42 -04:00
|
|
|
dupIfSource: [this.client_id],
|
2020-05-06 06:13:07 -04:00
|
|
|
})
|
|
|
|
return (this.update_timer = setTimeout(() => {
|
|
|
|
console.log(
|
|
|
|
`[${new Date()}] \t[${this.client_id.slice(
|
|
|
|
0,
|
|
|
|
4
|
|
|
|
)}] WARN: Resending update after 5 seconds`
|
|
|
|
)
|
|
|
|
return this.resendUpdate()
|
|
|
|
}, 5000))
|
|
|
|
}
|
2016-06-21 09:31:20 -04:00
|
|
|
|
2020-05-06 06:13:07 -04:00
|
|
|
processReply(update) {
|
|
|
|
if (update.op.v !== this.version) {
|
|
|
|
if (update.op.v < this.version) {
|
|
|
|
console.log(
|
|
|
|
`[${new Date()}] \t[${this.client_id.slice(
|
|
|
|
0,
|
|
|
|
4
|
|
|
|
)}] WARN: Duplicate ack (already seen version)`
|
|
|
|
)
|
|
|
|
return
|
|
|
|
} else {
|
|
|
|
console.error(
|
|
|
|
`[${new Date()}] \t[${this.client_id.slice(
|
|
|
|
0,
|
|
|
|
4
|
|
|
|
)}] ERROR: Version jumped ahead (client: ${this.version}, op: ${
|
|
|
|
update.op.v
|
|
|
|
})`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.version++
|
|
|
|
if (update.op.meta.source === this.client_id) {
|
|
|
|
if (this.inflight_op != null) {
|
|
|
|
this.counts.local_updates++
|
|
|
|
this.inflight_op = null
|
|
|
|
clearTimeout(this.update_timer)
|
|
|
|
const delay = Date.now() - this.inflight_op_sent
|
|
|
|
this.counts.max_delay = Math.max(this.counts.max_delay, delay)
|
|
|
|
return this.continue()
|
|
|
|
} else {
|
|
|
|
return console.log(
|
|
|
|
`[${new Date()}] \t[${this.client_id.slice(
|
|
|
|
0,
|
|
|
|
4
|
|
|
|
)}] WARN: Duplicate ack`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(update.op.op.length === 1)
|
|
|
|
this.counts.remote_updates++
|
2023-03-21 08:06:13 -04:00
|
|
|
let externalOp = update.op.op[0]
|
2020-05-06 06:13:07 -04:00
|
|
|
if (this.inflight_op != null) {
|
|
|
|
this.counts.conflicts++
|
2023-03-21 08:06:13 -04:00
|
|
|
this.inflight_op = transform(this.inflight_op, externalOp)
|
|
|
|
externalOp = transform(externalOp, this.inflight_op)
|
2020-05-06 06:13:07 -04:00
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
if (externalOp.p < this.pos) {
|
|
|
|
this.pos += externalOp.i.length
|
2020-05-06 06:13:07 -04:00
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
return (this.content = insert(this.content, externalOp.p, externalOp.i))
|
2020-05-06 06:13:07 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-17 07:17:43 -04:00
|
|
|
|
2020-05-06 06:13:07 -04:00
|
|
|
continue() {
|
|
|
|
if (this.updateCount > 0) {
|
|
|
|
this.updateCount--
|
2024-03-25 06:51:40 -04:00
|
|
|
return setTimeout(
|
|
|
|
() => {
|
|
|
|
return this.sendUpdate()
|
|
|
|
},
|
|
|
|
this.options.updateDelay * (0.5 + Math.random())
|
|
|
|
)
|
2020-05-06 06:13:07 -04:00
|
|
|
} else {
|
|
|
|
return this.updateCallback()
|
|
|
|
}
|
|
|
|
}
|
2016-06-17 07:17:43 -04:00
|
|
|
|
2020-05-06 06:13:07 -04:00
|
|
|
runForNUpdates(n, callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:13:07 -04:00
|
|
|
}
|
|
|
|
this.updateCallback = callback
|
|
|
|
this.updateCount = n
|
|
|
|
return this.continue()
|
|
|
|
}
|
2016-06-17 07:17:43 -04:00
|
|
|
|
2020-05-06 06:13:07 -04:00
|
|
|
check(callback) {
|
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:13:07 -04:00
|
|
|
}
|
|
|
|
return DocUpdaterClient.getDoc(
|
|
|
|
this.project_id,
|
|
|
|
this.doc_id,
|
|
|
|
(error, res, body) => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
if (body.lines == null) {
|
|
|
|
return console.error(
|
|
|
|
`[${new Date()}] \t[${this.client_id.slice(
|
|
|
|
0,
|
|
|
|
4
|
2023-03-21 08:06:13 -04:00
|
|
|
)}] ERROR: Invalid response from get doc (${this.doc_id})`,
|
2020-05-06 06:13:07 -04:00
|
|
|
body
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const content = body.lines.join('\n')
|
|
|
|
const { version } = body
|
|
|
|
if (content !== this.content) {
|
|
|
|
if (version === this.version) {
|
|
|
|
console.error(
|
|
|
|
`[${new Date()}] \t[${this.client_id.slice(
|
|
|
|
0,
|
|
|
|
4
|
|
|
|
)}] Error: Client content does not match server.`
|
|
|
|
)
|
|
|
|
console.error(`Server: ${content.split('a')}`)
|
|
|
|
console.error(`Client: ${this.content.split('a')}`)
|
|
|
|
} else {
|
|
|
|
console.error(
|
|
|
|
`[${new Date()}] \t[${this.client_id.slice(
|
|
|
|
0,
|
|
|
|
4
|
|
|
|
)}] Error: Version mismatch (Server: '${version}', Client: '${
|
|
|
|
this.version
|
|
|
|
}')`
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.isContentValid(this.content)) {
|
|
|
|
const iterable = this.content.split('')
|
|
|
|
for (let i = 0; i < iterable.length; i++) {
|
|
|
|
const chunk = iterable[i]
|
|
|
|
if (chunk != null && chunk !== 'a') {
|
|
|
|
console.log(chunk, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
throw new Error('bad content')
|
|
|
|
}
|
|
|
|
return callback()
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
isChunkValid(chunk) {
|
|
|
|
const char = 0
|
|
|
|
for (let i = 0; i < chunk.length; i++) {
|
|
|
|
const letter = chunk[i]
|
|
|
|
if (letter.charCodeAt(0) !== 65 + (i % 26)) {
|
|
|
|
console.error(
|
|
|
|
`[${new Date()}] \t[${this.client_id.slice(0, 4)}] Invalid Chunk:`,
|
|
|
|
chunk
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
isContentValid(content) {
|
|
|
|
for (const chunk of Array.from(content.split('a'))) {
|
|
|
|
if (chunk != null && chunk !== '') {
|
|
|
|
if (!this.isChunkValid(chunk)) {
|
|
|
|
console.error(
|
|
|
|
`[${new Date()}] \t[${this.client_id.slice(0, 4)}] Invalid content`,
|
|
|
|
content
|
|
|
|
)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
const checkDocument = function (projectId, docId, clients, callback) {
|
2020-05-06 06:13:07 -04:00
|
|
|
if (callback == null) {
|
2021-10-27 05:49:18 -04:00
|
|
|
callback = function () {}
|
2020-05-06 06:13:07 -04:00
|
|
|
}
|
2021-07-13 07:04:42 -04:00
|
|
|
const jobs = clients.map(client => cb => client.check(cb))
|
2020-05-06 06:13:07 -04:00
|
|
|
return async.parallel(jobs, callback)
|
|
|
|
}
|
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
const printSummary = function (docId, clients) {
|
2020-05-06 06:13:07 -04:00
|
|
|
const slot = require('cluster-key-slot')
|
|
|
|
const now = new Date()
|
|
|
|
console.log(
|
2023-03-21 08:06:13 -04:00
|
|
|
`[${now}] [${docId.slice(0, 4)} (slot: ${slot(docId)})] ${
|
2020-05-06 06:13:07 -04:00
|
|
|
clients.length
|
|
|
|
} clients...`
|
|
|
|
)
|
|
|
|
return (() => {
|
|
|
|
const result = []
|
|
|
|
for (const client of Array.from(clients)) {
|
|
|
|
console.log(
|
|
|
|
`[${now}] \t[${client.client_id.slice(0, 4)}] { local: ${
|
|
|
|
client.counts.local_updates
|
|
|
|
}, remote: ${client.counts.remote_updates}, conflicts: ${
|
|
|
|
client.counts.conflicts
|
|
|
|
}, max_delay: ${client.counts.max_delay} }`
|
|
|
|
)
|
|
|
|
result.push(
|
|
|
|
(client.counts = {
|
|
|
|
local_updates: 0,
|
|
|
|
remote_updates: 0,
|
|
|
|
conflicts: 0,
|
2021-07-13 07:04:42 -04:00
|
|
|
max_delay: 0,
|
2020-05-06 06:13:07 -04:00
|
|
|
})
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
})()
|
|
|
|
}
|
|
|
|
|
|
|
|
const CLIENT_COUNT = parseInt(process.argv[2], 10)
|
|
|
|
const UPDATE_DELAY = parseInt(process.argv[3], 10)
|
|
|
|
const SAMPLE_INTERVAL = parseInt(process.argv[4], 10)
|
2016-06-17 07:17:43 -04:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
for (const docAndProjectId of Array.from(process.argv.slice(5))) {
|
|
|
|
;(function (docAndProjectId) {
|
|
|
|
const [projectId, docId] = Array.from(docAndProjectId.split(':'))
|
|
|
|
console.log({ projectId, docId })
|
2020-05-06 06:13:07 -04:00
|
|
|
return DocUpdaterClient.setDocLines(
|
2023-03-21 08:06:13 -04:00
|
|
|
projectId,
|
|
|
|
docId,
|
2020-05-06 06:13:07 -04:00
|
|
|
[new Array(CLIENT_COUNT + 2).join('a')],
|
|
|
|
null,
|
|
|
|
null,
|
2021-07-13 07:04:42 -04:00
|
|
|
error => {
|
2020-05-06 06:13:07 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
return DocUpdaterClient.getDoc(projectId, docId, (error, res, body) => {
|
|
|
|
let runBatch
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
if (body.lines == null) {
|
|
|
|
return console.error(
|
|
|
|
`[${new Date()}] ERROR: Invalid response from get doc (${docId})`,
|
|
|
|
body
|
|
|
|
)
|
|
|
|
}
|
|
|
|
const content = body.lines.join('\n')
|
|
|
|
const { version } = body
|
2020-05-06 06:13:07 -04:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
const clients = []
|
|
|
|
for (
|
|
|
|
let pos = 1, end = CLIENT_COUNT, asc = end >= 1;
|
|
|
|
asc ? pos <= end : pos >= end;
|
|
|
|
asc ? pos++ : pos--
|
|
|
|
) {
|
|
|
|
;(function (pos) {
|
|
|
|
const client = new StressTestClient({
|
|
|
|
doc_id: docId,
|
|
|
|
project_id: projectId,
|
|
|
|
content,
|
|
|
|
pos,
|
|
|
|
version,
|
|
|
|
updateDelay: UPDATE_DELAY,
|
|
|
|
})
|
|
|
|
return clients.push(client)
|
|
|
|
})(pos)
|
|
|
|
}
|
2020-05-06 06:13:07 -04:00
|
|
|
|
2023-03-21 08:06:13 -04:00
|
|
|
return (runBatch = function () {
|
|
|
|
const jobs = clients.map(
|
|
|
|
client => cb =>
|
|
|
|
client.runForNUpdates(SAMPLE_INTERVAL / UPDATE_DELAY, cb)
|
|
|
|
)
|
|
|
|
return async.parallel(jobs, error => {
|
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
printSummary(docId, clients)
|
|
|
|
return checkDocument(projectId, docId, clients, error => {
|
2020-05-06 06:13:07 -04:00
|
|
|
if (error != null) {
|
|
|
|
throw error
|
|
|
|
}
|
2023-03-21 08:06:13 -04:00
|
|
|
return runBatch()
|
2020-05-06 06:13:07 -04:00
|
|
|
})
|
2023-03-21 08:06:13 -04:00
|
|
|
})
|
|
|
|
})()
|
|
|
|
})
|
2020-05-06 06:13:07 -04:00
|
|
|
}
|
|
|
|
)
|
2023-03-21 08:06:13 -04:00
|
|
|
})(docAndProjectId)
|
2020-05-06 06:12:55 -04:00
|
|
|
}
|