|
| 1 | +import logging |
| 2 | +import os |
| 3 | +import subprocess |
| 4 | +import tempfile |
| 5 | +import time |
| 6 | +from shutil import copyfile |
| 7 | + |
| 8 | +import redis |
| 9 | + |
| 10 | + |
| 11 | +def checkDatasetLocalRequirements(benchmark_config, redis_tmp_dir): |
| 12 | + for k in benchmark_config["dbconfig"]: |
| 13 | + if "dataset" in k: |
| 14 | + dataset = k["dataset"] |
| 15 | + if dataset is not None: |
| 16 | + logging.info( |
| 17 | + "Copying rdb {} to {}/dump.rdb".format(dataset, redis_tmp_dir) |
| 18 | + ) |
| 19 | + copyfile(dataset, "{}/dump.rdb".format(redis_tmp_dir)) |
| 20 | + |
| 21 | + |
| 22 | +def waitForConn(conn, retries=20, command="PING", shouldBe=True): |
| 23 | + """Wait until a given Redis connection is ready""" |
| 24 | + result = False |
| 25 | + err1 = "" |
| 26 | + while retries > 0 and result is False: |
| 27 | + try: |
| 28 | + if conn.execute_command(command) == shouldBe: |
| 29 | + result = True |
| 30 | + except redis.exceptions.BusyLoadingError: |
| 31 | + time.sleep(0.1) # give extra 100msec in case of RDB loading |
| 32 | + except redis.ConnectionError as err: |
| 33 | + err1 = str(err) |
| 34 | + except redis.ResponseError as err: |
| 35 | + err1 = str(err) |
| 36 | + if not err1.startswith("DENIED"): |
| 37 | + raise |
| 38 | + time.sleep(0.1) |
| 39 | + retries -= 1 |
| 40 | + logging.debug("Waiting for Redis") |
| 41 | + return result |
| 42 | + |
| 43 | + |
| 44 | +def spinUpLocalRedis( |
| 45 | + benchmark_config, |
| 46 | + port, |
| 47 | + local_module_file, |
| 48 | +): |
| 49 | + # copy the rdb to DB machine |
| 50 | + dataset = None |
| 51 | + temporary_dir = tempfile.mkdtemp() |
| 52 | + logging.info( |
| 53 | + "Using local temporary dir to spin up Redis Instance. Path: {}".format( |
| 54 | + temporary_dir |
| 55 | + ) |
| 56 | + ) |
| 57 | + checkDatasetLocalRequirements(benchmark_config, temporary_dir) |
| 58 | + |
| 59 | + # start redis-server |
| 60 | + command = ['redis-server', '--save', '""', '--port', '{}'.format(port), '--dir', temporary_dir, '--loadmodule', |
| 61 | + os.path.abspath(local_module_file)] |
| 62 | + logging.info( |
| 63 | + "Running local redis-server with the following args: {}".format( |
| 64 | + " ".join(command) |
| 65 | + ) |
| 66 | + ) |
| 67 | + redis_process = subprocess.Popen(command) |
| 68 | + result = waitForConn(redis.StrictRedis()) |
| 69 | + if result is True: |
| 70 | + logging.info("Redis available") |
| 71 | + return redis_process |
| 72 | + |
| 73 | + |
| 74 | +def get_local_run_full_filename( |
| 75 | + start_time_str, |
| 76 | + github_branch, |
| 77 | + test_name, |
| 78 | +): |
| 79 | + benchmark_output_filename = ( |
| 80 | + "{start_time_str}-{github_branch}-{test_name}.json".format( |
| 81 | + start_time_str=start_time_str, |
| 82 | + github_branch=github_branch, |
| 83 | + test_name=test_name, |
| 84 | + ) |
| 85 | + ) |
| 86 | + return benchmark_output_filename |
| 87 | + |
| 88 | + |
| 89 | +def prepareSingleBenchmarkCommand( |
| 90 | + executable_path: str, |
| 91 | + server_private_ip: object, |
| 92 | + server_plaintext_port: object, |
| 93 | + benchmark_config: object, |
| 94 | + results_file: object, |
| 95 | +) -> str: |
| 96 | + """ |
| 97 | + Prepares redisgraph-benchmark-go command parameters |
| 98 | + :param server_private_ip: |
| 99 | + :param server_plaintext_port: |
| 100 | + :param benchmark_config: |
| 101 | + :param results_file: |
| 102 | + :return: string containing the required command to run the benchmark given the configurations |
| 103 | + """ |
| 104 | + queries_str = [executable_path] |
| 105 | + for k in benchmark_config["queries"]: |
| 106 | + query = k["q"] |
| 107 | + queries_str.extend(["-query", "{}".format(query)]) |
| 108 | + if "ratio" in k: |
| 109 | + queries_str.extend(["-query-ratio", "{}".format(k["ratio"])]) |
| 110 | + for k in benchmark_config["clientconfig"]: |
| 111 | + if "graph" in k: |
| 112 | + queries_str.extend(["-graph-key", "{}".format(k["graph"])]) |
| 113 | + if "clients" in k: |
| 114 | + queries_str.extend(["-c", "{}".format(k["clients"])]) |
| 115 | + if "requests" in k: |
| 116 | + queries_str.extend(["-n", "{}".format(k["requests"])]) |
| 117 | + if "rps" in k: |
| 118 | + queries_str.extend(["-rps", "{}".format(k["rps"])]) |
| 119 | + queries_str.extend(["-h", "{}".format(server_private_ip)]) |
| 120 | + queries_str.extend(["-p", "{}".format(server_plaintext_port)]) |
| 121 | + queries_str.extend(["-json-out-file", "{}".format(results_file)]) |
| 122 | + logging.info( |
| 123 | + "Running the benchmark with the following parameters: {}".format( |
| 124 | + " ".join(queries_str) |
| 125 | + ) |
| 126 | + ) |
| 127 | + return queries_str |
0 commit comments