import json
import asyncio
import pymysql
import time
import random
from aiomysql import create_pool

now = lambda: time.time()
DATALEN = 231780
REPEATTIME = 200
STEP = 8
SYNC = False

HOST = 'localhost'
USER = 'root'
PORT = 3306
PW = '123456'
DB = 'sjhtest'

COUNT = [0 for i in range(STEP)]

COLUMNS = ['reviewerID', 
			'asin', 
			'helpful',
			'reviewText',
			'overall',
			'summary',
			'unixReviewTime',
			'reviewTime',]

async def exe_sql(lp, i):
	try:
		async with create_pool(host=HOST, port=PORT,
							   user=USER, password=PW,
							   db=DB, loop=lp) as pool:
			async with pool.get() as conn:
				async with conn.cursor() as cur:
					await cur.execute("UPDATE review SET helpful = '12345' \
										WHERE reviewerID = 'TESTUSER' \
										and reviewTime = {};"
										.format(random.randint(2000, 2014)))
					# print (item['reviewerID'])
					await conn.commit()
					# value = await cur.fetchone()
					# COUNT[i] += len(value)
	except KeyboardInterrupt:
		pass
	except Exception as e:
		print (e)
		# print (item)

def updDbSync():
	logfile = open("{}_{}_upd_log_s.csv".format(REPEATTIME, STEP), "w")
	count = 0

	st = now()
	lst = st
	print (STEP, 'Task Sync Test Start')
	db = pymysql.connect(HOST,USER,PW,DB )
	cursor = db.cursor()
	for i in range(REPEATTIME):
		try:
			cursor.execute("UPDATE review SET helpful = '12345' \
							WHERE reviewerID = 'TESTUSER' \
							and reviewTime = {};"
							.format(random.randint(2000, 2014)))
			db.commit()
			# value = cursor.fetchall()
			# count += len(value)
		except KeyboardInterrupt:
			pass
		except Exception as e:
			print (e)
		# db.close()
		if (i+1) % STEP == 0:
			# print ("{} Queries Finished, {} Lines Fetched. Time Elapsed: {:.4}".format((i+1), count, now() - st))
			# logfile.write("{},{},{:.4}\n".format((i+1), count, now()-lst))
			print ("{} Queries Finished. Time Elapsed: {:.4}".format((i+1), now() - st))
			logfile.write("{},{:.4}\n".format((i+1), now()-lst))
		lst = st

	# for item in data[:100]:
	# 	insertData(item)
	print (STEP, 'Task Sync Test Finished', now() - st)
	logfile.close()

def updDbAsync():
	logfile = open("{}_{}_upd_log_a.csv".format(REPEATTIME, STEP), "w")
	
	st = now()
	lst = st

	print (STEP, 'Task Async Test Start')
	for sti in range(0, REPEATTIME, STEP):
		lp = asyncio.get_event_loop()
		tasks = [asyncio.ensure_future(exe_sql(lp, si)) for si in range(STEP)]
		# print (tasks)
		lp.run_until_complete(asyncio.wait(tasks))
		# count += len(dones)
		# count_sum = sum(COUNT)
		if (sti + STEP) % STEP == 0:
			# print ("{} Queries Finished, {} Lines Fetched. Time Elapsed: {:.4}".format(sti+STEP, count_sum, now() - st))
			# logfile.write("{},{},{:.4}\n".format(sti+STEP, count_sum, now()-lst))
			print ("{} Queries Finished. Time Elapsed: {:.4}".format(sti+STEP, now() - st))
			logfile.write("{},{:.4}\n".format(sti+STEP, now()-lst))
		lst = st

	# for item in data[:100]:
	# 	insertData(item)
	print (STEP, 'Tast Async Test Finished', now() - st)
	logfile.close()

updDbSync()
updDbAsync()


d42c94e1d485037417447e67e3c7467d5c11902d
<mask token>


def updDbSync():
    logfile = open('{}_{}_upd_log_s.csv'.format(REPEATTIME, STEP), 'w')
    count = 0
    st = now()
    lst = st
    print(STEP, 'Task Sync Test Start')
    db = pymysql.connect(HOST, USER, PW, DB)
    cursor = db.cursor()
    for i in range(REPEATTIME):
        try:
            cursor.execute(
                "UPDATE review SET helpful = '12345' \t\t\t\t\t\t\tWHERE reviewerID = 'TESTUSER' \t\t\t\t\t\t\tand reviewTime = {};"
                .format(random.randint(2000, 2014)))
            db.commit()
        except KeyboardInterrupt:
            pass
        except Exception as e:
            print(e)
        if (i + 1) % STEP == 0:
            print('{} Queries Finished. Time Elapsed: {:.4}'.format(i + 1, 
                now() - st))
            logfile.write('{},{:.4}\n'.format(i + 1, now() - lst))
        lst = st
    print(STEP, 'Task Sync Test Finished', now() - st)
    logfile.close()


def updDbAsync():
    logfile = open('{}_{}_upd_log_a.csv'.format(REPEATTIME, STEP), 'w')
    st = now()
    lst = st
    print(STEP, 'Task Async Test Start')
    for sti in range(0, REPEATTIME, STEP):
        lp = asyncio.get_event_loop()
        tasks = [asyncio.ensure_future(exe_sql(lp, si)) for si in range(STEP)]
        lp.run_until_complete(asyncio.wait(tasks))
        if (sti + STEP) % STEP == 0:
            print('{} Queries Finished. Time Elapsed: {:.4}'.format(sti +
                STEP, now() - st))
            logfile.write('{},{:.4}\n'.format(sti + STEP, now() - lst))
        lst = st
    print(STEP, 'Tast Async Test Finished', now() - st)
    logfile.close()


<mask token>

<mask token>


async def exe_sql(lp, i):
    try:
        async with create_pool(host=HOST, port=PORT, user=USER, password=PW,
            db=DB, loop=lp) as pool:
            async with pool.get() as conn:
                async with conn.cursor() as cur:
                    await cur.execute(
                        "UPDATE review SET helpful = '12345' \t\t\t\t\t\t\t\t\t\tWHERE reviewerID = 'TESTUSER' \t\t\t\t\t\t\t\t\t\tand reviewTime = {};"
                        .format(random.randint(2000, 2014)))
                    await conn.commit()
    except KeyboardInterrupt:
        pass
    except Exception as e:
        print(e)


def updDbSync():
    logfile = open('{}_{}_upd_log_s.csv'.format(REPEATTIME, STEP), 'w')
    count = 0
    st = now()
    lst = st
    print(STEP, 'Task Sync Test Start')
    db = pymysql.connect(HOST, USER, PW, DB)
    cursor = db.cursor()
    for i in range(REPEATTIME):
        try:
            cursor.execute(
                "UPDATE review SET helpful = '12345' \t\t\t\t\t\t\tWHERE reviewerID = 'TESTUSER' \t\t\t\t\t\t\tand reviewTime = {};"
                .format(random.randint(2000, 2014)))
            db.commit()
        except KeyboardInterrupt:
            pass
        except Exception as e:
            print(e)
        if (i + 1) % STEP == 0:
            print('{} Queries Finished. Time Elapsed: {:.4}'.format(i + 1, 
                now() - st))
            logfile.write('{},{:.4}\n'.format(i + 1, now() - lst))
        lst = st
    print(STEP, 'Task Sync Test Finished', now() - st)
    logfile.close()


def updDbAsync():
    logfile = open('{}_{}_upd_log_a.csv'.format(REPEATTIME, STEP), 'w')
    st = now()
    lst = st
    print(STEP, 'Task Async Test Start')
    for sti in range(0, REPEATTIME, STEP):
        lp = asyncio.get_event_loop()
        tasks = [asyncio.ensure_future(exe_sql(lp, si)) for si in range(STEP)]
        lp.run_until_complete(asyncio.wait(tasks))
        if (sti + STEP) % STEP == 0:
            print('{} Queries Finished. Time Elapsed: {:.4}'.format(sti +
                STEP, now() - st))
            logfile.write('{},{:.4}\n'.format(sti + STEP, now() - lst))
        lst = st
    print(STEP, 'Tast Async Test Finished', now() - st)
    logfile.close()


updDbSync()
updDbAsync()

<mask token>
now = lambda : time.time()
DATALEN = 231780
REPEATTIME = 200
STEP = 8
SYNC = False
HOST = 'localhost'
USER = 'root'
PORT = 3306
PW = '123456'
DB = 'sjhtest'
COUNT = [(0) for i in range(STEP)]
COLUMNS = ['reviewerID', 'asin', 'helpful', 'reviewText', 'overall',
    'summary', 'unixReviewTime', 'reviewTime']


async def exe_sql(lp, i):
    try:
        async with create_pool(host=HOST, port=PORT, user=USER, password=PW,
            db=DB, loop=lp) as pool:
            async with pool.get() as conn:
                async with conn.cursor() as cur:
                    await cur.execute(
                        "UPDATE review SET helpful = '12345' \t\t\t\t\t\t\t\t\t\tWHERE reviewerID = 'TESTUSER' \t\t\t\t\t\t\t\t\t\tand reviewTime = {};"
                        .format(random.randint(2000, 2014)))
                    await conn.commit()
    except KeyboardInterrupt:
        pass
    except Exception as e:
        print(e)


def updDbSync():
    logfile = open('{}_{}_upd_log_s.csv'.format(REPEATTIME, STEP), 'w')
    count = 0
    st = now()
    lst = st
    print(STEP, 'Task Sync Test Start')
    db = pymysql.connect(HOST, USER, PW, DB)
    cursor = db.cursor()
    for i in range(REPEATTIME):
        try:
            cursor.execute(
                "UPDATE review SET helpful = '12345' \t\t\t\t\t\t\tWHERE reviewerID = 'TESTUSER' \t\t\t\t\t\t\tand reviewTime = {};"
                .format(random.randint(2000, 2014)))
            db.commit()
        except KeyboardInterrupt:
            pass
        except Exception as e:
            print(e)
        if (i + 1) % STEP == 0:
            print('{} Queries Finished. Time Elapsed: {:.4}'.format(i + 1, 
                now() - st))
            logfile.write('{},{:.4}\n'.format(i + 1, now() - lst))
        lst = st
    print(STEP, 'Task Sync Test Finished', now() - st)
    logfile.close()


def updDbAsync():
    logfile = open('{}_{}_upd_log_a.csv'.format(REPEATTIME, STEP), 'w')
    st = now()
    lst = st
    print(STEP, 'Task Async Test Start')
    for sti in range(0, REPEATTIME, STEP):
        lp = asyncio.get_event_loop()
        tasks = [asyncio.ensure_future(exe_sql(lp, si)) for si in range(STEP)]
        lp.run_until_complete(asyncio.wait(tasks))
        if (sti + STEP) % STEP == 0:
            print('{} Queries Finished. Time Elapsed: {:.4}'.format(sti +
                STEP, now() - st))
            logfile.write('{},{:.4}\n'.format(sti + STEP, now() - lst))
        lst = st
    print(STEP, 'Tast Async Test Finished', now() - st)
    logfile.close()


updDbSync()
updDbAsync()

import json
import asyncio
import pymysql
import time
import random
from aiomysql import create_pool
now = lambda : time.time()
DATALEN = 231780
REPEATTIME = 200
STEP = 8
SYNC = False
HOST = 'localhost'
USER = 'root'
PORT = 3306
PW = '123456'
DB = 'sjhtest'
COUNT = [(0) for i in range(STEP)]
COLUMNS = ['reviewerID', 'asin', 'helpful', 'reviewText', 'overall',
    'summary', 'unixReviewTime', 'reviewTime']


async def exe_sql(lp, i):
    try:
        async with create_pool(host=HOST, port=PORT, user=USER, password=PW,
            db=DB, loop=lp) as pool:
            async with pool.get() as conn:
                async with conn.cursor() as cur:
                    await cur.execute(
                        "UPDATE review SET helpful = '12345' \t\t\t\t\t\t\t\t\t\tWHERE reviewerID = 'TESTUSER' \t\t\t\t\t\t\t\t\t\tand reviewTime = {};"
                        .format(random.randint(2000, 2014)))
                    await conn.commit()
    except KeyboardInterrupt:
        pass
    except Exception as e:
        print(e)


def updDbSync():
    logfile = open('{}_{}_upd_log_s.csv'.format(REPEATTIME, STEP), 'w')
    count = 0
    st = now()
    lst = st
    print(STEP, 'Task Sync Test Start')
    db = pymysql.connect(HOST, USER, PW, DB)
    cursor = db.cursor()
    for i in range(REPEATTIME):
        try:
            cursor.execute(
                "UPDATE review SET helpful = '12345' \t\t\t\t\t\t\tWHERE reviewerID = 'TESTUSER' \t\t\t\t\t\t\tand reviewTime = {};"
                .format(random.randint(2000, 2014)))
            db.commit()
        except KeyboardInterrupt:
            pass
        except Exception as e:
            print(e)
        if (i + 1) % STEP == 0:
            print('{} Queries Finished. Time Elapsed: {:.4}'.format(i + 1, 
                now() - st))
            logfile.write('{},{:.4}\n'.format(i + 1, now() - lst))
        lst = st
    print(STEP, 'Task Sync Test Finished', now() - st)
    logfile.close()


def updDbAsync():
    logfile = open('{}_{}_upd_log_a.csv'.format(REPEATTIME, STEP), 'w')
    st = now()
    lst = st
    print(STEP, 'Task Async Test Start')
    for sti in range(0, REPEATTIME, STEP):
        lp = asyncio.get_event_loop()
        tasks = [asyncio.ensure_future(exe_sql(lp, si)) for si in range(STEP)]
        lp.run_until_complete(asyncio.wait(tasks))
        if (sti + STEP) % STEP == 0:
            print('{} Queries Finished. Time Elapsed: {:.4}'.format(sti +
                STEP, now() - st))
            logfile.write('{},{:.4}\n'.format(sti + STEP, now() - lst))
        lst = st
    print(STEP, 'Tast Async Test Finished', now() - st)
    logfile.close()


updDbSync()
updDbAsync()

import json
import asyncio
import pymysql
import time
import random
from aiomysql import create_pool

now = lambda: time.time()
DATALEN = 231780
REPEATTIME = 200
STEP = 8
SYNC = False

HOST = 'localhost'
USER = 'root'
PORT = 3306
PW = '123456'
DB = 'sjhtest'

COUNT = [0 for i in range(STEP)]

COLUMNS = ['reviewerID', 
			'asin', 
			'helpful',
			'reviewText',
			'overall',
			'summary',
			'unixReviewTime',
			'reviewTime',]

async def exe_sql(lp, i):
	try:
		async with create_pool(host=HOST, port=PORT,
							   user=USER, password=PW,
							   db=DB, loop=lp) as pool:
			async with pool.get() as conn:
				async with conn.cursor() as cur:
					await cur.execute("UPDATE review SET helpful = '12345' \
										WHERE reviewerID = 'TESTUSER' \
										and reviewTime = {};"
										.format(random.randint(2000, 2014)))
					# print (item['reviewerID'])
					await conn.commit()
					# value = await cur.fetchone()
					# COUNT[i] += len(value)
	except KeyboardInterrupt:
		pass
	except Exception as e:
		print (e)
		# print (item)

def updDbSync():
	logfile = open("{}_{}_upd_log_s.csv".format(REPEATTIME, STEP), "w")
	count = 0

	st = now()
	lst = st
	print (STEP, 'Task Sync Test Start')
	db = pymysql.connect(HOST,USER,PW,DB )
	cursor = db.cursor()
	for i in range(REPEATTIME):
		try:
			cursor.execute("UPDATE review SET helpful = '12345' \
							WHERE reviewerID = 'TESTUSER' \
							and reviewTime = {};"
							.format(random.randint(2000, 2014)))
			db.commit()
			# value = cursor.fetchall()
			# count += len(value)
		except KeyboardInterrupt:
			pass
		except Exception as e:
			print (e)
		# db.close()
		if (i+1) % STEP == 0:
			# print ("{} Queries Finished, {} Lines Fetched. Time Elapsed: {:.4}".format((i+1), count, now() - st))
			# logfile.write("{},{},{:.4}\n".format((i+1), count, now()-lst))
			print ("{} Queries Finished. Time Elapsed: {:.4}".format((i+1), now() - st))
			logfile.write("{},{:.4}\n".format((i+1), now()-lst))
		lst = st

	# for item in data[:100]:
	# 	insertData(item)
	print (STEP, 'Task Sync Test Finished', now() - st)
	logfile.close()

def updDbAsync():
	logfile = open("{}_{}_upd_log_a.csv".format(REPEATTIME, STEP), "w")
	
	st = now()
	lst = st

	print (STEP, 'Task Async Test Start')
	for sti in range(0, REPEATTIME, STEP):
		lp = asyncio.get_event_loop()
		tasks = [asyncio.ensure_future(exe_sql(lp, si)) for si in range(STEP)]
		# print (tasks)
		lp.run_until_complete(asyncio.wait(tasks))
		# count += len(dones)
		# count_sum = sum(COUNT)
		if (sti + STEP) % STEP == 0:
			# print ("{} Queries Finished, {} Lines Fetched. Time Elapsed: {:.4}".format(sti+STEP, count_sum, now() - st))
			# logfile.write("{},{},{:.4}\n".format(sti+STEP, count_sum, now()-lst))
			print ("{} Queries Finished. Time Elapsed: {:.4}".format(sti+STEP, now() - st))
			logfile.write("{},{:.4}\n".format(sti+STEP, now()-lst))
		lst = st

	# for item in data[:100]:
	# 	insertData(item)
	print (STEP, 'Tast Async Test Finished', now() - st)
	logfile.close()

updDbSync()
updDbAsync()

