Skip to content

Instantly share code, notes, and snippets.

@kmuthukk
Last active July 11, 2020 01:24
Show Gist options
  • Save kmuthukk/611813ab4685179038654486d2ae1c0a to your computer and use it in GitHub Desktop.
Save kmuthukk/611813ab4685179038654486d2ae1c0a to your computer and use it in GitHub Desktop.
# Dependencies:
# On CentOS you can install psycopg2 thus:
#
# sudo yum install postgresql-libs
# sudo yum install python-psycopg2
import psycopg2;
import time;
from datetime import datetime
from multiprocessing.dummy import Pool as ThreadPool
def create_table(table_name):
conn = psycopg2.connect("host=localhost dbname=postgres user=postgres port=5433")
conn.set_session(autocommit=True)
cur = conn.cursor()
cur.execute("""DROP TABLE IF EXISTS """ + table_name);
cur.execute("""CREATE TABLE IF NOT EXISTS """ + table_name + """(
k text not null,
v text not null,
PRIMARY KEY((k) HASH))
""")
print("Created " + table_name)
print("====================")
def load_data(table_name):
conn = psycopg2.connect("host=localhost dbname=postgres user=postgres port=5433")
conn.set_session(autocommit=True)
cur = conn.cursor()
num_rows = 100
# Load data to table
print(" Inserting %d rows..." % (num_rows))
t1 = time.time();
for idx in range(num_rows):
cur.execute("INSERT INTO " + table_name + "(k, v) VALUES (%s, %s)",
("k-"+str(idx), "v-"+str(idx)))
t2 = time.time();
avg_latency_ms = (t2 - t1) * 1000.0 / num_rows
print("Inserted %d rows" % (num_rows))
print("Avg Latency (ms) %f " % (avg_latency_ms))
def select_data():
conn = psycopg2.connect("host=localhost dbname=postgres user=postgres port=5433")
conn.set_session(autocommit=True)
cur = conn.cursor()
num_rows = 100000
for idx in range(num_rows):
cur.execute("""SELECT * from t1, t2 WHERE t1.k = t2.k AND %s = %s LIMIT 1""", ("foo", "foo"))
record = cur.fetchall()
print record
# Main
create_table("t1")
create_table("t2")
load_data("t1")
load_data("t2")
select_data()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment