Salome HOME
739376a8eef52624812a0dc3b9c56fefc16e2941
[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 glob
29 import pickle
30 import tempfile
31 import logging
32
33 def flush():
34     import sys
35     sys.stdout.flush()
36     sys.stderr.flush()
37
38 def unProxyfy( objs ):
39     """
40     objs is a list of SenderByte
41     """
42     ret = []
43     fileNamesProxyOut = []
44     for ret2 in objs:
45         ret3 = pickle.loads( SALOME_PyNode.SeqByteReceiver(ret2).data() )
46         ret4 = ret3
47         fileNameProxyOut = None
48         if SALOME_PyNode.GetBigObjectOnDiskThreshold() != -1:
49             if isinstance( ret3, SALOME_PyNode.BigObjectOnDiskBase ):
50                 ret4 = ret3.get()
51             else:
52                 raise RuntimeError("Oooops")
53             ret3.unlinkOnDestructor()
54             fileNameProxyOut = ret3.getFileName()
55         logging.debug("Prxy file : {}".format( fileNameProxyOut ))
56         ret.append( ret3 )
57         fileNamesProxyOut.append( fileNameProxyOut )
58     return ret, fileNamesProxyOut
59
60 class testPerfLogManager1(unittest.TestCase):
61     def testPerfLogManager0(self):
62         """
63         [EDF29150] : This test checks measure performance methods
64         """
65         hostname = "localhost"
66         cp = pylauncher.GetRequestForGiveContainer(hostname,"container_test")
67         #PROXY_THRES = "-1"
68         PROXY_THRES = "1"
69         with tempfile.TemporaryDirectory() as tmpdirnameMonitoring:
70             monitoringFile = os.path.join( str( tmpdirnameMonitoring ), "zeHtop.pckl" )
71             monitoringFileTwo = os.path.join( str( tmpdirnameMonitoring ), "zeHtopTwo.pckl" )
72             logging.debug("Monitoring file : {}".format(monitoringFile))
73             with tempfile.TemporaryDirectory() as tmpdirname:
74                 with salome.LogManagerLaunchMonitoringFileCtxMgr(250,monitoringFile) as monitoringParams:
75                     pyFileContainingCodeOfMonitoring = monitoringParams.pyFileName.filename
76                     logging.debug("Python file containing code of monitoring : {}".format(pyFileContainingCodeOfMonitoring))
77                     val_for_big_obj = str( tmpdirname )
78                     os.environ["SALOME_FILE_BIG_OBJ_DIR"] = val_for_big_obj
79                     # Override environement for all containers launched
80                     salome.cm.SetOverrideEnvForContainersSimple(env = [("SALOME_FILE_BIG_OBJ_DIR",val_for_big_obj),("SALOME_BIG_OBJ_ON_DISK_THRES",PROXY_THRES)])
81                     salome.cm.SetDeltaTimeBetweenCPUMemMeasureInMilliSecond( 250 )
82                     cont = salome.cm.GiveContainer(cp)
83                     logging.debug("{} {}".format(40*"*",cont.getPID()))
84                     script_st = """
85 import logging
86 import sys
87 import KernelBasis
88 cst = KernelBasis.GetTimeAdjustmentCst()
89 logging.debug("constant = {}".format(cst))
90 nbcore = 3
91 print("coucou {} {}".format(len(zeinput0),len(zeinput1)))
92 logging.debug("debug or not debug")
93 ob = [ [ bytes(3000000) ] ]
94 pihm, ts = KernelBasis.HeatMarcel(1 * nbcore * cst,nbcore)
95 print("Time ellapse spent : {} s".format(ts))
96 ob2 = [ [ bytes(100000) ] ]
97 pihm, ts = KernelBasis.HeatMarcel(1 * nbcore * cst,nbcore)
98 print("Time ellapse spent : {} s".format(ts))
99 sys.stderr.write("fake error message\\n")
100 """
101                     poa = salome.orb.resolve_initial_references("RootPOA")
102                     zeinput0 = [ bytes(100000000) ]
103                     if SALOME_PyNode.GetBigObjectOnDiskThreshold() != -1:
104                         zeinput0 = SALOME_PyNode.ProxyfyPickeled( zeinput0 )
105                         zeinput0.unlinkOnDestructor()
106                     obj = SALOME_PyNode.SenderByte_i(poa,pickle.dumps( (["zeinput0"],{"zeinput0": [zeinput0], "zeinput1": [ [zeinput0], [zeinput0] ] }) ))
107                     id_o = poa.activate_object(obj)
108                     refPtr = poa.id_to_reference(id_o)
109                     pyscript2 = cont.createPyScriptNode("testScript2",script_st)
110                     pyscript2.executeFirst(refPtr)
111                     ret2 = pyscript2.executeSecond(["ob","ob2"])# generate a DeprecationWarning: PY_SSIZE_T_CLEAN will be required for '#' formats on debian11 ?
112                     ret3, fileNamesProxyOut = unProxyfy( ret2 )
113                     logging.getLogger().debug("test logging 1")
114                     logging.debug("test logging 2")
115                     logging.debug( salome.orb.object_to_string( salome.logm ) )
116                     a = salome.logm.NaiveFetch()
117                     logging.debug(a)
118                     logging.debug(a[0][1][0])
119                     logging.debug( a[0][1][0].get()._input_hdd_mem._data[0]._data[0]._hdd_mem ) # important
120                     logging.debug( a[0][1][0].get()._input_hdd_mem._data[1]._data[0]._data[0]._hdd_mem ) # important
121                     fileNameProxyIn = a[0][1][0].get()._input_hdd_mem._data[0]._data[0]._file_name
122                     logging.debug( fileNameProxyIn )
123                     del zeinput0
124                     del ret3
125                     import gc ; gc.collect()
126                     if fileNameProxyIn is not None:
127                         if os.path.exists(fileNameProxyIn):
128                             raise RuntimeError("Oooops 2")
129                     for fileNameProxyOut in fileNamesProxyOut:
130                         if fileNameProxyOut is not None:
131                             if os.path.exists(fileNameProxyOut):
132                                 raise RuntimeError("Oooops 3")
133                     # execution #2 inside last
134                     script_st2 = """
135 import logging
136 b = 7+a
137 logging.debug("Execution 2")
138 import time
139 time.sleep(1)
140 """
141                     obj2 = SALOME_PyNode.SenderByte_i(poa,pickle.dumps((["a"],{"a":3})))
142                     id2_o = poa.activate_object(obj2)
143                     refPtr2 = poa.id_to_reference(id2_o)
144                     pyscript2.assignNewCompiledCode(script_st2)
145                     pyscript2.executeFirst(refPtr2)
146                     ret2_0 = pyscript2.executeSecond(["b"])
147                     ret2_1, fileNamesProxyOut2 = unProxyfy( ret2_0 )
148                     logging.debug( fileNamesProxyOut2 )
149                     a = salome.logm.NaiveFetch()
150                     del ret2_1
151                     import gc ; gc.collect()
152                     for fileNameProxyOut in fileNamesProxyOut2:
153                         if fileNameProxyOut is not None:
154                             if os.path.exists(fileNameProxyOut):
155                                 raise RuntimeError("Oooops 3")
156                     #
157                     fname = os.path.join(str( tmpdirname ),"perf.log")
158                     salome.logm.DumpInFile( fname )
159                     logManagerInst0 = salome.logm.LoadFromFile( fname )
160                     logging.debug( logManagerInst0[0][1][0].get()._input_hdd_mem._data[1]._data[0]._data[0]._hdd_mem ) # important
161                     logManagerInst = salome.logm.Fetch(True)
162                     logManagerInst2 = salome.logm.Fetch(True)
163                     salome.logm.putStructInFileAtomic(False,monitoringFileTwo)
164                     logging.debug( salome.logm.LoadFromFile(monitoringFileTwo)[0][1][0].get() )
165                     logging.debug( logManagerInst[0][1][0].get()._input_hdd_mem._data[1]._data[0]._data[0]._hdd_mem ) # important
166                     self.assertTrue( logManagerInst2[0][1][0].get() is None )
167                     self.assertTrue( logManagerInst[0][1][1].get()._output_hdd_mem._data[0]._file_name == fileNamesProxyOut2[0] )
168                     logging.debug( logManagerInst[0][1][1].log() )
169                     # 2 files because a backup file is stored in case of unexpected kill during 
170                     self.assertEqual( len( glob.glob("{}*".format(monitoringFile) ) ) , 2 )
171                     # leaving MonitoringFile Manager -> backup file is killed
172                     pass
173                 #self.assertEqual(monitoringFileSafe, monitoringFile)
174                 self.assertEqual( len( glob.glob("{}*".format(monitoringFile) ) ) , 1 )
175                 logging.debug( salome.logm.LoadFromFile(monitoringFile)[0][1][0].get() )
176                 del monitoringParams
177                 import gc ; gc.collect()
178                 self.assertFalse( os.path.exists(pyFileContainingCodeOfMonitoring) )
179                 cont.Shutdown()
180
181 if __name__ == '__main__':
182     from salome_utils import positionVerbosityOfLoggerRegardingState,setVerboseLevel,setVerbose
183     salome.standalone()
184     salome.salome_init()
185     setVerboseLevel(logging.DEBUG)
186     positionVerbosityOfLoggerRegardingState()
187     unittest.main()