Ticket #637: ticket637.py

File ticket637.py, 3.4 KB (added by Olly Betts, 11 years ago)

modified test script

Line 
1#!/usr/bin/python
2import os
3import simplejson as json
4import xapian as x
5import shutil
6import gc
7
8def make_db(path, num_docs=100000):
9 try:
10 shutil.rmtree(path)
11 except OSError, e:
12 if e.errno != 2:
13 raise
14
15 db = x.WritableDatabase(path, x.DB_CREATE)
16 for i in xrange(1, num_docs):
17 doc = x.Document()
18 doc.set_data(json.dumps({ 'id': i, 'enabled': True }))
19 doc.add_term('XTYPA')
20 db.add_document(doc)
21 return db
22
23def run_query(db, num_docs=100000):
24 e = x.Enquire(db)
25 e.set_query(x.Query('XTYPA'))
26 m = e.get_mset(0, num_docs, True, None)
27
28 # Store the MSetItem's data, which causes a memory leak
29 data = []
30 for i in m:
31 data.append({ 'data': i.document.get_data(), 'id': i.docid, })
32
33 # Make sure I'm not crazy
34 del num_docs, db, i, e, m, data
35 gc.collect()
36
37def main():
38 # print the PID to monitor
39 print 'PID to monitor: {}'.format(os.getpid())
40
41 db = make_db('ticket637.db')
42 raw_input("database is done, ready?")
43
44 gc.collect()
45 gc.collect()
46 gc.collect()
47 gc.collect()
48 print "num objects before = ", len(gc.get_objects())
49 run_query(db, 100000)
50 print "num objects after = ", len(gc.get_objects())
51 raw_input('done?')
52
53if __name__ == '__main__':
54 main()