1
0
Fork 0
mirror of synced 2024-10-05 12:34:50 +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 SUBJECT_COUNT = 10
const batchSize = 100
if (!process.argv[2]) {
console.error("Please specify an API key as script argument.")
process.exit(-1)
@ -14,6 +16,31 @@ if (!process.argv[2]) {
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() {
const apiKey = process.argv[2]
const app = await createApp(apiKey)
@ -26,10 +53,12 @@ async function run() {
console.log(`Table found: ${studentsTable.name}`)
let studentNumber = studentsTable.schema["Auto ID"].lastID
let i = 0
const students = await Promise.all(
Array.from({ length: STUDENT_COUNT }).map(async () => {
const row = await createRow(apiKey, app._id, studentsTable, {
const students = await batchCreate(
apiKey,
app._id,
STUDENT_COUNT,
studentsTable,
() => ({
"Student Number": (++studentNumber).toString(),
"First Name": generator.first(),
"Last Name": generator.last(),
@ -39,14 +68,6 @@ async function run() {
"Home Number": generator.phone(),
"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, {
@ -60,19 +81,14 @@ async function run() {
primaryDisplay: "Name",
})
i = 0
const subjects = await Promise.all(
Array.from({ length: SUBJECT_COUNT }).map(async () => {
const row = await createRow(apiKey, app._id, subjectTable, {
const subjects = await batchCreate(
apiKey,
app._id,
SUBJECT_COUNT,
subjectTable,
() => ({
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, {