Skip to content

Instantly share code, notes, and snippets.

@Intelrunner
Last active April 19, 2023 17:28
Show Gist options
  • Save Intelrunner/23cd94915413bc0fd510df443842d9d7 to your computer and use it in GitHub Desktop.
Save Intelrunner/23cd94915413bc0fd510df443842d9d7 to your computer and use it in GitHub Desktop.
Calculating Spanner Costs from GCE
class GCPCostCalculator:
def __init__(self):
self.sql_cores = 0
self.sql_ram = 0
self.sql_disk_size = 0
self.sql_disk_type = ""
self.sql_num_machines = 0
self.spanner_nodes = 0
self.spanner_region = ""
def get_inputs(self):
self.sql_cores = int(input("Enter the number of cores per SQL machine: "))
self.sql_ram = int(input("Enter the gb of RAM per SQL machine: "))
self.sql_disk_size = int(input("Enter the size of disk per SQL machine: "))
while True:
self.sql_disk_type = input("Enter the type of disk per SQL machine (ssd, standard, or balanced): ")
if self.sql_disk_type in ["ssd", "standard", "balanced"]:
break
else:
print("Invalid disk type. Please enter ssd, standard, or balanced.")
self.sql_num_machines = int(input("Enter the number of SQL machines: "))
self.spanner_nodes = int(input("Enter the number of Spanner nodes: "))
while True:
self.spanner_region = input("Enter the Spanner region (us-central1, us-west1, us-east1): ")
if self.spanner_region in ["us-central1", "us-west1", "us-east1"]:
break
else:
print("Invalid region. Please enter us-central1, us-west1, or us-east1.")
def calculate_sql_cost(self):
disk_price_map = {"ssd": 0.17, "standard": 0.08, "balanced": 0.12}
disk_price_per_gb = disk_price_map[self.sql_disk_type]
sql_disk_cost = self.sql_disk_size * disk_price_per_gb
sql_cost_per_hour = (self.sql_cores * 0.030) + (self.sql_ram * 0.004) + sql_disk_cost
sql_cost_per_month = sql_cost_per_hour * 24 * 30 * self.sql_num_machines
return sql_cost_per_month
def calculate_spanner_cost(self):
region_cost_map = {"us-central1": 1.50, "us-west1": 1.70, "us-east1": 1.80}
spanner_cost_per_node = self.spanner_nodes * region_cost_map[self.spanner_region]
spanner_cost_per_month = spanner_cost_per_node * 24 * 30
return spanner_cost_per_month
def calculate_cost_comparison(self):
sql_cost = self.calculate_sql_cost()
spanner_cost = self.calculate_spanner_cost()
if spanner_cost > sql_cost:
return "SQL machines are cheaper than Spanner nodes."
else:
return "Spanner nodes are cheaper than SQL machines."
def run(self):
self.get_inputs()
sql_cost = self.calculate_sql_cost()
spanner_cost = self.calculate_spanner_cost()
cost_comparison = self.calculate_cost_comparison()
print(f"Cost of SQL machines: ${sql_cost:.2f}")
print(f"Cost of Spanner nodes: ${spanner_cost:.2f}")
print(cost_comparison)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment