Salome HOME
[EDF30356] : Extend management of maximum_time attribute format from pylauncher to...
[modules/kernel.git] / src / Container / Test / testcontainer.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
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 from os import getcwd
23 from Engines import ContainerParameters, ResourceParameters
24 import SALOME
25 import salome
26
27 from time import sleep
28
29 class TestResourceManager(unittest.TestCase):
30     def getContainer(self, name):
31         rp = ResourceParameters(name="localhost",
32                                 hostname="localhost",
33                                 can_launch_batch_jobs=False,
34                                 can_run_containers=True,
35                                 OS="Linux",
36                                 componentList=[],
37                                 nb_proc=1,
38                                 mem_mb=1000,
39                                 cpu_clock=1000,
40                                 nb_node=1,
41                                 nb_proc_per_node=1,
42                                 policy="first",
43                                 resList=[])
44         cp = ContainerParameters(container_name=name,
45                                  mode="start",
46                                  workingdir=getcwd(),
47                                  nb_proc=1,
48                                  isMPI=False,
49                                  parallelLib="",
50                                  resource_params=rp)
51         cm = salome.naming_service.Resolve("/ContainerManager")
52         return cm.GiveContainer(cp)
53
54     def checkLoads(self, cont, loads):
55         self.assertEqual(len(loads), cont.getNumberOfCPUCores())
56         for load in loads:
57             self.assertTrue(0.0 <= load <= 1.0)
58
59     def test1(self):
60         # Check custom script
61         cont = self.getContainer("test_container_2")
62         import multiprocessing as mp
63         ref_load = [min(0.1*(i+1),1.0) for i in range(mp.cpu_count())]
64         cont.setPyScriptForCPULoad('cpu_loads = {}'.format(ref_load))
65         loads1 = cont.loadOfCPUCores()
66         self.assertEqual(loads1, ref_load)
67         cont.resetScriptForCPULoad()
68         loads2 = cont.loadOfCPUCores()
69         self.checkLoads(cont, loads2)
70         cont.Shutdown()
71
72     def test2(self):
73         # Check bad script
74         cont = self.getContainer("test_container_3")
75         cont.setPyScriptForCPULoad("bla-bla-bla")
76         self.assertRaises(Exception, cont.loadOfCPUCores)
77         cont.Shutdown()
78
79     def test3(self):
80         # check memory sizes
81         cont = self.getContainer("test_container_4")
82         memory_total = cont.getTotalPhysicalMemory()
83         memory_in_use = cont.getTotalPhysicalMemoryInUse()
84         memory_by_me = cont.getTotalPhysicalMemoryInUseByMe()
85         self.assertGreater(memory_total, memory_in_use)
86         self.assertGreater(memory_in_use, memory_by_me)
87         cont.Shutdown()
88     
89     def test4(self):
90         """
91         Test checking memory consumption of container
92         """
93         cont = self.getContainer("test_container_5")
94         memory_by_me_start = cont.getTotalPhysicalMemoryInUseByMe()
95         import pickle
96         psn = cont.createPyScriptNode("n","""b = bytearray(10485760)""")# create 10MB byte array abroad
97         psn.execute([],pickle.dumps(((),{})))
98         memory_by_me_end = cont.getTotalPhysicalMemoryInUseByMe()
99         self.assertGreater(memory_by_me_end,memory_by_me_start)
100         self.assertIn(memory_by_me_end-memory_by_me_start,[9,10,11,12])# test elevation of memory
101         cont.Shutdown()
102
103 if __name__ == '__main__':
104     salome.standalone()
105     salome.salome_init()
106     unittest.main()