Last active
April 5, 2022 22:43
-
-
Save mryhryki/423d160c1205a1e60e1d5284b5ad6dc5 to your computer and use it in GitHub Desktop.
DynamoDB の Scan 性能を測るテストスクリプト
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| node_modules | |
| package-lock.json |
DynamoDB の Scan の速度を測るテスト用スクリプトです。
$ npm run table:create
$ npm run table:delete$ npm run create_data
> [email protected] create_data
> node create_data.js
Total Count: 10000 , Time: 46179 ms
Finished$ npm run scan
> [email protected] scan
> node scan_test.js
...
Total Count: 10000 , Time: 10820 ms
Finished
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const {v4: uuid} = require('uuid') | |
| const {putData} = require('./dynamodb') | |
| const getData = () => ({uuid: uuid(), key1: uuid(), key2: uuid(), key3: uuid()}) | |
| const main = async () => { | |
| const start = new Date().getTime() | |
| let totalCount = 0; | |
| for await (const _ of Array.from({length: 100})) { | |
| await Promise.all(Array.from({length: 100}).map(() => { | |
| totalCount++; | |
| return putData(getData()); | |
| })) | |
| } | |
| console.log("Total Count:", totalCount, ", Time:", new Date().getTime() - start, "ms") | |
| } | |
| main().then(() => console.log("Finished")).catch(console.error) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const {DynamoDB} = require("aws-sdk"); | |
| const client = new DynamoDB({ | |
| apiVersion: "2012-10-08", | |
| region: "ap-northeast-1", | |
| }) | |
| const documentClient = new DynamoDB.DocumentClient({ | |
| apiVersion: "2012-10-08", | |
| region: "ap-northeast-1", | |
| }); | |
| const TableName = "scan-test"; | |
| const createTable = () => client.createTable({ | |
| TableName, | |
| AttributeDefinitions: [{ | |
| AttributeName: "uuid", | |
| AttributeType: "S", | |
| }], | |
| KeySchema: [{ | |
| AttributeName: "uuid", | |
| KeyType: "HASH", | |
| }], | |
| BillingMode: "PAY_PER_REQUEST", | |
| }).promise() | |
| const deleteTable = () => client.deleteTable({TableName}).promise() | |
| const scanData = (ExclusiveStartKey) => documentClient.scan({TableName, Limit: 100, ExclusiveStartKey}).promise(); | |
| const putData = (Item) => documentClient.put({TableName, Item}).promise(); | |
| module.exports = {createTable, deleteTable, scanData, putData} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "dynamodb-scan-test", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "index.js", | |
| "scripts": { | |
| "table:create": "node -e 'require(\"./dynamodb\").createTable()'", | |
| "table:delete": "node -e 'require(\"./dynamodb\").deleteTable()'", | |
| "create_data": "node create_data.js", | |
| "scan": "node scan_test.js" | |
| }, | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "aws-sdk": "^2.861.0", | |
| "uuid": "^8.3.2" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const {scanData} = require('./dynamodb') | |
| const main = async () => { | |
| const start = new Date().getTime() | |
| let totalCount = 0; | |
| let nextKey = undefined; | |
| do { | |
| const data = await scanData(nextKey) | |
| totalCount += data.Count | |
| nextKey = data.LastEvaluatedKey | |
| console.log(nextKey) | |
| } while (nextKey != null) | |
| console.log("Total Count:", totalCount, ", Time:", new Date().getTime() - start, "ms") | |
| } | |
| main().then(() => console.log("Finished")).catch(console.error) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment