mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-12 13:13:45 +01:00
Merge pull request #28779 from overleaf/msm-clsi-loadtest-async-await
[clsi] Replaced callbacks with async/await in `loadTests` GitOrigin-RevId: 81e84dd77f71560f765625dfdbeafcf14312a3ff
This commit is contained in:
@@ -1,12 +1,4 @@
|
||||
// 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 { fetchNothing } = require('@overleaf/fetch-utils')
|
||||
const Settings = require('@overleaf/settings')
|
||||
const async = require('async')
|
||||
const fs = require('node:fs')
|
||||
@@ -27,7 +19,7 @@ const getAverageCompileTime = function () {
|
||||
return totalTime / compileTimes.length
|
||||
}
|
||||
|
||||
const makeRequest = function (compileNumber, callback) {
|
||||
const makeRequest = async function (compileNumber) {
|
||||
let bulkBodyCount = 7
|
||||
let bodyContent = ''
|
||||
while (--bulkBodyCount) {
|
||||
@@ -35,67 +27,64 @@ const makeRequest = function (compileNumber, callback) {
|
||||
}
|
||||
|
||||
const startTime = new Date()
|
||||
return request.post(
|
||||
{
|
||||
url: buildUrl(`project/loadcompile-${compileNumber}/compile`),
|
||||
json: {
|
||||
compile: {
|
||||
resources: [
|
||||
{
|
||||
path: 'main.tex',
|
||||
content: `\
|
||||
|
||||
try {
|
||||
await fetchNothing(
|
||||
buildUrl(`project/loadcompile-${compileNumber}/compile`),
|
||||
{
|
||||
method: 'POST',
|
||||
json: {
|
||||
compile: {
|
||||
resources: [
|
||||
{
|
||||
path: 'main.tex',
|
||||
content: `\
|
||||
\\documentclass{article}
|
||||
\\begin{document}
|
||||
${bodyContent}
|
||||
\\end{document}\
|
||||
`,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
(err, response, body) => {
|
||||
if (err != null) {
|
||||
failedCount++
|
||||
return callback(new Error(`compile ${compileNumber} failed`))
|
||||
}
|
||||
if (response.statusCode !== 200) {
|
||||
failedCount++
|
||||
return callback(new Error(`compile ${compileNumber} failed`))
|
||||
}
|
||||
const totalTime = new Date() - startTime
|
||||
console.log(totalTime + 'ms')
|
||||
compileTimes.push(totalTime)
|
||||
return callback(err)
|
||||
}
|
||||
)
|
||||
)
|
||||
const totalTime = new Date() - startTime
|
||||
console.log(totalTime + 'ms')
|
||||
compileTimes.push(totalTime)
|
||||
} catch (error) {
|
||||
console.log({ error })
|
||||
failedCount++
|
||||
throw new Error(`compile ${compileNumber} failed`)
|
||||
}
|
||||
}
|
||||
|
||||
const jobs = _.map(
|
||||
__range__(1, totalCompiles, true),
|
||||
i => cb => makeRequest(i, cb)
|
||||
)
|
||||
const jobs = []
|
||||
for (let i = 0; i < totalCompiles; i++) {
|
||||
jobs.push(() => makeRequest(i))
|
||||
}
|
||||
|
||||
const startTime = new Date()
|
||||
async.parallelLimit(jobs, concurentCompiles, err => {
|
||||
if (err != null) {
|
||||
console.error(err)
|
||||
}
|
||||
const runJob = (job, _, cb) =>
|
||||
job()
|
||||
.then(() => cb())
|
||||
.catch(err => cb(err))
|
||||
|
||||
async function run() {
|
||||
const startTime = new Date()
|
||||
await async.eachOfLimit(jobs, concurentCompiles, runJob)
|
||||
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
|
||||
console.log(`total failures = ${failedCount}`)
|
||||
}
|
||||
|
||||
run()
|
||||
.then(() => process.exit(0))
|
||||
.catch(error => {
|
||||
console.error(error)
|
||||
process.exit(1)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user