Run creation in parallel

This commit is contained in:
Adria Navarro 2023-12-20 15:02:21 +01:00
parent 5240b04a3a
commit 57a7be7a26
1 changed files with 16 additions and 8 deletions

View File

@ -25,12 +25,11 @@ async function run() {
} }
console.log(`Table found: ${studentsTable.name}`) console.log(`Table found: ${studentsTable.name}`)
const students = [], const studentsPromise = []
subjects = []
let studentNumber = studentsTable.schema["Auto ID"].lastID let studentNumber = studentsTable.schema["Auto ID"].lastID
for (let i = 0; i < STUDENT_COUNT; i++) { for (let i = 0; i < STUDENT_COUNT; i++) {
students.push( studentsPromise.push(
(async () => {
await createRow(apiKey, app._id, studentsTable, { await createRow(apiKey, app._id, studentsTable, {
"Student Number": (++studentNumber).toString(), "Student Number": (++studentNumber).toString(),
"First Name": generator.first(), "First Name": generator.first(),
@ -41,14 +40,18 @@ async function run() {
"Home Number": generator.phone(), "Home Number": generator.phone(),
"Attendance_(%)": generator.integer({ min: 0, max: 100 }), "Attendance_(%)": generator.integer({ min: 0, max: 100 }),
}) })
)
console.log( console.log(
`Student ${i + 1} of ${STUDENT_COUNT} created (${ `Student ${i + 1} of ${STUDENT_COUNT} created (${
(Date.now() - start) / 1000 (Date.now() - start) / 1000
}s)` }s)`
)
})()
) )
} }
const students = await Promise.all(studentsPromise)
const subjectTable = await createTable(apiKey, app._id, { const subjectTable = await createTable(apiKey, app._id, {
schema: { schema: {
Name: { Name: {
@ -60,19 +63,24 @@ async function run() {
primaryDisplay: "Name", primaryDisplay: "Name",
}) })
const subjectPromises = []
for (let i = 0; i < SUBJECT_COUNT; i++) { for (let i = 0; i < SUBJECT_COUNT; i++) {
subjects.push( subjectPromises.push(
(async () => {
await createRow(apiKey, app._id, subjectTable, { await createRow(apiKey, app._id, subjectTable, {
Name: generator.profession(), Name: generator.profession(),
}) })
)
console.log( console.log(
`Subject ${i + 1} of ${SUBJECT_COUNT} created (${ `Subject ${i + 1} of ${SUBJECT_COUNT} created (${
(Date.now() - start) / 1000 (Date.now() - start) / 1000
}s)` }s)`
)
})()
) )
} }
const subjects = await Promise.all(subjectPromises)
const gradesTable = await createTable(apiKey, app._id, { const gradesTable = await createTable(apiKey, app._id, {
schema: { schema: {
Score: { Score: {
@ -124,7 +132,7 @@ async function run() {
run() run()
.then(() => { .then(() => {
console.log(`Done in(${(Date.now() - start) / 1000} seconds`) console.log(`Done in ${(Date.now() - start) / 1000} seconds`)
}) })
.catch(err => { .catch(err => {
console.error(err) console.error(err)