#!/usr/bin/env python
#
# For some reason the coral core loads a model
# relatively fast, but loading the same model
# in the GUI is slow. This acceptance test
# checks the speed and fails if the GUI is more than
# X percent slower
#

X = 40

filename = "model_hierarchy_with_19531_packages.xmi"

import os
import time
import coral

#
# This is a dummy loading of the model.
# IT IS IMPORTANT!
# It brings the xmi file into the system's cache.
# Without it, it is usually the case that t_core > t_gui ;-)
#
p = coral.loadDocument(filename)
p.invalidate()

#
# Time loading of model
#
t_core_start = time.time()
p = coral.loadDocument(filename)
t_core_end = time.time()
p.invalidate()

t_core = t_core_end - t_core_start

from coral.modeler import modeler, action
mw=modeler.ModelerWindow()
#
# Important! Process QT events and display
# the window so that this time is not included
# into the timing below
#
mw.processEvents()

#
# Time loading of model into GUI
#
t_gui_start = time.time()
action.openProject(filename)
t_gui_end = time.time()

t_gui = t_gui_end - t_gui_start

print "CORE: ", t_core
print "GUI: ", t_gui
assert t_core * (1 + float(X)/100) >= t_gui
assert t_core < t_gui
