| 1 | # |
|---|
| 2 | # Sample program used for analysing the trac db pool behavior |
|---|
| 3 | # |
|---|
| 4 | |
|---|
| 5 | import os |
|---|
| 6 | import sys |
|---|
| 7 | import time |
|---|
| 8 | |
|---|
| 9 | import pysqlite2.dbapi2 as sqlite |
|---|
| 10 | |
|---|
| 11 | from trac.env import Environment |
|---|
| 12 | |
|---|
| 13 | try: |
|---|
| 14 | import threading |
|---|
| 15 | except ImportError: |
|---|
| 16 | import dummy_threading as threading |
|---|
| 17 | threading._get_ident = lambda: 0 |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | def main(args=sys.argv): |
|---|
| 21 | path = len(args) > 1 and args[1] |
|---|
| 22 | n = len(args) > 2 and int(args[2]) or 3 |
|---|
| 23 | env = Environment(path) |
|---|
| 24 | |
|---|
| 25 | try: |
|---|
| 26 | cnx = env.get_db_cnx() |
|---|
| 27 | cursor = cnx.cursor() |
|---|
| 28 | cursor.execute("CREATE TABLE test(value integer)") |
|---|
| 29 | cursor.executemany("INSERT INTO test (value) VALUES (%s)", |
|---|
| 30 | [(1,)]*10000) |
|---|
| 31 | cnx.commit() |
|---|
| 32 | except: |
|---|
| 33 | print 'table exists' |
|---|
| 34 | |
|---|
| 35 | threads = [None]*n |
|---|
| 36 | while True: |
|---|
| 37 | for i in range(0, n): |
|---|
| 38 | t = threads[i] |
|---|
| 39 | if t and t.isAlive(): |
|---|
| 40 | t.join(0.1) |
|---|
| 41 | else: |
|---|
| 42 | t = threading.Thread(target=run, args=(i, env,)) |
|---|
| 43 | print 'start', t.start() |
|---|
| 44 | threads[i] = t |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | def run(i, env): |
|---|
| 48 | sum = 0 |
|---|
| 49 | r = None |
|---|
| 50 | for loop in range(0, 5): |
|---|
| 51 | # acquire resource |
|---|
| 52 | try: |
|---|
| 53 | # -- test interference with repository cache |
|---|
| 54 | print |
|---|
| 55 | print i, 'implicit acquire - repos...', |
|---|
| 56 | repos = env.get_repository() |
|---|
| 57 | print repos.youngest_rev |
|---|
| 58 | # -- (program works fine without the above block) |
|---|
| 59 | |
|---|
| 60 | print i, 'acquiring...', |
|---|
| 61 | cnx = env.get_db_cnx() |
|---|
| 62 | print i, 'acquired' |
|---|
| 63 | |
|---|
| 64 | # -- do some stuff with the connection |
|---|
| 65 | cursor = cnx.cursor() |
|---|
| 66 | cursor.execute("SELECT * FROM test") |
|---|
| 67 | for v, in cursor: |
|---|
| 68 | sum += v |
|---|
| 69 | print '[select query', i, '=>', sum, ']' |
|---|
| 70 | |
|---|
| 71 | cursor.execute("INSERT INTO test (value) VALUES (%s)", (333,)) |
|---|
| 72 | print '[insert query]', i |
|---|
| 73 | cnx.commit() |
|---|
| 74 | print '[commit]', i |
|---|
| 75 | print |
|---|
| 76 | print |
|---|
| 77 | # release resource |
|---|
| 78 | # if cnx: |
|---|
| 79 | # del cnx |
|---|
| 80 | except Exception, e: |
|---|
| 81 | print e |
|---|
| 82 | print i, '*'*40, 'oops, timeout reached...' |
|---|
| 83 | # sleep a while |
|---|
| 84 | time.sleep(0.1) |
|---|
| 85 | print '==='*10, 'done with', i |
|---|
| 86 | env.shutdown(threading._get_ident()) |
|---|
| 87 | |
|---|
| 88 | |
|---|
| 89 | if __name__ == '__main__': |
|---|
| 90 | main() |
|---|