Since PA is based on AWS, I decided to check it against that to see what level of AWS service I am getting at PA. Here is the code for the test (it is the same as above, except that smaller data generated in chunks is used to fit the RAM limit of the free tier of EC2):
import psycopg2
import time
import random
import string
CHUNK_SIZE = 10000000
N_CHUNKS = 10 # Do in chunks, so as to avoid a huge list
def ms_now():
return int(time.time_ns() / 1000000)
class Timer():
def __init__(self, timestamp_function=ms_now):
self.timestamp_function = timestamp_function
self.start = self.timestamp_function()
def stop(self):
return self.timestamp_function() - self.start
def random_text():
result = ''
for i in range(N_CHUNKS):
print(f"Chunk {i+1} of {N_CHUNKS}")
result += ''.join([random.choice(string.ascii_letters) for _ in range(CHUNK_SIZE)])
return result
def connect():
# Fill out host, port, dbname, username and password here
return psycopg2.connect(
host=host,
port=port,
dbname=dbname,
user=username,
password=password
)
def create_database(conn):
with conn.cursor() as cursor:
cursor.execute("DROP TABLE IF EXISTS speed_test;")
cursor.execute(
"CREATE TABLE speed_test (id serial PRIMARY KEY, data text);")
conn.commit()
timer = Timer()
data = random_text()
print(f"Generation time: {timer.stop()}ms")
timer = Timer()
cursor.execute("INSERT INTO speed_test (data) VALUES (%s);", (data,))
conn.commit()
print(f"Insertion time: {timer.stop()}ms")
def query_database(conn):
with conn.cursor() as cursor:
timer = Timer()
cursor.execute("SELECT data FROM speed_test;")
print(f"Query time: {timer.stop()}ms")
conn = connect()
create_database(conn)
query_database(conn)
Here are the timing results at PA:
Generation time: 77334ms
Insertion time: 15911ms
Query time: 2389ms
And this what I get by running the script on the FREE TIER of EC2 with the database located in the FREE TIER of RDS (Postgres):
Generation time: 54626ms
Insertion time: 14964ms
Query time: 2325ms
The performance I get from PA is about equal (and even slightly worse as far the computation is concerned) to the performance of the weakest instances of AWS (free tier is free tier), while I do pay money to PA. In my opinion, this is quite conclusive...