Salome HOME
77ac219a3e7c78923a5bf01d79f730aca847c066
[modules/kernel.git] / bin / appliskel / tests / concurrentSession / usecase_concurrent_single_appli.sh
1 #!/bin/bash
2
3 # Copyright (C) 2015-2023  CEA/DEN, EDF R&D, OPEN CASCADE
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
18 #
19 # See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #
21
22 echo "This is a script that can be run concurrently."
23 echo "It takes as single argument the number of concurrent executions:"
24 echo "Usage: " $0 "<nb_execution> <output_folder>"
25 echo
26 echo "Here is what executed code contents looks like:"
27 echo "   - do some stuff"
28 echo "   - run SALOME in terminal mode, and log port number to a file"
29 echo "   - run some scripts (Python, binary), each in a dedicated SALOME session on a specific port"
30 echo "   - do some stuff"
31 echo
32
33
34 if [ $# != 2 ]; then
35     echo "Usage:" $0 "<nb_execution> <output_folder>"
36     exit 1
37 fi
38
39 NB_COMP=$1
40 OUTPUT_FOLDER=$2
41 BASE_DIR=`pwd`
42 SALOME_PORT=-1
43
44 case ${OUTPUT_FOLDER} in
45     /*) ;; # OUTPUT_FOLDER is given as an absolute path
46     *) OUTPUT_FOLDER=${BASE_DIR}/${OUTPUT_FOLDER} ;; # build absolute path
47 esac
48
49 if [ "x${SALOME_APPLI_FOLDER}" == "x" ]; then
50     echo "SALOME_APPLI_FOLDER variable is not set (or empty)."
51     echo "Try to locate SALOME launcher in current directory."
52     SALOME_APPLI_FOLDER=`pwd`
53     if ! [ -f ${SALOME_APPLI_FOLDER}/salome ]; then
54         echo "Unable to locate salome command."
55         exit 1
56     fi
57 fi
58
59 start_salome() {
60     ${SALOME_APPLI_FOLDER}/salome start -t --ns-port-log=${OUTPUT_FOLDER}/session.log
61     SALOME_PORT=`cat ${OUTPUT_FOLDER}/session.log`
62     echo "SALOME is running on port" ${SALOME_PORT}
63 }
64
65 stop_salome() {
66     ${SALOME_APPLI_FOLDER}/salome shell -p ${SALOME_PORT} ${SALOME_APPLI_FOLDER}/bin/salome/killSalomeWithPort.py args:${SALOME_PORT}
67 }
68
69 run_command() {
70     WORK_DIR=`pwd` # a pushd has been done before calling this function
71     echo "Run command in folder:" ${WORK_DIR}
72     ${SALOME_APPLI_FOLDER}/salome shell -p ${SALOME_PORT} ${SALOME_APPLI_FOLDER}/bin/salome/waitContainers.py
73     ${SALOME_APPLI_FOLDER}/salome shell -p ${SALOME_PORT} ${BASE_DIR}/myscript.py args:${WORK_DIR}
74     echo "Execution terminated in folder:" ${WORK_DIR}
75 }
76
77
78 # make myscript.py
79 echo "
80 #!/usr/bin/env python
81 import os
82 import sys
83 import time
84 work_dir = sys.argv[1]
85 logfile = os.path.join(work_dir, 'myscript.log')
86 msg = 'Waiting for 3s in folder %s'%work_dir
87 time.sleep(3)
88 with open(logfile, 'w') as f:
89   f.write(msg)
90 " > ${BASE_DIR}/myscript.py
91
92 # Build output folders
93 typeset -i i=${NB_COMP}
94 while (( $i > 0 ))
95 do
96     mkdir -p ${OUTPUT_FOLDER}/execution_$i
97     let "i=i-1"
98 done
99
100 # Start execution
101 start_salome
102 typeset -i i=${NB_COMP}
103 while (( $i > 0 ))
104 do
105     pushd ${OUTPUT_FOLDER}/execution_$i > /dev/null
106     run_command &
107     popd > /dev/null
108     let "i=i-1"
109 done
110 stop_salome