Skip to content

Instantly share code, notes, and snippets.

@mryhryki
Last active April 5, 2022 22:43
Show Gist options
  • Save mryhryki/423d160c1205a1e60e1d5284b5ad6dc5 to your computer and use it in GitHub Desktop.
Save mryhryki/423d160c1205a1e60e1d5284b5ad6dc5 to your computer and use it in GitHub Desktop.
DynamoDB の Scan 性能を測るテストスクリプト

DynamoDB の Scan 性能を測るテストスクリプト

node_modules
package-lock.json

DynamoDB Scan Test

DynamoDB の Scan の速度を測るテスト用スクリプトです。

Create / Delete Table

$ npm run table:create
$ npm run table:delete

Create Data to test table

$ npm run create_data

> [email protected] create_data
> node create_data.js

Total Count: 10000 , Time: 46179 ms
Finished

Test Scan time

$ npm run scan

> [email protected] scan
> node scan_test.js

...

Total Count: 10000 , Time: 10820 ms
Finished
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)
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}
{
"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"
}
}
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