Salome HOME
[EDF29150] : log performance of python scripts run inside SALOME container + verbosit...
[modules/kernel.git] / src / Launcher / Test / testPerfLogManager1.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2024  CEA/DEN, EDF R&D
3 #
4 # This library is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU Lesser General Public
6 # License as published by the Free Software Foundation; either
7 # version 2.1 of the License, or (at your option) any later version.
8 #
9 # This library is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12 # Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public
15 # License along with this library; if not, write to the Free Software
16 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 #
20
21 import unittest
22 import os
23 import salome
24 import Engines
25 import pylauncher
26 import SALOME_PyNode
27
28 import pickle
29 import tempfile
30 import logging
31
32 def flush():
33     import sys
34     sys.stdout.flush()
35     sys.stderr.flush()
36
37 def unProxyfy( objs ):
38     """
39     objs is a list of SenderByte
40     """
41     ret = []
42     fileNamesProxyOut = []
43     for ret2 in objs:
44         ret3 = pickle.loads( SALOME_PyNode.SeqByteReceiver(ret2).data() )
45         ret4 = ret3
46         fileNameProxyOut = None
47         if SALOME_PyNode.GetBigObjectOnDiskThreshold() != -1:
48             if isinstance( ret3, SALOME_PyNode.BigObjectOnDiskBase ):
49                 ret4 = ret3.get()
50             else:
51                 raise RuntimeError("Oooops")
52             ret3.unlinkOnDestructor()
53             fileNameProxyOut = ret3.getFileName()
54         logging.debug("Prxy file : {}".format( fileNameProxyOut ))
55         ret.append( ret3 )
56         fileNamesProxyOut.append( fileNameProxyOut )
57     return ret, fileNamesProxyOut
58
59 class testPerfLogManager1(unittest.TestCase):
60     def testPerfLogManager0(self):
61         """
62         [EDF29150] : This test checks measure performance methods
63         """
64         hostname = "localhost"
65         cp = pylauncher.GetRequestForGiveContainer(hostname,"container_test")
66         #PROXY_THRES = "-1"
67         PROXY_THRES = "1"
68         with tempfile.TemporaryDirectory() as tmpdirname:
69             val_for_big_obj = str( tmpdirname )
70             # Override environement for all containers launched
71             salome.cm.SetOverrideEnvForContainersSimple(env = [("SALOME_FILE_BIG_OBJ_DIR",val_for_big_obj),("SALOME_BIG_OBJ_ON_DISK_THRES",PROXY_THRES)])
72             salome.cm.SetDeltaTimeBetweenCPUMemMeasureInMilliSecond( 250 )
73             cont = salome.cm.GiveContainer(cp)
74             logging.debug("{} {}".format(40*"*",cont.getPID()))
75             script_st = """
76 import logging
77 import sys
78 import KernelBasis
79 cst = KernelBasis.GetTimeAdjustmentCst()
80 logging.debug("constant = {}".format(cst))
81 nbcore = 3
82 print("coucou {} {}".format(len(zeinput0),len(zeinput1)))
83 logging.debug("debug or not debug")
84 ob = [ [ bytes(3000000) ] ]
85 pihm, ts = KernelBasis.HeatMarcel(1 * nbcore * cst,nbcore)
86 print("Time ellapse spent : {} s".format(ts))
87 ob2 = [ [ bytes(100000) ] ]
88 pihm, ts = KernelBasis.HeatMarcel(1 * nbcore * cst,nbcore)
89 print("Time ellapse spent : {} s".format(ts))
90 sys.stderr.write("fake error message\\n")
91 """
92             poa = salome.orb.resolve_initial_references("RootPOA")
93             zeinput0 = [ bytes(100000000) ]
94             if SALOME_PyNode.GetBigObjectOnDiskThreshold() != -1:
95                 zeinput0 = SALOME_PyNode.ProxyfyPickeled( zeinput0 )
96                 zeinput0.unlinkOnDestructor()
97             obj = SALOME_PyNode.SenderByte_i(poa,pickle.dumps( (["zeinput0"],{"zeinput0": [zeinput0], "zeinput1": [ [zeinput0], [zeinput0] ] }) ))
98             id_o = poa.activate_object(obj)
99             refPtr = poa.id_to_reference(id_o)
100             pyscript2 = cont.createPyScriptNode("testScript2",script_st)
101             pyscript2.executeFirst(refPtr)
102             ret2 = pyscript2.executeSecond(["ob","ob2"])# generate a DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats on debian11 ?
103             ret3, fileNamesProxyOut = unProxyfy( ret2 )
104             logging.getLogger().debug("test logging 1")
105             logging.debug("test logging 2")
106             logging.debug( salome.orb.object_to_string( salome.logm ) )
107             a = salome.logm.NaiveFetch()
108             logging.debug(a)
109             logging.debug(a[0][1][0])
110             logging.debug( a[0][1][0].get()._input_hdd_mem._data[0]._data[0]._hdd_mem ) # important
111             logging.debug( a[0][1][0].get()._input_hdd_mem._data[1]._data[0]._data[0]._hdd_mem ) # important
112             fileNameProxyIn = a[0][1][0].get()._input_hdd_mem._data[0]._data[0]._file_name
113             logging.debug( fileNameProxyIn )
114             del zeinput0
115             del ret3
116             import gc ; gc.collect()
117             if fileNameProxyIn is not None:
118                 if os.path.exists(fileNameProxyIn):
119                     raise RuntimeError("Oooops 2")
120             for fileNameProxyOut in fileNamesProxyOut:
121                 if fileNameProxyOut is not None:
122                     if os.path.exists(fileNameProxyOut):
123                         raise RuntimeError("Oooops 3")
124
125             # execution #2 inside last
126             script_st2 = """
127 import logging
128 b = 7+a
129 logging.debug("Execution 2")
130 import time
131 time.sleep(1)
132 """
133             obj2 = SALOME_PyNode.SenderByte_i(poa,pickle.dumps((["a"],{"a":3})))
134             id2_o = poa.activate_object(obj2)
135             refPtr2 = poa.id_to_reference(id2_o)
136             pyscript2.assignNewCompiledCode(script_st2)
137             pyscript2.executeFirst(refPtr2)
138             ret2_0 = pyscript2.executeSecond(["b"])
139             ret2_1, fileNamesProxyOut2 = unProxyfy( ret2_0 )
140             logging.debug( fileNamesProxyOut2 )
141             a = salome.logm.NaiveFetch()
142             del ret2_1
143             import gc ; gc.collect()
144             for fileNameProxyOut in fileNamesProxyOut2:
145                 if fileNameProxyOut is not None:
146                     if os.path.exists(fileNameProxyOut):
147                         raise RuntimeError("Oooops 3")
148             #
149             fname = os.path.join(str( tmpdirname ),"perf.log")
150             salome.logm.DumpInFile( fname )
151             logManagerInst0 = salome.logm.LoadFromFile( fname )
152             logging.debug( logManagerInst0[0][1][0].get()._input_hdd_mem._data[1]._data[0]._data[0]._hdd_mem ) # important
153             logManagerInst = salome.logm.Fetch(True)
154             logManagerInst2 = salome.logm.Fetch(True)
155             logging.debug( logManagerInst[0][1][0].get()._input_hdd_mem._data[1]._data[0]._data[0]._hdd_mem ) # important
156             self.assertTrue( logManagerInst2[0][1][0].get() is None )
157             self.assertTrue( logManagerInst[0][1][1].get()._output_hdd_mem._data[0]._file_name == fileNamesProxyOut2[0] )
158             logging.debug( logManagerInst[0][1][1].log() )
159             cont.Shutdown()
160
161 if __name__ == '__main__':
162     from salome_utils import positionVerbosityOfLoggerRegardingState,setVerboseLevel,setVerbose
163     salome.standalone()
164     salome.salome_init()
165     setVerboseLevel(logging.DEBUG)
166     positionVerbosityOfLoggerRegardingState()
167     unittest.main()