setup.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import os
  2. import sys
  3. import time
  4. import setup_util
  5. import subprocess
  6. import multiprocessing
  7. def start(args, logfile, errfile):
  8. fwroot = args.fwroot
  9. try:
  10. ulib_root = subprocess.check_output('printf $ULIB_ROOT', shell=True, stderr=errfile)
  11. fcfg = ulib_root + "/benchmark.cfg"
  12. # 1. Change ULib Server configuration
  13. # I don't understand if the two approach are different...
  14. #threads = str(args.max_threads)
  15. PROCS = str(multiprocessing.cpu_count())
  16. setup_util.replace_text(fcfg, "PREFORK_CHILD .*", "PREFORK_CHILD " + PROCS)
  17. fprg = ulib_root + "/bin/userver_tcp"
  18. # 2. Start ULib Server (userver_tcp)
  19. logfile.write("ULib: trying to start server %s -c %s\n" % (fprg, fcfg))
  20. # sudo mysqlcheck -v -r -A -u benchmarkdbuser -p
  21. os.putenv("ORM_DRIVER","mysql")
  22. os.putenv("ORM_OPTION","host=" + args.database_host + " user=benchmarkdbuser password=benchmarkdbpass dbname=hello_world")
  23. os.putenv("UMEMPOOL", "1583,1507,-19,45,16458,523,-27,-14,27")
  24. # Run in the background, but keep stdout/stderr for easy debugging
  25. subprocess.Popen( "%s -c %s" % (fprg, fcfg), shell=True, stdout=logfile, stderr=errfile)
  26. # subprocess.Popen("UTRACE=\"0 50M\" " + fprg + " -c " + fcfg, shell=True, stdout=logfile, stderr=errfile)
  27. logfile.write("ULib: server STARTED\n")
  28. return 0
  29. except subprocess.CalledProcessError:
  30. return 1
  31. def stop(logfile, errfile):
  32. try:
  33. logfile.write( "ULib: setup.py STOP\n")
  34. # Stop ULib Server (userver_tcp)
  35. subprocess.check_call("kill -TERM $( cat $ULIB_ROOT/userver_tcp.pid )", shell=True, stderr=errfile, stdout=logfile)
  36. time.sleep(2);
  37. p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
  38. out, err = p.communicate()
  39. for line in out.splitlines():
  40. if 'userver_tcp' in line:
  41. pid = int(line.split(None, 2)[1])
  42. os.kill(pid, 9)
  43. subprocess.call("rm -f $ULIB_ROOT/userver_tcp.pid", shell=True, stderr=errfile, stdout=logfile)
  44. return 0
  45. except subprocess.CalledProcessError:
  46. return 1