]> SALOME platform Git repositories - modules/kernel.git/blob - src/Container/Test/testcontainer.py
Salome HOME
Merge branch 'V9_9_BR'
[modules/kernel.git] / src / Container / Test / testcontainer.py
1 #  -*- coding: iso-8859-1 -*-
2 # Copyright (C) 2007-2022  CEA/DEN, EDF R&D, 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 loadOfCPUCores
61         cont = self.getContainer("test_container_1")
62         loads1 = cont.loadOfCPUCores()
63         self.checkLoads(cont, loads1)
64         sleep(1)
65         loads2 = cont.loadOfCPUCores()
66         self.checkLoads(cont, loads2)
67         self.assertNotEqual(loads1, loads2)
68         cont.Shutdown()
69
70     def test2(self):
71         # Check custom script
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)
81         cont.Shutdown()
82
83     def test3(self):
84         # Check bad script
85         cont = self.getContainer("test_container_3")
86         cont.setPyScriptForCPULoad("bla-bla-bla")
87         self.assertRaises(Exception, cont.loadOfCPUCores)
88         cont.Shutdown()
89
90     def test4(self):
91         # check memory sizes
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)
98         cont.Shutdown()
99     
100     def test5(self):
101         """
102         Test checking memory consumption of container
103         """
104         cont = self.getContainer("test_container_5")
105         memory_by_me_start = cont.getTotalPhysicalMemoryInUseByMe()
106         import pickle
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
112         cont.Shutdown()
113
114 if __name__ == '__main__':
115     salome.standalone()
116     salome.salome_init()
117     unittest.main()