testSimulationPerformance.py 4.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
# -*- coding: utf-8 -*-
##############################################################################
# Copyright (c) 2012 Nexedi SA and Contributors. All Rights Reserved.
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsibility of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# guarantees and support are strongly advised to contract a Free Software
# Service Company
#
# This program is Free Software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
##############################################################################

import subprocess
import unittest
from test import pystone
from time import time
pystone.clock = time
from Products.ERP5Type.tests.runUnitTest import ERP5TypeTestLoader
from Products.ERP5.tests.testTradeModelLine import TestTradeModelLineSale

ERP5TypeTestLoader._testMethodPrefix = 'perf'

class TestSimulationPerformance(TestTradeModelLineSale):

  def getTitle(self):
    return "Simulation Performance"

  def afterSetUp(self):
    super(TestSimulationPerformance, self).afterSetUp()
    subprocess.call('sync')

  def runAlarms(self):
    for alarm in self.portal.portal_alarms.objectValues():
      if alarm.isEnabled():
        endswith = alarm.getId().endswith
        if endswith('_builder_alarm'):
          alarm.activeSense()
    self.tic()

  def perf_00_setupAndFillCache(self):
    self.test_01_OrderWithSimpleTaxedAndDiscountedLines()
    self.__class__._order = self['order'].getRelativeUrl()
    self.runAlarms()
    print "\n%s pystones/second" % pystone.pystones()[1]

  def perf_01_invoiceSimpleOrder(self, order_count=1):
    start = time()
    order = self.portal.unrestrictedTraverse(self._order)
    order_list = [self.clone(order) for _ in xrange(order_count)]
    for order in order_list:
      for line in list(order.getMovementList()):
        self.clone(line)
    self.tic()
    for order in order_list:
      order.order()
71
      self.commit()
72 73 74
    self.tic()
    for order in order_list:
      order.confirm()
75
      self.commit()
76 77 78 79 80 81 82 83 84
    self.tic()

    self.runAlarms()
    packing_list_list = sum((order.getCausalityRelatedValueList(
        portal_type=self.packing_list_portal_type)
      for order in order_list), [])

    for packing_list in packing_list_list:
      self.packPackingList(packing_list)
85
      self.commit()
86 87 88
    self.tic()
    for packing_list in packing_list_list:
      packing_list.start()
89
      self.commit()
90 91 92
    self.tic()
    for packing_list in packing_list_list:
      packing_list.stop()
93
      self.commit()
94 95 96 97 98 99 100 101 102
    self.tic()

    self.runAlarms()
    invoice_list = sum((packing_list.getCausalityRelatedValueList(
        portal_type=self.invoice_portal_type)
      for packing_list in packing_list_list), [])

    for packing_list in packing_list_list:
      packing_list.deliver()
103
      self.commit()
104 105 106
    self.tic()
    for invoice in invoice_list:
      invoice.start()
107
      self.commit()
108 109 110
    self.tic()
    for invoice in invoice_list:
      invoice.stop()
111
      self.commit()
112 113 114
    self.tic()
    for invoice in invoice_list:
      invoice.deliver()
115
      self.commit()
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
    self.tic()

    self.runAlarms()
    end = time()
    print "\n%s took %.4gs (%s order(s))" % (self._testMethodName,
                                             end - start, order_count)

  def perf_02_invoiceManySimpleOrders(self):
    self.perf_01_invoiceSimpleOrder(10)


def test_suite():
  suite = unittest.TestSuite()
  suite.addTest(unittest.makeSuite(TestSimulationPerformance))
  return suite