Skip to content

Commit

Permalink
feat: add query timing
Browse files Browse the repository at this point in the history
  • Loading branch information
solaris007 committed Nov 28, 2023
1 parent f98203d commit 0a303da
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
10 changes: 10 additions & 0 deletions packages/spacecat-shared-dynamo/src/modules/getItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* governing permissions and limitations under the License.
*/

import { performance } from 'perf_hooks';

/**
* Retrieves an item from DynamoDB using a table name and key object.
*
Expand All @@ -35,7 +37,15 @@ async function getItem(docClient, tableName, key, log = console) {
};

try {
const startTime = performance.now();

const data = await docClient.get(params);

const endTime = performance.now();
const duration = endTime - startTime;

log.info(`GetItem execution time: ${duration.toFixed(2)} ms for query: ${JSON.stringify(params)}`);

return data.Item;
} catch (error) {
log.error('DB Get Item Error:', error);
Expand Down
10 changes: 10 additions & 0 deletions packages/spacecat-shared-dynamo/src/modules/putItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* governing permissions and limitations under the License.
*/

import { performance } from 'perf_hooks';

/**
* Inserts or updates an item in a DynamoDB table.
*
Expand All @@ -31,7 +33,15 @@ async function putItem(docClient, tableName, item, log = console) {
};

try {
const startTime = performance.now();

await docClient.put(params);

const endTime = performance.now();
const duration = endTime - startTime;

log.info(`PutItem execution time: ${duration.toFixed(2)} ms for query: ${JSON.stringify(params)}`);

return { message: 'Item inserted/updated successfully.' };
} catch (error) {
log.error('DB Put Item Error:', error);
Expand Down
24 changes: 24 additions & 0 deletions packages/spacecat-shared-dynamo/src/modules/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* governing permissions and limitations under the License.
*/

import { performance } from 'perf_hooks';

/**
* Queries DynamoDB and automatically handles pagination to retrieve all items.
*
Expand All @@ -23,18 +25,40 @@ async function query(docClient, originalParams, log = console) {
let items = [];
const params = { ...originalParams };

let totalTime = 0;
let paginationCount = 0;

try {
let data;
do {
const startTime = performance.now();

/*
This is one of the scenarios where it's appropriate to disable
the ESLint rule for this specific case.
In this case, it's necessary because each query depends on the
result of the previous one (to get the LastEvaluatedKey).
*/
// eslint-disable-next-line no-await-in-loop
data = await docClient.query(params);

const endTime = performance.now(); // End timing
const duration = endTime - startTime;
totalTime += duration;
paginationCount += 1;

log.info(`Pagination ${paginationCount} query time: ${duration.toFixed(2)} ms`);

items = items.concat(data.Items);
params.ExclusiveStartKey = data.LastEvaluatedKey;
} while (data.LastEvaluatedKey);
} catch (error) {
log.error('DB Query Error:', error);
throw error;
}

log.info(`Total query time: ${totalTime.toFixed(2)} ms with ${paginationCount} paginations for query: ${JSON.stringify(params)}`);

return items;
}

Expand Down
10 changes: 10 additions & 0 deletions packages/spacecat-shared-dynamo/src/modules/removeItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
* governing permissions and limitations under the License.
*/

import { performance } from 'perf_hooks';

/**
* Removes an item from a DynamoDB table.
*
Expand All @@ -35,7 +37,15 @@ async function removeItem(docClient, tableName, key, log = console) {
};

try {
const startTime = performance.now();

await docClient.delete(params);

const endTime = performance.now();
const duration = endTime - startTime;

log.info(`RemoveItem execution time: ${duration.toFixed(2)} ms for query: ${JSON.stringify(params)}`);

return { message: 'Item removed successfully.' };
} catch (error) {
log.error('DB Remove Item Error:', error);
Expand Down

0 comments on commit 0a303da

Please sign in to comment.