diff --git a/packages/spacecat-shared-dynamo/src/modules/getItem.js b/packages/spacecat-shared-dynamo/src/modules/getItem.js index 4b017ff1..eaa64a3f 100644 --- a/packages/spacecat-shared-dynamo/src/modules/getItem.js +++ b/packages/spacecat-shared-dynamo/src/modules/getItem.js @@ -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. * @@ -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); diff --git a/packages/spacecat-shared-dynamo/src/modules/putItem.js b/packages/spacecat-shared-dynamo/src/modules/putItem.js index 632abce1..39fb28ea 100644 --- a/packages/spacecat-shared-dynamo/src/modules/putItem.js +++ b/packages/spacecat-shared-dynamo/src/modules/putItem.js @@ -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. * @@ -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); diff --git a/packages/spacecat-shared-dynamo/src/modules/query.js b/packages/spacecat-shared-dynamo/src/modules/query.js index b3ed9aa6..0bec8a76 100644 --- a/packages/spacecat-shared-dynamo/src/modules/query.js +++ b/packages/spacecat-shared-dynamo/src/modules/query.js @@ -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. * @@ -23,11 +25,30 @@ 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); @@ -35,6 +56,9 @@ async function query(docClient, originalParams, log = console) { 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; } diff --git a/packages/spacecat-shared-dynamo/src/modules/removeItem.js b/packages/spacecat-shared-dynamo/src/modules/removeItem.js index 53b78596..a89a4ace 100644 --- a/packages/spacecat-shared-dynamo/src/modules/removeItem.js +++ b/packages/spacecat-shared-dynamo/src/modules/removeItem.js @@ -10,6 +10,8 @@ * governing permissions and limitations under the License. */ +import { performance } from 'perf_hooks'; + /** * Removes an item from a DynamoDB table. * @@ -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);