You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

90 line
2.4 KiB

  1. # imports - standard imports
  2. import os
  3. import re
  4. import subprocess
  5. # imports - module imports
  6. import bench
  7. def generate_config(bench_path):
  8. from urllib.parse import urlparse
  9. from bench.bench import Bench
  10. config = Bench(bench_path).conf
  11. redis_version = get_redis_version()
  12. ports = {}
  13. for key in ("redis_cache", "redis_queue"):
  14. ports[key] = urlparse(config[key]).port
  15. write_redis_config(
  16. template_name="redis_queue.conf",
  17. context={
  18. "port": ports["redis_queue"],
  19. "bench_path": os.path.abspath(bench_path),
  20. "redis_version": redis_version,
  21. },
  22. bench_path=bench_path,
  23. )
  24. write_redis_config(
  25. template_name="redis_cache.conf",
  26. context={
  27. "maxmemory": config.get("cache_maxmemory", get_max_redis_memory()),
  28. "port": ports["redis_cache"],
  29. "redis_version": redis_version,
  30. },
  31. bench_path=bench_path,
  32. )
  33. # make pids folder
  34. pid_path = os.path.join(bench_path, "config", "pids")
  35. if not os.path.exists(pid_path):
  36. os.makedirs(pid_path)
  37. # ACL feature is introduced in Redis 6.0
  38. if redis_version < 6.0:
  39. return
  40. # make ACL files
  41. acl_rq_path = os.path.join(bench_path, "config", "redis_queue.acl")
  42. acl_redis_cache_path = os.path.join(bench_path, "config", "redis_cache.acl")
  43. open(acl_rq_path, "a").close()
  44. open(acl_redis_cache_path, "a").close()
  45. def write_redis_config(template_name, context, bench_path):
  46. template = bench.config.env().get_template(template_name)
  47. if "config_path" not in context:
  48. context["config_path"] = os.path.abspath(os.path.join(bench_path, "config"))
  49. if "pid_path" not in context:
  50. context["pid_path"] = os.path.join(context["config_path"], "pids")
  51. with open(os.path.join(bench_path, "config", template_name), "w") as f:
  52. f.write(template.render(**context))
  53. def get_redis_version():
  54. import semantic_version
  55. version_string = subprocess.check_output("redis-server --version", shell=True)
  56. version_string = version_string.decode("utf-8").strip()
  57. # extract version number from string
  58. version = re.findall(r"\d+\.\d+", version_string)
  59. if not version:
  60. return None
  61. version = semantic_version.Version(version[0], partial=True)
  62. return float(f"{version.major}.{version.minor}")
  63. def get_max_redis_memory():
  64. try:
  65. max_mem = os.sysconf("SC_PAGE_SIZE") * os.sysconf("SC_PHYS_PAGES")
  66. except ValueError:
  67. max_mem = int(subprocess.check_output(["sysctl", "-n", "hw.memsize"]).strip())
  68. return max(50, int((max_mem / (1024.0**2)) * 0.05))