Skip to content

Instantly share code, notes, and snippets.

@rsynnott
Last active April 24, 2016 22:38
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save rsynnott/d6d68eedcc95e163f169bc29535750b6 to your computer and use it in GitHub Desktop.
How many capacity units does a dynamodb scan use
def scanSizeTest(client: AmazonDynamoDBClient, tableName: String) = {
val recordCount = 100
var recordSize = 10
populateTable(client, tableName, recordCount, recordSize)
print(s"For batchGet of $recordCount count of $recordSize byte records (${recordCount * recordSize} bytes), used ${batchGetTable(client, tableName, recordCount)} units\n")
print(s"For scan of $recordCount count of $recordSize byte records (${recordCount * recordSize} bytes), used ${scanTable(client, tableName)} units\n")
recordSize = 100
populateTable(client, tableName, recordCount, recordSize)
print(s"For scan of $recordCount count of $recordSize byte records (${recordCount * recordSize} bytes), used ${scanTable(client, tableName)} units\n")
recordSize = 1000
populateTable(client, tableName, recordCount, recordSize)
print(s"For scan of $recordCount count of $recordSize byte records (${recordCount * recordSize} bytes), used ${scanTable(client, tableName)} units\n")
}
def batchGetTable(client: AmazonDynamoDBClient, tableName: String, recordCount: Int) = {
// Eventually consistent reads; make sure everything is in place
Thread.sleep(10000)
val keys = (0 until recordCount).map { i => Map("k" -> new AttributeValue().withS(i.toString)).asJava }.asJavaCollection
val ka = new KeysAndAttributes().withKeys(keys)
val res = client.batchGetItem(new BatchGetItemRequest()
.withRequestItems(Map(tableName -> ka))
.withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL))
res.getConsumedCapacity
}
def scanTable(client: AmazonDynamoDBClient, tableName: String) = {
// Scans are eventually consistent; make sure everything is in place
Thread.sleep(10000)
val res = client.scan(new ScanRequest()
.withTableName(tableName)
.withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL))
res.getConsumedCapacity.getCapacityUnits
}
def populateTable(client:AmazonDynamoDBClient, tableName: String, recordCount: Int, dataSize: Int) = {
val fakeData = new Array[Byte](dataSize)
for (i <- 0 until recordCount) {
val res = client.putItem(new PutItemRequest()
.withTableName(tableName)
.withReturnConsumedCapacity(ReturnConsumedCapacity.TOTAL)
.withItem(Map("k" -> new AttributeValue().withS(i.toString),
"v" -> new AttributeValue().withB(ByteBuffer.wrap(fakeData)))))
}
print("Finished populating\n")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment