overleaf/services/clsi/test/load/js/loadTest.js

102 lines
2.8 KiB
JavaScript
Raw Normal View History

2020-02-25 11:15:31 -05:00
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const request = require('request')
const Settings = require('@overleaf/settings')
2020-02-25 11:15:31 -05:00
const async = require('async')
const fs = require('node:fs')
2020-06-15 04:52:21 -04:00
const _ = require('lodash')
2020-02-25 11:15:31 -05:00
const concurentCompiles = 5
const totalCompiles = 50
2021-07-13 07:04:48 -04:00
const buildUrl = path =>
2020-02-25 11:15:31 -05:00
`http://${Settings.internal.clsi.host}:${Settings.internal.clsi.port}/${path}`
const mainTexContent = fs.readFileSync('./bulk.tex', 'utf-8')
const compileTimes = []
let failedCount = 0
2020-08-10 12:01:11 -04:00
const getAverageCompileTime = function () {
2020-02-25 11:15:31 -05:00
const totalTime = _.reduce(compileTimes, (sum, time) => sum + time, 0)
return totalTime / compileTimes.length
}
2020-08-10 12:01:11 -04:00
const makeRequest = function (compileNumber, callback) {
2020-02-25 11:15:31 -05:00
let bulkBodyCount = 7
let bodyContent = ''
while (--bulkBodyCount) {
bodyContent = bodyContent += mainTexContent
}
const startTime = new Date()
return request.post(
{
url: buildUrl(`project/loadcompile-${compileNumber}/compile`),
json: {
compile: {
resources: [
{
path: 'main.tex',
content: `\
\\documentclass{article}
\\begin{document}
${bodyContent}
\\end{document}\
2021-07-13 07:04:48 -04:00
`,
},
],
},
},
2020-02-25 11:15:31 -05:00
},
(err, response, body) => {
if (err != null) {
2020-02-25 11:15:31 -05:00
failedCount++
return callback(new Error(`compile ${compileNumber} failed`))
2020-02-25 11:15:31 -05:00
}
if (response.statusCode !== 200) {
2020-02-25 11:15:31 -05:00
failedCount++
return callback(new Error(`compile ${compileNumber} failed`))
2020-02-25 11:15:31 -05:00
}
const totalTime = new Date() - startTime
console.log(totalTime + 'ms')
compileTimes.push(totalTime)
return callback(err)
}
)
}
2021-07-13 07:04:48 -04:00
const jobs = _.map(
__range__(1, totalCompiles, true),
i => cb => makeRequest(i, cb)
2020-02-25 11:15:31 -05:00
)
const startTime = new Date()
2021-07-13 07:04:48 -04:00
async.parallelLimit(jobs, concurentCompiles, err => {
2020-02-25 11:15:31 -05:00
if (err != null) {
console.error(err)
}
console.log(`total time taken = ${(new Date() - startTime) / 1000}s`)
console.log(`total compiles = ${totalCompiles}`)
console.log(`concurent compiles = ${concurentCompiles}`)
console.log(`average time = ${getAverageCompileTime() / 1000}s`)
console.log(`max time = ${_.max(compileTimes) / 1000}s`)
console.log(`min time = ${_.min(compileTimes) / 1000}s`)
return console.log(`total failures = ${failedCount}`)
})
function __range__(left, right, inclusive) {
const range = []
const ascending = left < right
const end = !inclusive ? right : ascending ? right + 1 : right - 1
for (let i = left; ascending ? i < end : i > end; ascending ? i++ : i--) {
range.push(i)
}
return range
}