setup.py 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. # setup.py
  2. # THIS CALLING IS WORKING
  3. # ----------------------------------------------------------------------
  4. # toolset/run-tests.py --install server --test ULib --type all --verbose
  5. # ----------------------------------------------------------------------
  6. # ....
  7. # ULib: setup.py running - FWROOT is /home/tfb/FrameworkBenchmarks
  8. # ....
  9. # INFO:root:Running installation for ULib
  10. # INSTALL:
  11. # export TROOT=$FWROOT/ULib &&
  12. # export IROOT=$FWROOT/installs &&
  13. # . $FWROOT/toolset/setup/linux/bash_functions.sh &&
  14. # . $FWROOT/ULib/install.sh (cwd=$FWROOT//installs)
  15. # ....
  16. # -----------------------------------------------------
  17. # Running Test: ULib ...
  18. # -----------------------------------------------------
  19. # ULib: TROOT is EMPTY
  20. # ULib: setup.py START - IROOT is /home/tfb/FrameworkBenchmarks/installs/pertest/ULib - TROOT is /home/tfb/FrameworkBenchmarks/ULib
  21. # ULib: bash_profile.sh script START
  22. # ULib: trying to start server /home/tfb/FrameworkBenchmarks/installs/pertest/ULib/ULib/bin/userver_tcp -c /home/tfb/FrameworkBenchmarks/installs/pertest/ULib/ULib/benchmark.cfg
  23. # ULib: server STARTED
  24. # {'results': []}
  25. # ULib: TROOT is EMPTY
  26. # ULib: setup.py STOP - IROOT is /home/tfb/FrameworkBenchmarks/installs/pertest/ULib - TROOT is /home/tfb/FrameworkBenchmarks/ULib
  27. # ULib: bash_profile.sh script START
  28. # ----------------------------------------------------------------------
  29. # NOW THIS CALLING IS WORKING TOO BUT IT IS CORRECT TO CALL IT BEFORE INSTALLING...?
  30. # ----------------------------------------------------------------------------------
  31. # toolset/run-tests.py --test ULib --type all --verbose
  32. # ----------------------------------------------------------------------------------
  33. # ....
  34. # ULib: setup.py running - FWROOT is /home/tfb/FrameworkBenchmarks
  35. # ....
  36. # -----------------------------------------------------
  37. # Running Test: ULib ...
  38. # -----------------------------------------------------
  39. # ULib: TROOT is EMPTY
  40. # ULib: setup.py START - IROOT is /home/tfb/FrameworkBenchmarks/installs/pertest/ULib - TROOT is /home/tfb/FrameworkBenchmarks/ULib
  41. # ULib: bash_profile.sh script START
  42. # ULib: NOT INSTALLED...
  43. # ULib: install.sh script START
  44. # ULib: trying to start server /home/tfb/FrameworkBenchmarks/installs/pertest/ULib/ULib/bin/userver_tcp -c /home/tfb/FrameworkBenchmarks/installs/pertest/ULib/ULib/benchmark.cfg
  45. # ULib: server STARTED
  46. # {'results': []}
  47. # ULib: TROOT is EMPTY
  48. # ULib: setup.py STOP - IROOT is /home/tfb/FrameworkBenchmarks/installs/pertest/ULib - TROOT is /home/tfb/FrameworkBenchmarks/ULib
  49. # ULib: bash_profile.sh script START
  50. # -----------------------------------------------------
  51. import os
  52. import sys
  53. import time
  54. #import logging
  55. import setup_util
  56. import subprocess
  57. import multiprocessing
  58. #log = logging.getLogger('framework_test')
  59. fwroot = setup_util.get_fwroot()
  60. print( "ULib: setup.py running - FWROOT is %s" % fwroot)
  61. #log.info("ULib: setup.py running - FWROOT is %s" % fwroot)
  62. # Queries the shell for the value of IROOT
  63. def get_iroot():
  64. try:
  65. # Use printf to avoid getting a newline
  66. # Redirect to avoid stderr printing
  67. iroot = subprocess.check_output('printf \"$IROOT\" 2>/dev/null', shell=True, executable='/bin/bash')
  68. if iroot != "":
  69. return iroot
  70. print( 'ULib: IROOT is EMPTY')
  71. # log.critical('ULib: IROOT is EMPTY')
  72. return setup_util.get_fwroot() + "/installs"
  73. except subprocess.CalledProcessError:
  74. return "";
  75. # Queries the shell for the value of TROOT
  76. def get_troot():
  77. try:
  78. # Use printf to avoid getting a newline
  79. # Redirect to avoid stderr printing
  80. troot = subprocess.check_output('printf \"$TROOT\" 2>/dev/null', shell=True, executable='/bin/bash')
  81. if troot != "":
  82. return troot
  83. print( 'ULib: TROOT is EMPTY')
  84. # log.critical('ULib: TROOT is EMPTY')
  85. return setup_util.get_fwroot() + "/ULib"
  86. except subprocess.CalledProcessError:
  87. return "";
  88. def getEnvironmentVar(name, iroot, troot):
  89. try:
  90. script = """
  91. export IROOT=%s
  92. . %s/bash_profile.sh 2>>/tmp/ULib_setup.txt
  93. printf $%s 2>>/tmp/ULib_setup.txt
  94. """
  95. value = subprocess.check_output(script % (iroot, troot, name), shell=True, executable='/bin/bash')
  96. return value
  97. except subprocess.CalledProcessError:
  98. return "";
  99. def callScriptAndCheckForInstallation(name, iroot, troot):
  100. try:
  101. script = """
  102. export IROOT=%s
  103. export TROOT=%s
  104. . %s/%s.sh >>/tmp/ULib_setup.txt 2>&1
  105. if [ ! -x "${ULIB_ROOT}/bin/userver_tcp" ] || [ ! -e "${ULIB_DOCUMENT_ROOT}/db.so" ] || [ ! -f "${ULIB_ROOT}/benchmark.cfg" ]; then
  106. exit 1
  107. fi
  108. """
  109. print( "ULib: %s.sh script START" % name)
  110. # log.info("ULib: %s.sh script START" % name)
  111. return subprocess.call(script % (iroot, troot, troot, name), shell=True, executable='/bin/bash')
  112. except subprocess.CalledProcessError:
  113. return 1
  114. # --------------------------------------------------------------------------------------------------------
  115. # TROOT - Path of this test's directory
  116. # IROOT - Path of this test's install directory ($FWROOT/installs or $FWROOT/installs/pertest/<test-name>)
  117. # --------------------------------------------------------------------------------------------------------
  118. def checkEnvironment(iroot, troot):
  119. try:
  120. if callScriptAndCheckForInstallation('bash_profile', iroot, troot) != 0:
  121. print( 'ULib: NOT INSTALLED...')
  122. # log.critical('ULib: NOT INSTALLED...')
  123. if callScriptAndCheckForInstallation('install', iroot, troot) != 0:
  124. print( "ULib: install.sh script FAILED")
  125. # log.info("ULib: install.sh script FAILED")
  126. return False
  127. return True
  128. except:
  129. pass
  130. return False
  131. ##############
  132. # start(args)
  133. ##############
  134. def start(args, logfile, errfile):
  135. try:
  136. iroot = get_iroot()
  137. troot = get_troot()
  138. print( "ULib: setup.py START - IROOT is %s - TROOT is %s" % (iroot, troot))
  139. # log.info("ULib: setup.py START - IROOT is %s - TROOT is %s" % (iroot, troot))
  140. if not checkEnvironment(iroot, troot):
  141. return 1
  142. ulib_root = getEnvironmentVar('ULIB_ROOT', iroot, troot)
  143. fcfg = ulib_root + "/benchmark.cfg"
  144. # 1. Change ULib Server configuration
  145. # I don't understand if the two approach are different...
  146. #threads = str(args.max_threads)
  147. PROCS = str(multiprocessing.cpu_count())
  148. setup_util.replace_text(fcfg, "PREFORK_CHILD .*", "PREFORK_CHILD " + PROCS)
  149. fprg = ulib_root + "/bin/userver_tcp"
  150. # 2. Start ULib Server (userver_tcp)
  151. print( "ULib: trying to start server " + fprg + " -c " + fcfg)
  152. # log.info("ULib: trying to start server " + fprg + " -c " + fcfg)
  153. # sudo mysqlcheck -v -r -A -u benchmarkdbuser -p
  154. os.putenv("ORM_DRIVER","mysql")
  155. os.putenv("ORM_OPTION","host=" + args.database_host + " user=benchmarkdbuser password=benchmarkdbpass dbname=hello_world")
  156. os.putenv("UMEMPOOL", "1583,1507,-19,45,16458,523,-27,-14,27")
  157. ulib_server_output = getEnvironmentVar('ULIB_SERVER_OUTPUT', iroot, troot)
  158. # Run in the background, but keep stdout/stderr for easy debugging
  159. subprocess.Popen( fprg + " -c " + fcfg + " >" + ulib_server_output + " 2>&1", shell=True, stdout=logfile, stderr=errfile)
  160. # subprocess.Popen("UTRACE=\"0 50M\" " + fprg + " -c " + fcfg + " >" + ulib_server_output + " 2>&1", shell=True, stdout=logfile, stderr=errfile)
  161. print( "ULib: server STARTED")
  162. # log.info("ULib: server STARTED")
  163. return 0
  164. except subprocess.CalledProcessError:
  165. return 1
  166. ##############
  167. # stop()
  168. ##############
  169. def stop(logfile, errfile):
  170. try:
  171. iroot = get_iroot()
  172. troot = get_troot()
  173. print( "ULib: setup.py STOP - IROOT is %s - TROOT is %s" % (iroot, troot))
  174. # log.info("ULib: setup.py STOP - IROOT is %s - TROOT is %s" % (iroot, troot))
  175. if not checkEnvironment(iroot, troot):
  176. return 1
  177. ulib_root = getEnvironmentVar('ULIB_ROOT', iroot, troot)
  178. # Stop ULib Server (userver_tcp)
  179. subprocess.check_call("kill -TERM $( cat " + ulib_root + "/userver_tcp.pid )", shell=True, stderr=errfile, stdout=logfile)
  180. time.sleep(2);
  181. p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
  182. out, err = p.communicate()
  183. for line in out.splitlines():
  184. if 'userver_tcp' in line:
  185. pid = int(line.split(None, 2)[1])
  186. os.kill(pid, 9)
  187. subprocess.call("rm -f " + ulib_root + "/userver_tcp.pid /tmp/ULib_setup.txt", shell=True, stderr=errfile, stdout=logfile)
  188. return 0
  189. except subprocess.CalledProcessError:
  190. return 1