<s> """<STR_LIT>""" <EOL> import core . data <EOL> import hashlib <EOL> import json <EOL> import logging <EOL> import psycopg2 <EOL> import psycopg2 . extensions <EOL> import random <EOL> import multiprocessing <EOL> import time <EOL> class Benchmark ( multiprocessing . Process ) : <EOL> def __init__ ( self , host , port , user , name , password , keys = None , q = None ) : <EOL> super ( self . __class__ , self ) . __init__ ( ) <EOL> self . keys = keys <EOL> if host : <EOL> self . host = host <EOL> else : <EOL> self . host = '<STR_LIT:localhost>' <EOL> if port : <EOL> self . port = port <EOL> else : <EOL> self . port = <NUM_LIT> <EOL> self . user = user <EOL> self . dbname = name <EOL> self . password = password <EOL> self . cursor = None <EOL> self . connect ( ) <EOL> self . q = q <EOL> def connect ( self ) : <EOL> dsn = "<STR_LIT>" % ( self . host , int ( self . port ) , self . user , self . dbname ) <EOL> if self . password : <EOL> dsn = "<STR_LIT>" % ( dsn , self . password ) <EOL> logging . info ( '<STR_LIT>' ) <EOL> self . pgsql = psycopg2 . connect ( dsn ) <EOL> self . pgsql . set_isolation_level ( psycopg2 . extensions . ISOLATION_LEVEL_AUTOCOMMIT ) <EOL> self . cursor = self . pgsql . cursor ( ) <EOL> def run ( self ) : <EOL> select = "<STR_LIT>" <EOL> update = "<STR_LIT>" <EOL> logging . info ( '<STR_LIT>' ) <EOL> bid = core . bench . start ( '<STR_LIT>' ) <EOL> failed = <NUM_LIT:0> <EOL> for key in self . keys : <EOL> try : <EOL> x = random . randint ( <NUM_LIT:0> , <NUM_LIT:100> ) <EOL> if x < <NUM_LIT> : <EOL> sql = select % key <EOL> self . cursor . execute ( sql ) <EOL> if not self . cursor . rowcount : <EOL> failed += <NUM_LIT:1> <EOL> break <EOL> elif x > <NUM_LIT> : <EOL> self . cursor . execute ( select % key ) <EOL> record = self . cursor . fetchone ( ) <EOL> if not record : <EOL> failed += <NUM_LIT:1> <EOL> else : <EOL> value = json . loads ( record [ <NUM_LIT:0> ] ) <EOL> value [ '<STR_LIT>' ] = '<STR_LIT>' % value [ '<STR_LIT>' ] <EOL> self . cursor . execute ( update , { '<STR_LIT>' : key , '<STR_LIT:value>' : json . dumps ( value ) } ) <EOL> if not self . cursor . rowcount : <EOL> failed += <NUM_LIT:1> <EOL> except Exception , e : <EOL> logging . error ( '<STR_LIT>' % e ) <EOL> core . bench . end ( bid ) <EOL> logging . info ( '<STR_LIT>' % failed ) <EOL> self . q . put ( core . bench . get ( ) ) <EOL> def load ( self , csvfile ) : <EOL> csv = core . data . load_csv ( csvfile ) <EOL> try : <EOL> self . cursor . execute ( '<STR_LIT>' ) <EOL> if self . cursor . rowcount : <EOL> logging . info ( '<STR_LIT>' ) <EOL> self . cursor . execute ( '<STR_LIT>' ) <EOL> except psycopg2 . ProgrammingError : <EOL> logging . info ( '<STR_LIT>' ) <EOL> sql = '<STR_LIT>' <EOL> logging . debug ( sql ) <EOL> self . cursor . execute ( sql ) <EOL> sql = "<STR_LIT>" <EOL> logging . info ( '<STR_LIT>' ) <EOL> x = <NUM_LIT:0> <EOL> try : <EOL> for row in csv : <EOL> pkey = core . data . make_key ( csv . fieldnames , row ) <EOL> self . cursor . execute ( sql , { '<STR_LIT>' : pkey , '<STR_LIT:value>' : json . dumps ( row ) } ) <EOL> x += <NUM_LIT:1> <EOL> except Exception , e : <EOL> logging . error ( '<STR_LIT>' % e ) <EOL> return False <EOL> logging . info ( '<STR_LIT>' % x ) <EOL> return True </s>
