#!/usr/bin/env python
#
# Tests the short-circuiting in the transaction manager.
# Previously, the list of transactions were pruned after
# the last endModifyCmd() was called, which resulted in
# non-linear scaling w.r.t. to the amount of commands in the
# transaction. This test makes sure that the first 10%
# of the commands used as much time as the last 10% of the commands.
#
#
#

import coral
import time

L = coral.lang.UML14

m = L.Model()

t_times = []

N_SAMPLES = 1000

TM = coral.getTransactionManager()
TM.beginModifyCmd()
try:
    for i in xrange(N_SAMPLES):
        t_start = time.time()
        m.ownedElement.add(L.Class())
        t_end = time.time()
        t_times.append(t_end - t_start)
finally:
    TM.endModifyCmd()

first_tenths = sum(t_times[:N_SAMPLES // 10])
last_tenths = sum(t_times[N_SAMPLES-N_SAMPLES // 10:])

max_percentage_diff = 15

print first_tenths
print last_tenths

assert first_tenths < last_tenths * (1.0 + max_percentage_diff / 100.0)
assert last_tenths < first_tenths * (1.0 + max_percentage_diff / 100.0)

