Advertisement
Guest User

Untitled

a guest
Jan 16th, 2024
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.69 KB | None | 0 0
  1. import time,psycopg2
  2.  
  3. NUNIQUE = 10000
  4. NDUPES  = 1
  5.  
  6. def bench( f, *args, **kwargs ):
  7.     t = time.time()
  8.     try:
  9.         return f( *args, **kwargs )
  10.     finally:
  11.         dt = time.time()-t
  12.         c = db.cursor()
  13.         c.execute( "SELECT pg_relation_size( 'testins' )")
  14.         size, = c.fetchone()
  15.         c.execute( "SELECT count(*) FROM testins")
  16.         rows, = c.fetchone()
  17.         print("%s: %.03fs %d rows %10.03f kB" % (f.__name__, dt, rows, size/1024))
  18.  
  19. def insert_only( ):
  20.     c = db.cursor()
  21.     for id in range( NUNIQUE ):
  22.         for n in range( NDUPES ):
  23.             try:
  24.                 c.execute("INSERT INTO testins (id,t) VALUES (%s,'hello, world')", (id,) )
  25.                 c.execute("COMMIT")
  26.             except Exception as e:
  27.                 # print(e)
  28.                 c.execute("ROLLBACK")
  29.  
  30. def select_then_insert( ):
  31.     c = db.cursor()
  32.     for id in range( NUNIQUE ):
  33.         for n in range( NDUPES ):
  34.             try:
  35.                 c.execute("SELECT * FROM testins WHERE id=%s", (id,) )
  36.                 if not c.fetchall():
  37.                     c.execute("INSERT INTO testins (id,t) VALUES (%s,'hello, world')", (id,) )
  38.                 c.execute("COMMIT")
  39.             except Exception as e:
  40.                 # print(e)
  41.                 c.execute("ROLLBACK")
  42.  
  43. def insert_select( ):
  44.     c = db.cursor()
  45.     for id in range( NUNIQUE ):
  46.         for n in range( NDUPES ):
  47.             try:
  48.                 c.execute("""INSERT INTO testins (id,t) SELECT %s,'hello, world'
  49.                    WHERE NOT EXISTS( SELECT FROM testins WHERE id=%s ) RETURNING id
  50.                    """, (id,id) )
  51.             except Exception as e:
  52.                 # print(e)
  53.                 c.execute("ROLLBACK")
  54.  
  55. def on_conflict( ):
  56.     c = db.cursor()
  57.     for id in range( NUNIQUE ):
  58.         for n in range( NDUPES ):
  59.             try:
  60.                 c.execute("INSERT INTO testins (id,t) VALUES (%s,'hello, world') ON CONFLICT (id) DO NOTHING RETURNING id", (id,) )
  61.                 c.execute("COMMIT")
  62.             except Exception as e:
  63.                 # print(e)
  64.                 c.execute("ROLLBACK")
  65.  
  66.  
  67.  
  68. if __name__ == "__main__":
  69.     db = psycopg2.connect("dbname=test")
  70.     c = db.cursor()
  71.     try:
  72.         c.execute( "CREATE TABLE testins( id INTEGER PRIMARY KEY, t TEXT )");
  73.         c.execute("COMMIT")
  74.     except Exception as e:
  75.         print(e)
  76.         c.execute("ROLLBACK")
  77.  
  78.     c.execute( "TRUNCATE testins;" )
  79.     r = bench( on_conflict )
  80.  
  81.     c.execute( "TRUNCATE testins;" )
  82.     r = bench( insert_only )
  83.  
  84.     c.execute( "TRUNCATE testins;" )
  85.     r = bench( select_then_insert )
  86.  
  87.     c.execute( "TRUNCATE testins;" )
  88.     r = bench( insert_select )
  89.  
  90.  
  91.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement