|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: 生成memtier 验证数据 |
| 4 | +categories: [database] |
| 5 | +tags: [python, memtier,redis] |
| 6 | +--- |
| 7 | + |
| 8 | +用chatgpt生成的代码改的,chatgpt太强了 |
| 9 | + |
| 10 | +<!-- more --> |
| 11 | + |
| 12 | +直接贴代码了 |
| 13 | + |
| 14 | +```python |
| 15 | + |
| 16 | +import csv |
| 17 | +import time |
| 18 | +import random |
| 19 | +import string |
| 20 | +import argparse |
| 21 | +from typing import Generator, Dict |
| 22 | + |
| 23 | +class RecordGenerator: |
| 24 | + """记录生成器类""" |
| 25 | + def __init__(self): |
| 26 | + self.current_time = int(time.time()) |
| 27 | + |
| 28 | + def generate_random_string(self, min_length: int = 5, max_length: int = 20) -> str: |
| 29 | + """ |
| 30 | + 生成随机字符串 |
| 31 | + """ |
| 32 | + length = random.randint(min_length, max_length) |
| 33 | + res = ''.join(random.choices(string.ascii_lowercase, k=length)) |
| 34 | + return "'" + res + "'" |
| 35 | + |
| 36 | + def generate_record(self) -> Dict: |
| 37 | + """ |
| 38 | + 生成单条记录 |
| 39 | + """ |
| 40 | + # 生成随机key和data |
| 41 | + key = self.generate_random_string(20, 50) |
| 42 | + data = self.generate_random_string(60, 200) |
| 43 | + |
| 44 | + # 计算实际长度 |
| 45 | + actual_key_length = len(key) |
| 46 | + actual_data_length = len(data) |
| 47 | + # To avoid unnecessary error while passing file |
| 48 | + # 1. Add extra 4 bytes in nbytes |
| 49 | + # 2. Add extra 2 bytes in nkey |
| 50 | + # 我们生成的数据是多引号的,已经+2了 |
| 51 | + return { |
| 52 | + 'dumpflags': '0', |
| 53 | + ' time': ' 0', #self.current_time, |
| 54 | + ' exptime': ' 60', #self.current_time + random.randint(60, 3600), |
| 55 | + ' nbytes': f' {actual_data_length +2}', |
| 56 | + ' nsuffix': f' {random.randint(100, 300)}', |
| 57 | + ' it_flags': ' 0', |
| 58 | + ' clsid': ' 1', |
| 59 | + ' nkey': f' {actual_key_length}', ## 已经加2了 |
| 60 | + ' key': f' {key}', |
| 61 | + ' data': f' {data}' |
| 62 | + } |
| 63 | + |
| 64 | + def records(self, num_records: int) -> Generator[Dict, None, None]: |
| 65 | + """ |
| 66 | + 生成记录的生成器 |
| 67 | + """ |
| 68 | + for _ in range(num_records): |
| 69 | + yield self.generate_record() |
| 70 | + |
| 71 | +class CSVWriter: |
| 72 | + """CSV文件写入器类""" |
| 73 | + def __init__(self, filename: str): |
| 74 | + self.filename = filename |
| 75 | + self.fieldnames = [ |
| 76 | + 'dumpflags', ' time', ' exptime', ' nbytes', ' nsuffix', |
| 77 | + ' it_flags', ' clsid', ' nkey', ' key', ' data' |
| 78 | + ] |
| 79 | + |
| 80 | + def write_records(self, records_generator: Generator[Dict, None, None]) -> int: |
| 81 | + """ |
| 82 | + 将记录写入CSV文件 |
| 83 | + 返回写入的记录数量 |
| 84 | + """ |
| 85 | + count = 0 |
| 86 | + try: |
| 87 | + with open(self.filename, 'w', newline='') as csvfile: |
| 88 | + writer = csv.DictWriter(csvfile, fieldnames=self.fieldnames) |
| 89 | + writer.writeheader() |
| 90 | + |
| 91 | + for record in records_generator: |
| 92 | + writer.writerow(record) |
| 93 | + count += 1 |
| 94 | + # 每1000条记录显示一次进度 |
| 95 | + if count % 1000 == 0: |
| 96 | + print(f"已生成 {count} 条记录...") |
| 97 | + |
| 98 | + return count |
| 99 | + except Exception as e: |
| 100 | + print(f"写入文件时发生错误:{e}") |
| 101 | + return count |
| 102 | + |
| 103 | +def parse_arguments() -> argparse.Namespace: |
| 104 | + """解析命令行参数""" |
| 105 | + parser = argparse.ArgumentParser(description='memtierbenchmark data load CSV文件') |
| 106 | + parser.add_argument('-n', '--number', |
| 107 | + type=int, |
| 108 | + default=10, |
| 109 | + help='要生成的记录数量(默认:10)') |
| 110 | + parser.add_argument('-o', '--output', |
| 111 | + type=str, |
| 112 | + default='output.csv', |
| 113 | + help='output.csv)') |
| 114 | + return parser.parse_args() |
| 115 | + |
| 116 | +def main(): |
| 117 | + # 解析命令行参数 |
| 118 | + args = parse_arguments() |
| 119 | + |
| 120 | + try: |
| 121 | + # 验证输入参数 |
| 122 | + if args.number <= 0: |
| 123 | + raise ValueError("记录数量必须大于0") |
| 124 | + |
| 125 | + print(f"开始生成 {args.number} 条记录...") |
| 126 | + start_time = time.time() |
| 127 | + |
| 128 | + # 创建生成器和写入器 |
| 129 | + generator = RecordGenerator() |
| 130 | + writer = CSVWriter(args.output) |
| 131 | + |
| 132 | + # 生成并写入记录 |
| 133 | + records_count = writer.write_records(generator.records(args.number)) |
| 134 | + |
| 135 | + end_time = time.time() |
| 136 | + duration = end_time - start_time |
| 137 | + |
| 138 | + # 输出统计信息 |
| 139 | + print(f"\n完成!") |
| 140 | + print(f"生成文件:{args.output}") |
| 141 | + print(f"记录数量:{records_count}") |
| 142 | + print(f"耗时:{duration:.2f} 秒") |
| 143 | + print(f"平均速度:{records_count/duration:.2f} 记录/秒") |
| 144 | + |
| 145 | + except ValueError as ve: |
| 146 | + print(f"参数错误:{ve}") |
| 147 | + except Exception as e: |
| 148 | + print(f"程序执行出错:{e}") |
| 149 | + |
| 150 | +if __name__ == "__main__": |
| 151 | + main() |
| 152 | + |
| 153 | +# python3 gen.py -n 10000000 -o output.csv |
| 154 | +``` |
0 commit comments