def main(): # Setup logging logging.basicConfig( level=logging.DEBUG, format='[%(levelname)s] (%(threadName)-10s) %(message)s', ) # Get the current minimum and maximum value of the auto_increment primary key in our test table. conn = settings.db_connect() cur = conn.cursor() cur.execute('SELECT id FROM ' + settings.DB_TABLE + ' ORDER BY id ASC LIMIT 1') id_min = cur.fetchone()[0] cur.execute('SELECT id FROM ' + settings.DB_TABLE + ' ORDER BY id DESC LIMIT 1') id_max = cur.fetchone()[0] cur.close() conn.close() # Start worker threads try: threads = [Worker(id_min, id_max) for i in xrange(settings.THREAD_COUNT)] for thread in threads: thread.start() while True: sleep(1) except (KeyboardInterrupt, SystemExit): for thread in threads: thread.stop()
def run(self): vhost_list = ['www.mycompany.co.jp', 'www.company.info', 'www.service.io', 'www.website.com'] status_list = [200, 404, 500] timestamp = time() while not self.stopped(): start = time() conn = settings.db_connect() cur = conn.cursor() parameters = [] for i in xrange(settings.INSERT_ROW_COUNT): vhost = choice(vhost_list) rhost = '.'.join((str(randrange(1, 256)), str(randrange(1, 256)), str(randrange(1, 256)), str(randrange(1, 256)))) logname = '-' username = randstring(10) timestamp += randrange(0, 5) / 10 request = '/%s/%s/%s.html' % (randstring(20), randstring(20), randstring(20)) status = choice(status_list) response_size = randrange(1, 16 * 1024 * 1024) parameters.append((vhost, rhost, logname, username, settings.db_TimestampFromTicks(timestamp), request, status, response_size)) cur.executemany('INSERT INTO ' + settings.DB_TABLE + ' (vhost, rhost, logname, username, timestamp, request, status, response_size) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', parameters) conn.commit() cur.close() conn.close() end = time() logging.info('Inserting %s records took %.2f seconds... (%.2f seconds per 1000 records)' % (len(parameters), (end - start), (end - start) / len(parameters) * 1000))
def run(self): logging.debug('Starting...') while not self.stopped(): start = time() conn = settings.db_connect() cur = conn.cursor() for i in xrange(settings.SELECT_ROW_COUNT): cur.execute('SELECT * FROM ' + settings.DB_TABLE + ' WHERE id = %s', (randrange(self.id_min, self.id_max),)) conn.commit() cur.close() conn.close() end = time() logging.info('Selecting %s rows from indexes between [%s, %s] took %.2f seconds...' % (settings.SELECT_ROW_COUNT, self.id_min, self.id_max, (end - start),))
def run(self): vhost_list = [ 'www.mycompany.co.jp', 'www.company.info', 'www.service.io', 'www.website.com' ] status_list = [200, 404, 500] timestamp = time() while not self.stopped(): start = time() conn = settings.db_connect() cur = conn.cursor() parameters = [] for i in xrange(settings.INSERT_ROW_COUNT): vhost = choice(vhost_list) rhost = '.'.join( (str(randrange(1, 256)), str(randrange(1, 256)), str(randrange(1, 256)), str(randrange(1, 256)))) logname = '-' username = randstring(10) timestamp += randrange(0, 5) / 10 request = '/%s/%s/%s.html' % (randstring(20), randstring(20), randstring(20)) status = choice(status_list) response_size = randrange(1, 16 * 1024 * 1024) parameters.append((vhost, rhost, logname, username, settings.db_TimestampFromTicks(timestamp), request, status, response_size)) cur.executemany( 'INSERT INTO ' + settings.DB_TABLE + ' (vhost, rhost, logname, username, timestamp, request, status, response_size) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)', parameters) conn.commit() cur.close() conn.close() end = time() logging.info( 'Inserting %s records took %.2f seconds... (%.2f seconds per 1000 records)' % (len(parameters), (end - start), (end - start) / len(parameters) * 1000))