## 2. Fetch the result and return it. return sess.query(Account).count() ## Create S sums operations def S_sums(sess, S): sums = [] start = time.time() for i in xrange(0, S): while True: try: sum = sum_balance(sess) sums.append(sum) except Exception as e: print e continue break stop = time.time() return sums, stop - start ## Create the engine and run the sums engine = get_conn() Session = sessionmaker( bind=engine.execution_options(isolation_level=args.I, autocommit=True)) sess = Session() sums, time = S_sums(sess, args.S) print sums
import time import random from db_connect import get_conn conn = get_conn().raw_connection() cursor = conn.cursor() cursor.execute("DROP TABLE IF EXISTS account") cursor.execute(""" CREATE TABLE account( id INT NOT NULL GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, branch INT NOT NULL, balance FLOAT NOT NULL) """) for i in range(0, 100000): account = random.randint(1, 20) balance = round(random.uniform(0, 100000), 2) cursor.execute("""INSERT INTO account VALUES (DEFAULT, %d,%f)""" % (account, balance)) conn.commit() print('Done!')