Last active
June 20, 2025 09:34
-
-
Save LittleNewton/20e1012b8e982bb08a347f2cbd8dc787 to your computer and use it in GitHub Desktop.
test_checksum.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # test_checksum.py | |
| import os | |
| FILE_NAME = 'testfile.bin' | |
| FILE_SIZE = 1 * 1024 * 1024 * 1024 # 1 GB | |
| BLOCK_SIZE = 64 * 1024 # 64 KB | |
| def generate_rand_file(): | |
| """创建一个 1GB 随机数据的文件""" | |
| with open(FILE_NAME, 'wb') as f: | |
| for _ in range(FILE_SIZE // BLOCK_SIZE): | |
| block = os.urandom(BLOCK_SIZE) | |
| f.write(block) | |
| print(f"文件 {FILE_NAME} 已创建, 大小 {FILE_SIZE // (1024 * 1024)} MB") | |
| def load_file(): | |
| """读取整个文件到内存并返回字节数组""" | |
| with open(FILE_NAME, 'rb') as f: | |
| file_bytes = f.read() | |
| print(f"文件 {FILE_NAME} 已加载到内存, 总大小 {len(file_bytes) // (1024 * 1024)} MB") | |
| return file_bytes | |
| def overwrite_file(file_bytes): | |
| """用file_bytes对文件进行 64K 对齐的覆盖写""" | |
| with open(FILE_NAME, 'r+b') as f: # r+b 模式允许读写 | |
| for offset in range(0, len(file_bytes), BLOCK_SIZE): | |
| block = file_bytes[offset:offset + BLOCK_SIZE] | |
| f.seek(offset) | |
| f.write(block) | |
| print(f"文件 {FILE_NAME} 已完成64K对齐的覆盖写") | |
| def main(): | |
| if not os.path.exists(FILE_NAME): | |
| generate_rand_file() | |
| file_bytes = load_file() | |
| print("开始进行 64K 对齐的覆盖写...") | |
| while True: | |
| overwrite_file(file_bytes) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment