2014-04-03 17:37:07 +00:00
|
|
|
"""
|
|
|
|
PyQt/PySide stress test:
|
|
|
|
|
|
|
|
Create lots of random widgets and graphics items, connect them together randomly,
|
|
|
|
the tear them down repeatedly.
|
|
|
|
|
|
|
|
The purpose of this is to attempt to generate segmentation faults.
|
|
|
|
"""
|
2015-07-18 13:38:34 +00:00
|
|
|
from pyqtgraph.Qt import QtTest
|
2014-04-03 17:37:07 +00:00
|
|
|
import pyqtgraph as pg
|
2014-04-14 02:51:54 +00:00
|
|
|
from random import seed, randint
|
|
|
|
import sys, gc, weakref
|
|
|
|
|
|
|
|
app = pg.mkQApp()
|
|
|
|
|
|
|
|
seed(12345)
|
|
|
|
|
|
|
|
widgetTypes = [
|
|
|
|
pg.PlotWidget,
|
|
|
|
pg.ImageView,
|
|
|
|
pg.GraphicsView,
|
|
|
|
pg.QtGui.QWidget,
|
|
|
|
pg.QtGui.QTreeWidget,
|
|
|
|
pg.QtGui.QPushButton,
|
|
|
|
]
|
|
|
|
|
|
|
|
itemTypes = [
|
|
|
|
pg.PlotCurveItem,
|
|
|
|
pg.ImageItem,
|
|
|
|
pg.PlotDataItem,
|
|
|
|
pg.ViewBox,
|
|
|
|
pg.QtGui.QGraphicsRectItem
|
|
|
|
]
|
|
|
|
|
|
|
|
widgets = []
|
|
|
|
items = []
|
2015-12-24 15:38:31 +00:00
|
|
|
allWidgets = weakref.WeakKeyDictionary()
|
2014-04-14 02:51:54 +00:00
|
|
|
|
2014-04-27 18:02:55 +00:00
|
|
|
|
|
|
|
def crashtest():
|
2014-04-14 02:51:54 +00:00
|
|
|
global allWidgets
|
|
|
|
try:
|
|
|
|
gc.disable()
|
|
|
|
actions = [
|
2014-04-14 02:59:57 +00:00
|
|
|
createWidget,
|
|
|
|
#setParent,
|
|
|
|
forgetWidget,
|
|
|
|
showWidget,
|
2014-04-15 19:10:04 +00:00
|
|
|
processEvents,
|
2014-04-14 02:51:54 +00:00
|
|
|
#raiseException,
|
|
|
|
#addReference,
|
|
|
|
]
|
|
|
|
|
|
|
|
thread = WorkThread()
|
2014-04-15 19:10:04 +00:00
|
|
|
thread.start()
|
2014-04-14 02:51:54 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
try:
|
|
|
|
action = randItem(actions)
|
|
|
|
action()
|
2014-04-15 19:10:04 +00:00
|
|
|
print('[%d widgets alive, %d zombie]' % (len(allWidgets), len(allWidgets) - len(widgets)))
|
2014-04-14 02:51:54 +00:00
|
|
|
except KeyboardInterrupt:
|
2014-04-15 19:10:04 +00:00
|
|
|
print("Caught interrupt; send another to exit.")
|
|
|
|
try:
|
|
|
|
for i in range(100):
|
2015-07-18 13:38:34 +00:00
|
|
|
QtTest.QTest.qWait(100)
|
2014-04-15 19:10:04 +00:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
thread.terminate()
|
|
|
|
break
|
2014-04-14 02:51:54 +00:00
|
|
|
except:
|
|
|
|
sys.excepthook(*sys.exc_info())
|
|
|
|
finally:
|
|
|
|
gc.enable()
|
2014-04-03 17:37:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
2014-04-14 02:51:54 +00:00
|
|
|
class WorkThread(pg.QtCore.QThread):
|
|
|
|
'''Intended to give the gc an opportunity to run from a non-gui thread.'''
|
|
|
|
def run(self):
|
|
|
|
i = 0
|
|
|
|
while True:
|
|
|
|
i += 1
|
|
|
|
if (i % 1000000) == 0:
|
|
|
|
print('--worker--')
|
|
|
|
|
2014-04-03 17:37:07 +00:00
|
|
|
|
2014-04-14 02:51:54 +00:00
|
|
|
def randItem(items):
|
|
|
|
return items[randint(0, len(items)-1)]
|
2014-04-03 17:37:07 +00:00
|
|
|
|
2014-04-14 02:51:54 +00:00
|
|
|
def p(msg):
|
|
|
|
print(msg)
|
|
|
|
sys.stdout.flush()
|
2014-04-03 17:37:07 +00:00
|
|
|
|
2014-04-14 02:51:54 +00:00
|
|
|
def createWidget():
|
|
|
|
p('create widget')
|
|
|
|
global widgets, allWidgets
|
2014-04-15 19:10:04 +00:00
|
|
|
if len(widgets) > 50:
|
|
|
|
return
|
2014-04-14 02:51:54 +00:00
|
|
|
widget = randItem(widgetTypes)()
|
2014-04-15 19:10:04 +00:00
|
|
|
widget.setWindowTitle(widget.__class__.__name__)
|
2014-04-14 02:51:54 +00:00
|
|
|
widgets.append(widget)
|
2015-12-24 15:41:17 +00:00
|
|
|
allWidgets[widget] = 1
|
2014-04-14 02:51:54 +00:00
|
|
|
p(" %s" % widget)
|
|
|
|
return widget
|
|
|
|
|
|
|
|
def setParent():
|
|
|
|
p('set parent')
|
|
|
|
global widgets
|
|
|
|
if len(widgets) < 2:
|
|
|
|
return
|
|
|
|
child = parent = None
|
|
|
|
while child is parent:
|
|
|
|
child = randItem(widgets)
|
|
|
|
parent = randItem(widgets)
|
|
|
|
p(" %s parent of %s" % (parent, child))
|
|
|
|
child.setParent(parent)
|
|
|
|
|
|
|
|
def forgetWidget():
|
|
|
|
p('forget widget')
|
|
|
|
global widgets
|
|
|
|
if len(widgets) < 1:
|
|
|
|
return
|
|
|
|
widget = randItem(widgets)
|
|
|
|
p(' %s' % widget)
|
|
|
|
widgets.remove(widget)
|
|
|
|
|
|
|
|
def showWidget():
|
|
|
|
p('show widget')
|
|
|
|
global widgets
|
|
|
|
if len(widgets) < 1:
|
|
|
|
return
|
|
|
|
widget = randItem(widgets)
|
|
|
|
p(' %s' % widget)
|
|
|
|
widget.show()
|
|
|
|
|
|
|
|
def processEvents():
|
|
|
|
p('process events')
|
2015-07-18 13:38:34 +00:00
|
|
|
QtTest.QTest.qWait(25)
|
2014-04-14 02:51:54 +00:00
|
|
|
|
2014-04-27 18:02:55 +00:00
|
|
|
class TstException(Exception):
|
2014-04-14 02:51:54 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def raiseException():
|
|
|
|
p('raise exception')
|
2014-04-27 18:02:55 +00:00
|
|
|
raise TstException("A test exception")
|
2014-04-14 02:51:54 +00:00
|
|
|
|
|
|
|
def addReference():
|
|
|
|
p('add reference')
|
|
|
|
global widgets
|
|
|
|
if len(widgets) < 1:
|
|
|
|
return
|
|
|
|
obj1 = randItem(widgets)
|
|
|
|
obj2 = randItem(widgets)
|
|
|
|
p(' %s -> %s' % (obj1, obj2))
|
|
|
|
obj1._testref = obj2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2015-07-18 13:38:34 +00:00
|
|
|
test_stability()
|