Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: performance stress test for INSERT and UPSERT #762

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 113 additions & 0 deletions test/scenarios/bookshop/insert-performance.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
const { Readable } = require('stream')

const cds = require('../../cds.js')
const bookshop = cds.utils.path.resolve(__dirname, '../../bookshop')

const vacuum = true

describe('Bookshop - Stream Performance', () => {
cds.test(bookshop)

beforeAll(async () => {
cds.db.pools._factory.options = {
max: 50,
min: 0,
acquireTimeoutMillis: undefined, // Disabled to allow the database pool to queue the INSERT statements
idleTimeoutMillis: 7500,
softIdleTimeoutMillis: 7500
}
await cds.disconnect()
})

const rawData = (start, end) => {
const gen = function* () {
let i = start
yield `[{"ID":${i++},"title":"${i}","author_ID":101,"genre_ID":11}`
for (; i < end; i++) {
yield `,{"ID":${i},"title":"${i}","author_ID":101,"genre_ID":11}`
}
yield ']'
}
return Readable.from(gen(), { objectMode: false })
}
const objectData = (start, end) => {
return (function* () {
let i = start
for (; i < end; i++) {
yield {
ID: i,
title: `${i}`,
author_ID: 101,
genre_ID: 11
}
}
})()
}
const objectDataArray = (start, end) => ([...objectData(start, end)])

const getData = objectDataArray || objectData || rawData

const tests = [
{
method: 'INSERT',
getData,
connections: 50,
},
{
method: 'INSERT',
getData,
chunkSize: 5000,
},
{
method: 'UPSERT',
getData,
connections: 50,
},
{
method: 'UPSERT',
getData,
chunkSize: 5000,
},
]

const massData = async function ({ method, getData, connections, chunkSize }, { rows }) {
const { Books } = cds.entities('sap.capire.bookshop')

const step = chunkSize || Math.ceil(rows / connections)
const proms = new Array(
chunkSize
? Math.ceil(rows / chunkSize)
: connections
)
.fill('')
.map(async (_, i) => cds.tx(async () => cds.ql[method](getData(step * i, step * (i + 1))).into(Books)))
const errors = await Promise.allSettled(proms)
const result = await SELECT.one`count(*)`.from(Books)
console.log('method:', method, 'errors:', errors.map(r => r.reason).filter(a => a), 'count:', result.count)

Check warning on line 86 in test/scenarios/bookshop/insert-performance.test.js

View workflow job for this annotation

GitHub Actions / Node.js 18

Unexpected console statement

Check warning on line 86 in test/scenarios/bookshop/insert-performance.test.js

View workflow job for this annotation

GitHub Actions / HANA Node.js 18

Unexpected console statement
}

beforeEach(async () => {
const { Books } = cds.entities('sap.capire.bookshop')
await DELETE.from(Books).where([{ val: 1 }])

if (vacuum) {
await cds.tx(async tx => {
await tx.run('COMMIT')
// VACUUM has to be called outside of any transaction
await tx.run("VACUUM (FULL)")
})
}
}, 2 * 60 * 1000)

describe.each(tests)('mass $method', (config) => {
const sizes = [
{ rows: 1 << 8 },
{ rows: 1 << 16 },
{ rows: 1 << 20 },
{ rows: 1 << 22 },
// { rows: 1 << 24 },
]

test.each(sizes)('rows: ~$rows', massData.bind(null, config), 5 * 60 * 1000)
})
})
Loading