#!/usr/bin/env python
import sys
import threading
import logging
from time import time, sleep
from random import randrange
import psycopg2
import requests

DB_HOST = '54.165.169.96'
DB_USER = 'postgres'
DB_NAME = 'bench'
DB_TABLE = 'account3_bench'
THREAD_COUNT = 20
PASSWD = 'test'


def connect():
    conn = psycopg2.connect(host=DB_HOST, user=DB_USER, database=DB_NAME, password=PASSWD, sslmode='require')
    return conn


class Worker(threading.Thread):
    def __init__(self, id_min, id_max):
        super(Worker, self).__init__()
        self._stop = threading.Event()
        self.id_min = id_min
        self.id_max = id_max

    def stop(self):
        logging.debug('Stopping...')
        self._stop.set()

    def stopped(self):
        return self._stop.isSet()

    def run(self):
        logging.debug('Starting...')
        i = 0
        while not self.stopped():
            start = time()
            conn = connect()
            cur = conn.cursor()
            account_id = randrange(self.id_min, self.id_max)
            cur.execute('SELECT * FROM ' + DB_TABLE + ' WHERE account_id = %d' % account_id)
            conn.commit()
            cur.close()
            conn.close()
            end = time()
            if (i % 100) == 0:
                requests.get('https://google.com')

            sleep(0.01)
            i += 1
            logging.info('Selecting rows from indexes between [%s, %s] took %.2f seconds...' % (self.id_min, self.id_max, (end - start),))



def main():
    # Setup logging
    logging.basicConfig(
        level=logging.DEBUG,
        format='%(asctime)s - [%(levelname)s] (%(threadName)-10s) %(message)s',
        datefmt='%m-%d-%y %H:%M',
        filename='db_stats1.log',
        filemode='a'
    )

    conn = connect()
    cur = conn.cursor()
    cur.execute('SELECT min(account_id) FROM ' + DB_TABLE)
    id_min = cur.fetchone()[0]
    cur.execute('SELECT max(account_id) FROM ' + DB_TABLE)
    id_max = cur.fetchone()[0]
    cur.close()
    conn.close()

    # Start worker threads
    try:
        threads = [Worker(id_min, id_max) for i in xrange(THREAD_COUNT)]
        for thread in threads:
            thread.start()

        while True:
            sleep(1)
    except (KeyboardInterrupt, SystemExit):
        for thread in threads:
            thread.stop()

if __name__ == "__main__":
    sys.exit(main())
