|
| 1 | +import os |
| 2 | +import argparse |
| 3 | +from ruamel.yaml import YAML |
| 4 | +import collections |
| 5 | + |
| 6 | +# Command groups mapping |
| 7 | +COMMAND_GROUPS = { |
| 8 | + "string": ["set", "get", "append", "getbit", "setrange", "bitcount", "mget"], |
| 9 | + "hash": [ |
| 10 | + "hset", |
| 11 | + "hget", |
| 12 | + "hincrby", |
| 13 | + "hmset", |
| 14 | + "hdel", |
| 15 | + "hscan", |
| 16 | + "hexists", |
| 17 | + "hkeys", |
| 18 | + "hvals", |
| 19 | + "hmget", |
| 20 | + "hsetnx", |
| 21 | + "hgetall", |
| 22 | + ], |
| 23 | + "list": ["lpush", "rpop", "lpop", "lrem", "lrange", "lindex", "lpos", "linsert"], |
| 24 | + "set": [ |
| 25 | + "sadd", |
| 26 | + "smembers", |
| 27 | + "sismember", |
| 28 | + "sunion", |
| 29 | + "sdiff", |
| 30 | + "sinter", |
| 31 | + "smismember", |
| 32 | + "sscan", |
| 33 | + ], |
| 34 | + "sorted_set": [ |
| 35 | + "zadd", |
| 36 | + "zrange", |
| 37 | + "zrevrange", |
| 38 | + "zrangebyscore", |
| 39 | + "zrevrangebyscore", |
| 40 | + "zincrby", |
| 41 | + "zrem", |
| 42 | + "zscore", |
| 43 | + "zrank", |
| 44 | + "zunion", |
| 45 | + "zunionstore", |
| 46 | + "zrevrank", |
| 47 | + "zscan", |
| 48 | + "zcard", |
| 49 | + ], |
| 50 | + "stream": ["xadd", "xread"], |
| 51 | + "geospatial": ["geosearch", "geopos", "geohash", "geodist"], |
| 52 | + "key_management": [ |
| 53 | + "expire", |
| 54 | + "pexpire", |
| 55 | + "ttl", |
| 56 | + "expireat", |
| 57 | + "touch", |
| 58 | + "del", |
| 59 | + "exists", |
| 60 | + ], |
| 61 | + "pubsub": ["ping", "hello"], |
| 62 | + "scripting": ["eval", "evalsha"], |
| 63 | + "transaction": ["multi", "exec"], |
| 64 | + "hyperloglog": ["pfadd"], |
| 65 | + "server_management": ["hello"], |
| 66 | +} |
| 67 | + |
| 68 | + |
| 69 | +def parse_arguments(arguments): |
| 70 | + """ |
| 71 | + Parses the memtier benchmark arguments to extract relevant parameters. |
| 72 | + Specifically extracts the --command argument. |
| 73 | +
|
| 74 | + Args: |
| 75 | + arguments (str): The arguments string from the YAML file. |
| 76 | +
|
| 77 | + Returns: |
| 78 | + dict: A dictionary containing extracted parameters. |
| 79 | + """ |
| 80 | + params = {} |
| 81 | + command = None |
| 82 | + |
| 83 | + for arg in arguments.split(): |
| 84 | + if arg.startswith("--command="): |
| 85 | + command = arg.split("=", 1)[1] |
| 86 | + elif arg == "--command": |
| 87 | + command = arguments.split()[arguments.split().index(arg) + 1] |
| 88 | + |
| 89 | + return command |
| 90 | + |
| 91 | + |
| 92 | +def categorize_command(command): |
| 93 | + """ |
| 94 | + Categorize a Redis command into a command group. |
| 95 | +
|
| 96 | + Args: |
| 97 | + command (str): The Redis command. |
| 98 | +
|
| 99 | + Returns: |
| 100 | + str: The command group. |
| 101 | + """ |
| 102 | + for group, commands in COMMAND_GROUPS.items(): |
| 103 | + if command in commands: |
| 104 | + return group |
| 105 | + return "unknown" |
| 106 | + |
| 107 | + |
| 108 | +def summarize_yaml_file(yaml_file_path, command_summary, command_group_summary): |
| 109 | + """ |
| 110 | + Processes a single YAML file to extract the tested commands and groups. |
| 111 | +
|
| 112 | + Args: |
| 113 | + yaml_file_path (str): Path to the YAML file. |
| 114 | + command_summary (dict): Dictionary to store the command summary. |
| 115 | + command_group_summary (dict): Dictionary to store the command group summary. |
| 116 | + """ |
| 117 | + yaml = YAML() |
| 118 | + yaml.preserve_quotes = True |
| 119 | + |
| 120 | + try: |
| 121 | + with open(yaml_file_path, "r") as file: |
| 122 | + config = yaml.load(file) |
| 123 | + except Exception as e: |
| 124 | + print(f"Error reading {yaml_file_path}: {e}") |
| 125 | + return |
| 126 | + |
| 127 | + # Extract tested commands from 'tested-commands' |
| 128 | + tested_commands = config.get("tested-commands", []) |
| 129 | + for command in tested_commands: |
| 130 | + command_summary["tested_commands"][command] += 1 |
| 131 | + command_group = categorize_command(command) |
| 132 | + command_group_summary[command_group] += 1 |
| 133 | + |
| 134 | + # Extract command from 'clientconfig.arguments' |
| 135 | + arguments = config.get("clientconfig", {}).get("arguments", "") |
| 136 | + if arguments: |
| 137 | + command = parse_arguments(arguments) |
| 138 | + if command: |
| 139 | + command_summary["client_arguments_commands"][command] += 1 |
| 140 | + command_group = categorize_command(command) |
| 141 | + command_group_summary[command_group] += 1 |
| 142 | + |
| 143 | + |
| 144 | +def summarize_directory(directory): |
| 145 | + """ |
| 146 | + Summarizes the commands and command groups across all YAML files in a directory. |
| 147 | +
|
| 148 | + Args: |
| 149 | + directory (str): Path to the directory containing YAML files. |
| 150 | + """ |
| 151 | + command_summary = { |
| 152 | + "tested_commands": collections.Counter(), |
| 153 | + "client_arguments_commands": collections.Counter(), |
| 154 | + } |
| 155 | + command_group_summary = collections.Counter() |
| 156 | + |
| 157 | + # Iterate over all YAML files in the directory |
| 158 | + for filename in os.listdir(directory): |
| 159 | + if filename.endswith(".yml") or filename.endswith(".yaml"): |
| 160 | + yaml_file_path = os.path.join(directory, filename) |
| 161 | + summarize_yaml_file(yaml_file_path, command_summary, command_group_summary) |
| 162 | + |
| 163 | + # Print summary |
| 164 | + print("\nTested Commands Summary:") |
| 165 | + for command, count in command_summary["tested_commands"].items(): |
| 166 | + print(f"{command}: {count} occurrences") |
| 167 | + |
| 168 | + print("\nClient Arguments Commands Summary:") |
| 169 | + for command, count in command_summary["client_arguments_commands"].items(): |
| 170 | + print(f"{command}: {count} occurrences") |
| 171 | + |
| 172 | + print("\nCommand Group Summary:") |
| 173 | + for group, count in command_group_summary.items(): |
| 174 | + print(f"{group.capitalize()}: {count} occurrences") |
| 175 | + |
| 176 | + |
| 177 | +def main(): |
| 178 | + parser = argparse.ArgumentParser( |
| 179 | + description="Summarize commands and command groups from YAML benchmark files." |
| 180 | + ) |
| 181 | + parser.add_argument( |
| 182 | + "--directory", |
| 183 | + type=str, |
| 184 | + default="../redis_benchmarks_specification/test-suites/", |
| 185 | + help="Path to the directory containing YAML test files.", |
| 186 | + ) |
| 187 | + |
| 188 | + args = parser.parse_args() |
| 189 | + directory = args.directory |
| 190 | + |
| 191 | + if not os.path.isdir(directory): |
| 192 | + print(f"Directory {directory} does not exist.") |
| 193 | + return |
| 194 | + |
| 195 | + summarize_directory(directory) |
| 196 | + |
| 197 | + |
| 198 | +if __name__ == "__main__": |
| 199 | + main() |
0 commit comments