1
0
Fork 0
mirror of synced 2024-10-05 20:44:47 +13:00

Use batches

This commit is contained in:
Adria Navarro 2023-12-21 17:39:14 +01:00
parent b034542536
commit c46509d6cb

View file

@ -7,6 +7,8 @@ const generator = new Chance()
const STUDENT_COUNT = 500 const STUDENT_COUNT = 500
const SUBJECT_COUNT = 10 const SUBJECT_COUNT = 10
const batchSize = 100
if (!process.argv[2]) { if (!process.argv[2]) {
console.error("Please specify an API key as script argument.") console.error("Please specify an API key as script argument.")
process.exit(-1) process.exit(-1)
@ -14,6 +16,31 @@ if (!process.argv[2]) {
const start = Date.now() const start = Date.now()
async function batchCreate(apiKey, appId, count, table, generator) {
let i = 0
async function createSingleRow() {
const row = await createRow(apiKey, appId, table, generator())
console.log(
`${table.name} - ${++i} of ${count} created (${
(Date.now() - start) / 1000
}s)`
)
return row
}
const rows = []
for (let j = 0; j < count; j += batchSize) {
const batchPromises = Array.from(
{ length: Math.min(batchSize, count - j) },
createSingleRow
)
const batchRows = await Promise.all(batchPromises)
rows.push(...batchRows)
}
return rows
}
async function run() { async function run() {
const apiKey = process.argv[2] const apiKey = process.argv[2]
const app = await createApp(apiKey) const app = await createApp(apiKey)
@ -26,10 +53,12 @@ async function run() {
console.log(`Table found: ${studentsTable.name}`) console.log(`Table found: ${studentsTable.name}`)
let studentNumber = studentsTable.schema["Auto ID"].lastID let studentNumber = studentsTable.schema["Auto ID"].lastID
let i = 0 const students = await batchCreate(
const students = await Promise.all( apiKey,
Array.from({ length: STUDENT_COUNT }).map(async () => { app._id,
const row = await createRow(apiKey, app._id, studentsTable, { STUDENT_COUNT,
studentsTable,
() => ({
"Student Number": (++studentNumber).toString(), "Student Number": (++studentNumber).toString(),
"First Name": generator.first(), "First Name": generator.first(),
"Last Name": generator.last(), "Last Name": generator.last(),
@ -39,14 +68,6 @@ 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(
`Student ${++i} of ${STUDENT_COUNT} created (${
(Date.now() - start) / 1000
}s)`
)
return row
})
) )
const subjectTable = await createTable(apiKey, app._id, { const subjectTable = await createTable(apiKey, app._id, {
@ -60,19 +81,14 @@ async function run() {
primaryDisplay: "Name", primaryDisplay: "Name",
}) })
i = 0 const subjects = await batchCreate(
const subjects = await Promise.all( apiKey,
Array.from({ length: SUBJECT_COUNT }).map(async () => { app._id,
const row = await createRow(apiKey, app._id, subjectTable, { SUBJECT_COUNT,
subjectTable,
() => ({
Name: generator.profession(), Name: generator.profession(),
}) })
console.log(
`Subject ${++i} of ${SUBJECT_COUNT} created (${
(Date.now() - start) / 1000
}s)`
)
return row
})
) )
const gradesTable = await createTable(apiKey, app._id, { const gradesTable = await createTable(apiKey, app._id, {