1 # Copyright (C) 2006-2014 CEA/DEN, EDF R&D
3 # This library is free software; you can redistribute it and/or
4 # modify it under the terms of the GNU Lesser General Public
5 # License as published by the Free Software Foundation; either
6 # version 2.1 of the License, or (at your option) any later version.
8 # This library is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 # Lesser General Public License for more details.
13 # You should have received a copy of the GNU Lesser General Public
14 # License along with this library; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
27 class TestSaveLoadRun(unittest.TestCase):
29 SALOMERuntime.RuntimeSALOME.setRuntime()
30 self.r=SALOMERuntime.getSALOMERuntime()
34 """First test of HP Container no loop here only the 3 sorts of python nodes (the Distributed is it still used and useful ?) """
35 fname="TestSaveLoadRun0.xml"
37 sqrtOfNumberOfTurn=1000 # 3000 -> 3.2s/Node, 1000 -> 0.1s/Node
39 p=self.r.createProc("prTest0")
40 td=p.createType("double","double")
41 ti=p.createType("int","int")
42 cont=p.createContainer("gg","HPSalome")
44 cont.setProperty("name","localhost")
45 cont.setProperty("hostname","localhost")
51 ref=datetime.datetime.now()
52 t=0. ; pas=1./float(nb)
56 t+=1.+cos(1.*(x*3.14159))
59 print "coucou from script0-%i -> %s"%(dbg,str(datetime.datetime.now()-ref))
65 ref=datetime.datetime.now()
66 o2=0. ; pas=1./float(i1)
70 o2+=1.+cos(1.*(x*3.14159))
72 print "coucou from script1-%i -> %s"%(dbg,str(datetime.datetime.now()-ref))
74 for i in xrange(nbOfNodes):
75 node0=self.r.createFuncNode("DistPython","node%i"%(i))
78 node0.setContainer(cont)
79 node0.setScript(script0)
80 nb=node0.edAddInputPort("nb",ti) ; nb.edInitInt(sqrtOfNumberOfTurn)
81 dbg=node0.edAddInputPort("dbg",ti) ; dbg.edInitInt(i+1)
82 out0=node0.edAddOutputPort("s",td)
84 nodeMiddle=self.r.createFuncNode("Salome","node%i_1"%(i))
85 p.edAddChild(nodeMiddle)
86 p.edAddCFLink(node0,nodeMiddle)
87 nodeMiddle.setFname("ff")
88 nodeMiddle.setContainer(cont)
89 nodeMiddle.setScript(script0)
90 nb=nodeMiddle.edAddInputPort("nb",ti) ; nb.edInitInt(sqrtOfNumberOfTurn)
91 dbg=nodeMiddle.edAddInputPort("dbg",ti) ; dbg.edInitInt(i+1)
92 out0=nodeMiddle.edAddOutputPort("s",td)
93 nodeMiddle.setExecutionMode("remote")
95 nodeEnd=self.r.createScriptNode("Salome","node%i_2"%(i+1))
97 p.edAddCFLink(nodeMiddle,nodeEnd)
98 nodeEnd.setContainer(cont)
99 nodeEnd.setScript(script1)
100 i1=nodeEnd.edAddInputPort("i1",ti) ; i1.edInitInt(sqrtOfNumberOfTurn)
101 dbg=nodeEnd.edAddInputPort("dbg",ti) ; dbg.edInitInt(i)
102 o2=nodeEnd.edAddOutputPort("o2",td)
103 nodeEnd.setExecutionMode("remote")
107 ex=pilot.ExecutorSwig()
108 self.assertEqual(p.getState(),pilot.READY)
109 st=datetime.datetime.now()
112 print "Time spend of test0 to run 1st %s"%(str(datetime.datetime.now()-st))
113 self.assertEqual(p.getState(),pilot.DONE)
114 # 2nd exec using the same already launched remote python interpreters
115 st=datetime.datetime.now()
117 print "Time spend of test0 to run 2nd %s"%(str(datetime.datetime.now()-st))
118 self.assertEqual(p.getState(),pilot.DONE)
119 # 3rd exec using the same already launched remote python interpreters
120 st=datetime.datetime.now()
122 print "Time spend of test0 to run 3rd %s"%(str(datetime.datetime.now()-st))
123 self.assertEqual(p.getState(),pilot.DONE)
127 """ HP Container again like test0 but the initialization key of HPContainer is used here."""
128 fname="TestSaveLoadRun1.xml"
130 sqrtOfNumberOfTurn=1000 # 3000 -> 3.2s/Node, 1000 -> 0.1s/Node
131 l=loader.YACSLoader()
132 p=self.r.createProc("prTest1")
133 td=p.createType("double","double")
134 ti=p.createType("int","int")
135 cont=p.createContainer("gg","HPSalome")
136 cont.setSizeOfPool(4)
137 cont.setProperty("InitializeScriptKey","aa=123.456")
138 cont.setProperty("name","localhost")
139 cont.setProperty("hostname","localhost")
145 ref=datetime.datetime.now()
146 t=0. ; pas=1./float(nb)
150 t+=1.+cos(1.*(x*3.14159))
153 print "coucou from script0-%i -> %s"%(dbg,str(datetime.datetime.now()-ref))
156 # here in script1 aa is refered ! aa will exist thanks to HPCont Init Script
160 ref=datetime.datetime.now()
161 o2=0. ; pas=1./float(i1)
165 o2+=1.+cos(1.*(x*3.14159))
167 print "coucou %lf from script1-%i -> %s"%(aa,dbg,str(datetime.datetime.now()-ref))
171 for i in xrange(nbOfNodes):
172 nodeMiddle=self.r.createFuncNode("Salome","node%i_1"%(i)) # PyFuncNode remote
173 p.edAddChild(nodeMiddle)
174 nodeMiddle.setFname("ff")
175 nodeMiddle.setContainer(cont)
176 nodeMiddle.setScript(script0)
177 nb=nodeMiddle.edAddInputPort("nb",ti) ; nb.edInitInt(sqrtOfNumberOfTurn)
178 dbg=nodeMiddle.edAddInputPort("dbg",ti) ; dbg.edInitInt(i+1)
179 out0=nodeMiddle.edAddOutputPort("s",td)
180 nodeMiddle.setExecutionMode("remote")
182 nodeEnd=self.r.createScriptNode("Salome","node%i_2"%(i+1)) # PythonNode remote
183 p.edAddChild(nodeEnd)
184 p.edAddCFLink(nodeMiddle,nodeEnd)
185 nodeEnd.setContainer(cont)
186 nodeEnd.setScript(script1)
187 i1=nodeEnd.edAddInputPort("i1",ti) ; i1.edInitInt(sqrtOfNumberOfTurn)
188 dbg=nodeEnd.edAddInputPort("dbg",ti) ; dbg.edInitInt(i)
189 o2=nodeEnd.edAddOutputPort("o2",td)
190 nodeEnd.setExecutionMode("remote")
195 self.assertEqual(p.edGetDirectDescendants()[0].getContainer().getProperty("InitializeScriptKey"),"aa=123.456")
197 ex=pilot.ExecutorSwig()
198 self.assertEqual(p.getState(),pilot.READY)
199 st=datetime.datetime.now()
201 print "Time spend of test1 to 1st run %s"%(str(datetime.datetime.now()-st))
202 self.assertEqual(p.getState(),pilot.DONE)
204 st=datetime.datetime.now()
206 print "Time spend of test1 to 2nd run %s"%(str(datetime.datetime.now()-st))
207 self.assertEqual(p.getState(),pilot.DONE)
209 st=datetime.datetime.now()
211 print "Time spend of test1 to 3rd run %s"%(str(datetime.datetime.now()-st))
212 self.assertEqual(p.getState(),pilot.DONE)
216 """ Test on HP Containers in foreach context."""
219 print "%%lf - %%s"%%(aa,str(my_container))
222 script1="""from math import cos
224 ref=datetime.datetime.now()
225 o2=0. ; pas=1./float(i1)
229 o2+=1.+cos(1.*(x*3.14159))
231 print "coucou %lf from script -> %s"%(aa,str(datetime.datetime.now()-ref))
235 script2="""o9=sum(i8)
237 fname="TestSaveLoadRun2.xml"
239 sqrtOfNumberOfTurn=1000 # 3000 -> 3.2s/Node, 1000 -> 0.1s/Node
240 l=loader.YACSLoader()
241 p=self.r.createProc("prTest1")
242 td=p.createType("double","double")
243 ti=p.createType("int","int")
244 tdi=p.createSequenceTc("seqint","seqint",ti)
245 tdd=p.createSequenceTc("seqdouble","seqdouble",td)
246 cont=p.createContainer("gg","HPSalome")
247 cont.setSizeOfPool(4)
248 cont.setProperty("InitializeScriptKey","aa=123.456")
249 cont.setProperty("name","localhost")
250 cont.setProperty("hostname","localhost")
252 node0=self.r.createFuncNode("Salome","PyFunction0") # PyFuncNode remote
255 node0.setContainer(cont)
256 node0.setScript(script0%(sqrtOfNumberOfTurn))
257 out0_0=node0.edAddOutputPort("o1",tdi)
258 out1_0=node0.edAddOutputPort("o2",ti)
259 node0.setExecutionMode("remote")
261 node1=self.r.createForEachLoop("node1",ti)
263 p.edAddCFLink(node0,node1)
264 p.edAddLink(out0_0,node1.edGetSeqOfSamplesPort())
265 node1.edGetNbOfBranchesPort().edInitInt(8)
267 node2=self.r.createScriptNode("Salome","PyScript3")
268 node1.edAddChild(node2)
269 node2.setContainer(cont)
270 node2.setScript(script1)
271 i1=node2.edAddInputPort("i1",ti)
272 p.edAddLink(node1.edGetSamplePort(),i1)
273 out0_2=node2.edAddOutputPort("o2",td)
274 out1_2=node2.edAddOutputPort("o3",ti)
275 node2.setExecutionMode("remote")
277 node3=self.r.createScriptNode("Salome","PyScript7")
279 node3.setScript(script2)
280 p.edAddCFLink(node1,node3)
281 i8=node3.edAddInputPort("i8",tdd)
282 o9=node3.edAddOutputPort("o9",td)
283 p.edAddLink(out0_2,i8)
287 o9=p.getChildByName("PyScript7").getOutputPort("o9")
288 self.assertTrue(len(p.edGetDirectDescendants()[1].getChildByName("PyScript3").getContainer().getProperty("InitializeScriptKey"))!=0)
290 refExpected=16016013.514623128
291 ex=pilot.ExecutorSwig()
292 self.assertEqual(p.getState(),pilot.READY)
293 st=datetime.datetime.now()
295 print "Time spend of test2 to 1st run %s"%(str(datetime.datetime.now()-st))
296 self.assertEqual(p.getState(),pilot.DONE)
297 self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
299 st=datetime.datetime.now()
301 print "Time spend of test2 to 2nd run %s"%(str(datetime.datetime.now()-st))
302 self.assertEqual(p.getState(),pilot.DONE)
303 self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
305 st=datetime.datetime.now()
307 print "Time spend of test2 to 3rd run %s"%(str(datetime.datetime.now()-st))
308 self.assertEqual(p.getState(),pilot.DONE)
309 self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
313 """ Test that focuses on parallel load of containers."""
316 print "%%lf - %%s"%%(aa,str(my_container))
319 script1="""from math import cos
321 ref=datetime.datetime.now()
322 o2=0. ; pas=1./float(i1)
326 o2+=1.+cos(1.*(x*3.14159))
328 print "coucou %lf from script -> %s"%(aa,str(datetime.datetime.now()-ref))
332 script2="""o9=sum(i8)
334 fname="TestSaveLoadRun3.xml"
336 sqrtOfNumberOfTurn=10
337 l=loader.YACSLoader()
338 p=self.r.createProc("prTest1")
339 td=p.createType("double","double")
340 ti=p.createType("int","int")
341 tdi=p.createSequenceTc("seqint","seqint",ti)
342 tdd=p.createSequenceTc("seqdouble","seqdouble",td)
343 cont=p.createContainer("gg","HPSalome")
344 cont.setSizeOfPool(8)
345 cont.setProperty("InitializeScriptKey","aa=123.456")
346 cont.setProperty("name","localhost")
347 cont.setProperty("hostname","localhost")
349 node0=self.r.createFuncNode("Salome","PyFunction0") # PyFuncNode remote
352 node0.setContainer(cont)
353 node0.setScript(script0%(sqrtOfNumberOfTurn))
354 out0_0=node0.edAddOutputPort("o1",tdi)
355 out1_0=node0.edAddOutputPort("o2",ti)
356 node0.setExecutionMode("remote")
358 node1=self.r.createForEachLoop("node1",ti)
360 p.edAddCFLink(node0,node1)
361 p.edAddLink(out0_0,node1.edGetSeqOfSamplesPort())
362 node1.edGetNbOfBranchesPort().edInitInt(16)
364 node2=self.r.createScriptNode("Salome","PyScript3")
365 node1.edAddChild(node2)
366 node2.setContainer(cont)
367 node2.setScript(script1)
368 i1=node2.edAddInputPort("i1",ti)
369 p.edAddLink(node1.edGetSamplePort(),i1)
370 out0_2=node2.edAddOutputPort("o2",td)
371 out1_2=node2.edAddOutputPort("o3",ti)
372 node2.setExecutionMode("remote")
374 node3=self.r.createScriptNode("Salome","PyScript7")
376 node3.setScript(script2)
377 p.edAddCFLink(node1,node3)
378 i8=node3.edAddInputPort("i8",tdd)
379 o9=node3.edAddOutputPort("o9",td)
380 p.edAddLink(out0_2,i8)
384 o9=p.getChildByName("PyScript7").getOutputPort("o9")
385 self.assertTrue(len(p.edGetDirectDescendants()[1].getChildByName("PyScript3").getContainer().getProperty("InitializeScriptKey"))!=0)
387 refExpected=11000.008377058712
388 ex=pilot.ExecutorSwig()
389 self.assertEqual(p.getState(),pilot.READY)
390 st=datetime.datetime.now()
392 print "Time spend of test3 to 1st run %s"%(str(datetime.datetime.now()-st))
393 self.assertEqual(p.getState(),pilot.DONE)
394 self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
396 st=datetime.datetime.now()
398 print "Time spend of test3 to 2nd run %s"%(str(datetime.datetime.now()-st))
399 self.assertEqual(p.getState(),pilot.DONE)
400 self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
402 st=datetime.datetime.now()
404 print "Time spend of test3 to 3rd run %s"%(str(datetime.datetime.now()-st))
405 self.assertEqual(p.getState(),pilot.DONE)
406 self.assertAlmostEqual(refExpected,o9.getPyObj(),5)
410 """Non regression test of multi pyScriptNode, pyFuncNode sharing the same HPContainer instance."""
411 fname="TestSaveLoadRun4.xml"
417 for j in xrange(i+10):
424 l=loader.YACSLoader()
425 ex=pilot.ExecutorSwig()
426 p=self.r.createProc("pr")
427 cont=p.createContainer("gg","HPSalome")
428 cont.setSizeOfPool(10)
429 td=p.createType("int","int")
430 td2=p.createSequenceTc("seqint","seqint",td)
431 td3=p.createSequenceTc("seqintvec","seqintvec",td2)
432 node1=self.r.createScriptNode("","node1")
433 node1.setScript(script1)
434 o1=node1.edAddOutputPort("o1",td3)
437 node2=self.r.createForEachLoop("node2",td2)
439 p.edAddCFLink(node1,node2)
440 p.edAddLink(o1,node2.edGetSeqOfSamplesPort())
441 node2.edGetNbOfBranchesPort().edInitInt(2)
442 node20=self.r.createBloc("node20")
443 node2.edAddChild(node20)
444 node200=self.r.createForEachLoop("node200",td)
445 node20.edAddChild(node200)
446 node200.edGetNbOfBranchesPort().edInitInt(10)
447 p.edAddLink(node2.edGetSamplePort(),node200.edGetSeqOfSamplesPort())
448 node2000=self.r.createScriptNode("","node2000")
449 node2000.setContainer(cont)
450 node2000.setExecutionMode("remote")
451 node200.edAddChild(node2000)
452 i5=node2000.edAddInputPort("i5",td)
453 o6=node2000.edAddOutputPort("o6",td)
454 node2000.setScript("o6=2+i5")
455 p.edAddLink(node200.edGetSamplePort(),i5)
457 node3=self.r.createForEachLoop("node3",td)
459 p.edAddCFLink(node2,node3)
460 p.edAddLink(o6,node3.edGetSeqOfSamplesPort())
461 node3.edGetNbOfBranchesPort().edInitInt(2)
462 node30=self.r.createBloc("node30")
463 node3.edAddChild(node30)
464 node300=self.r.createForEachLoop("node300",td)
465 node30.edAddChild(node300)
466 node300.edGetNbOfBranchesPort().edInitInt(10)
467 p.edAddLink(node3.edGetSamplePort(),node300.edGetSeqOfSamplesPort())
468 node3000=self.r.createScriptNode("","node3000")
469 node3000.setContainer(cont)
470 node3000.setExecutionMode("remote")
471 node300.edAddChild(node3000)
472 i14=node3000.edAddInputPort("i14",td)
473 o15=node3000.edAddOutputPort("o15",td)
474 node3000.setScript("o15=3+i14")
475 p.edAddLink(node300.edGetSamplePort(),i14)
477 node4=self.r.createScriptNode("","node4")
478 node4.setScript("o9=i8")
480 i8=node4.edAddInputPort("i8",td3)
481 o9=node4.edAddOutputPort("o9",td3)
482 p.edAddCFLink(node3,node4)
486 ex = pilot.ExecutorSwig()
487 self.assertEqual(p.getState(),pilot.READY)
489 self.assertEqual(p.getState(),pilot.DONE)
490 zeResu=p.getChildByName("node4").getOutputPort("o9").get()
491 self.assertEqual(zeResu,[[5,6,7,8,9,10,11,12,13,14],[15,16,17,18,19,20,21,22,23,24,25],[26,27,28,29,30,31,32,33,34,35,36,37],[38,39,40,41,42,43,44,45,46,47,48,49,50],[51,52,53,54,55,56,57,58,59,60,61,62,63,64],[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79], [80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95]])
495 """Non regression test 2 of multi pyNode, pyFuncNode sharing the same HPContainer instance."""
496 fname="TestSaveLoadRun5.xml"
502 for j in xrange(i+10):
509 l=loader.YACSLoader()
510 ex=pilot.ExecutorSwig()
511 p=self.r.createProc("pr")
512 cont=p.createContainer("gg","HPSalome")
513 cont.setSizeOfPool(10)
514 td=p.createType("int","int")
515 td2=p.createSequenceTc("seqint","seqint",td)
516 td3=p.createSequenceTc("seqintvec","seqintvec",td2)
517 node1=self.r.createScriptNode("","node1")
518 node1.setScript(script1)
519 o1=node1.edAddOutputPort("o1",td3)
522 node2=self.r.createForEachLoop("node2",td2)
524 p.edAddCFLink(node1,node2)
525 p.edAddLink(o1,node2.edGetSeqOfSamplesPort())
526 node2.edGetNbOfBranchesPort().edInitInt(2)
527 node20=self.r.createBloc("node20")
528 node2.edAddChild(node20)
529 node200=self.r.createForEachLoop("node200",td)
530 node20.edAddChild(node200)
531 node200.edGetNbOfBranchesPort().edInitInt(10)
532 p.edAddLink(node2.edGetSamplePort(),node200.edGetSeqOfSamplesPort())
533 node2000=self.r.createFuncNode("Salome","node2000")
534 node2000.setFname("ff")
535 node2000.setContainer(cont)
536 node2000.setExecutionMode("remote")
537 node200.edAddChild(node2000)
538 i5=node2000.edAddInputPort("i5",td)
539 o6=node2000.edAddOutputPort("o6",td)
540 node2000.setScript("def ff(x):\n return 2+x")
541 p.edAddLink(node200.edGetSamplePort(),i5)
543 node3=self.r.createForEachLoop("node3",td)
545 p.edAddCFLink(node2,node3)
546 p.edAddLink(o6,node3.edGetSeqOfSamplesPort())
547 node3.edGetNbOfBranchesPort().edInitInt(2)
548 node30=self.r.createBloc("node30")
549 node3.edAddChild(node30)
550 node300=self.r.createForEachLoop("node300",td)
551 node30.edAddChild(node300)
552 node300.edGetNbOfBranchesPort().edInitInt(10)
553 p.edAddLink(node3.edGetSamplePort(),node300.edGetSeqOfSamplesPort())
554 node3000=self.r.createFuncNode("Salome","node3000")
555 node3000.setFname("ff")
556 node3000.setContainer(cont)
557 node3000.setExecutionMode("remote")
558 node300.edAddChild(node3000)
559 i14=node3000.edAddInputPort("i14",td)
560 o15=node3000.edAddOutputPort("o15",td)
561 node3000.setScript("def ff(x):\n return 3+x")
562 p.edAddLink(node300.edGetSamplePort(),i14)
564 node4=self.r.createScriptNode("","node4")
565 node4.setScript("o9=i8")
567 i8=node4.edAddInputPort("i8",td3)
568 o9=node4.edAddOutputPort("o9",td3)
569 p.edAddCFLink(node3,node4)
573 ex = pilot.ExecutorSwig()
574 self.assertEqual(p.getState(),pilot.READY)
576 self.assertEqual(p.getState(),pilot.DONE)
577 zeResu=p.getChildByName("node4").getOutputPort("o9").get()
578 self.assertEqual(zeResu,[[5,6,7,8,9,10,11,12,13,14],[15,16,17,18,19,20,21,22,23,24,25],[26,27,28,29,30,31,32,33,34,35,36,37],[38,39,40,41,42,43,44,45,46,47,48,49,50],[51,52,53,54,55,56,57,58,59,60,61,62,63,64],[65,66,67,68,69,70,71,72,73,74,75,76,77,78,79], [80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95]])
584 U = os.getenv('USER')
585 f=open("/tmp/" + U + "/UnitTestsResult", 'a')
586 f.write(" --- TEST src/yacsloader: testSaveLoadRun.py\n")
587 suite = unittest.makeSuite(TestSaveLoadRun)
588 result=unittest.TextTestRunner(f, descriptions=1, verbosity=1).run(suite)
590 sys.exit(not result.wasSuccessful())