是否存在用于创建特定大小的测试文件的Python模块? [英] Is there a Python module for creating test files of specific sizes?
问题描述
我有一台服务器,文件已上传到该服务器.我需要为该服务器配置各种文件大小的上传/响应时间,即上传10kb文件,100mb文件和许多其他大小所需的时间.我想避免手动创建所有文件并将其存储.
I have a server that has files uploaded to it. I need to profile the upload/response time for various file sizes to that server i.e. how long it takes to upload a 10kb file, a 100mb file, and many other sizes. I want to avoid manually creating all of the files and storing them.
是否有一个Python模块可让您创建任意大小的测试文件?我基本上是在寻找类似的东西:
Is there a Python module that lets you create test files of arbitrary sizes? I'm basically looking for something that would work like:
test_1mb_file = test_file_module.create_test_file(size=1048576)
推荐答案
创建1MB文件实际上不需要写1MB:
You don't really need to write 1MB to create a 1MB file:
with open('bigfile', 'wb') as bigfile:
bigfile.seek(1048575)
bigfile.write('0')
另一方面,您真的需要文件吗?许多API都采用任何类似文件的对象".并不总是很清楚这是否意味着read
,read
和seek
,按行迭代或其他方式……但是无论如何,您应该能够模拟一个1MB的文件而不会创建比单个readline
一次.
On the other hand, do you really need a file at all? Many APIs take any "file-like object". It's not always clear whether that means read
, read
and seek
, iterating by lines, or something else… but whatever it is, you should be able to simulate a 1MB file without creating more data than a single read
or readline
at a time.
PS,如果您实际上不是从Python发送文件,而是创建它们以供以后使用,则有专门针对这种情况设计的工具:
PS, if you're not actually sending the files from Python, just creating them to use later, there are tools that are specifically designed for this kind of thing:
dd bs=1024 seek=1024 count=0 if=/dev/null of=bigfile # 1MB uninitialized
dd bs=1024 count=1024 if=/dev/zero of=bigfile # 1MB of zeroes
dd bs=1024 count=1024 if=/dev/random of=bigfile # 1MB of random data
这篇关于是否存在用于创建特定大小的测试文件的Python模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!