Skip to content

Instantly share code, notes, and snippets.

@massie
Created December 13, 2012 00:55
Show Gist options
  • Save massie/4273109 to your computer and use it in GitHub Desktop.
Save massie/4273109 to your computer and use it in GitHub Desktop.
Calculating DynamoDB costs
package edu.berkeley.cs.amplab.carat.dynamodb
import com.amazonaws.services.dynamodb.model.{TableDescription, DescribeTableRequest}
import collection.JavaConversions._
class DynamoDBCostStructure(cp10wu: Double, cp50ru: Double, cpgb: Double) {
private val costPer10WriteUnits: Double = cp10wu
private val costPer50ReadUnits: Double = cp50ru
private val costPerGB: Double = cpgb
private val hoursPerMonth = 24 * 31
def monthlyCapacityCost(tableDesc: TableDescription): Double = {
// We pay a flat hourly rate based on the capacity we reserve
tableDesc.getProvisionedThroughput.getReadCapacityUnits / 50.0 * costPer50ReadUnits * hoursPerMonth +
tableDesc.getProvisionedThroughput.getWriteCapacityUnits / 10.0 * costPer10WriteUnits * hoursPerMonth
}
def monthlyStorageCost(tableDesc: TableDescription): Double = {
// We pay for each GB of stored data
tableDesc.getTableSizeBytes / 1024.0 / 1024.0 * costPerGB
}
def monthlyTransferCosts(): Unit = {
// TODO: We pay for outbound data transfers (~0.12 per GB). Inbound is free.
Unit
}
}
/**
* Program to calculate costs of DynamoDB tables
*/
object CalculateCosts {
val otherUSCost = new DynamoDBCostStructure(0.01, 0.01, 1.00)
val norcalUSCost = new DynamoDBCostStructure(0.0112, 0.0112, 1.12)
val costStructure = otherUSCost
val delim = ","
val dollarFmt = "$%.2f"
def main(args: Array[String]) {
val tables = DynamoDbEncoder.dd.listTables().getTableNames
assessTables(tables: _*)
}
def assessTables(tables: String*) {
println(Array("name", "capacity cost", "storage cost", "status", "total size bytes",
"items", "avg. record bytes").mkString(delim))
for (t <- tables) {
val v = DynamoDbEncoder.dd.describeTable(new DescribeTableRequest().withTableName(t)).getTable
val capacityCost = costStructure.monthlyCapacityCost(v).formatted(dollarFmt)
val storageCost = costStructure.monthlyStorageCost(v).formatted(dollarFmt)
println(Array(v.getTableName, capacityCost, storageCost, v.getTableStatus,
(v.getTableSizeBytes / 1024.0 / 1024.0).formatted("%.2f GB"),
v.getItemCount, v.getTableSizeBytes/v.getItemCount).mkString(delim))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment