1 # -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2022 CEA/DEN, EDF R&D, OPEN CASCADE
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.
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.
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
18 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 from Engines import ContainerParameters, ResourceParameters
27 from time import sleep
29 class TestResourceManager(unittest.TestCase):
30 def getContainer(self, name):
31 rp = ResourceParameters(name="localhost",
33 can_launch_batch_jobs=False,
34 can_run_containers=True,
44 cp = ContainerParameters(container_name=name,
51 cm = salome.naming_service.Resolve("/ContainerManager")
52 return cm.GiveContainer(cp)
54 def checkLoads(self, cont, loads):
55 self.assertEqual(len(loads), cont.getNumberOfCPUCores())
57 self.assertTrue(0.0 <= load <= 1.0)
60 # Check loadOfCPUCores
61 cont = self.getContainer("test_container_1")
62 loads1 = cont.loadOfCPUCores()
63 self.checkLoads(cont, loads1)
65 loads2 = cont.loadOfCPUCores()
66 self.checkLoads(cont, loads2)
67 self.assertNotEqual(loads1, loads2)
72 cont = self.getContainer("test_container_2")
73 import multiprocessing as mp
74 ref_load = [min(0.1*(i+1),1.0) for i in range(mp.cpu_count())]
75 cont.setPyScriptForCPULoad('cpu_loads = {}'.format(ref_load))
76 loads1 = cont.loadOfCPUCores()
77 self.assertEqual(loads1, ref_load)
78 cont.resetScriptForCPULoad()
79 loads2 = cont.loadOfCPUCores()
80 self.checkLoads(cont, loads2)
85 cont = self.getContainer("test_container_3")
86 cont.setPyScriptForCPULoad("bla-bla-bla")
87 self.assertRaises(Exception, cont.loadOfCPUCores)
92 cont = self.getContainer("test_container_4")
93 memory_total = cont.getTotalPhysicalMemory()
94 memory_in_use = cont.getTotalPhysicalMemoryInUse()
95 memory_by_me = cont.getTotalPhysicalMemoryInUseByMe()
96 self.assertGreater(memory_total, memory_in_use)
97 self.assertGreater(memory_in_use, memory_by_me)
102 Test checking memory consumption of container
104 cont = self.getContainer("test_container_5")
105 memory_by_me_start = cont.getTotalPhysicalMemoryInUseByMe()
107 psn = cont.createPyScriptNode("n","""b = bytearray(10485760)""")# create 10MB byte array abroad
108 psn.execute([],pickle.dumps(((),{})))
109 memory_by_me_end = cont.getTotalPhysicalMemoryInUseByMe()
110 self.assertGreater(memory_by_me_end,memory_by_me_start)
111 self.assertIn(memory_by_me_end-memory_by_me_start,[10,11,12])# test elevation of memory
114 if __name__ == '__main__':