Python Forum

Full Version: Trying to simulate different file write speed
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello All,

I am trying to write a code that simulate write file stream with the control on speed throughput (40Mbit/s ,50Mbit/s etc ...) , and using different block size.

I have a basic code that can do full speed stream write , below example i calculating few parameters and the code
My question is that i can't figure out how to calculate sleep time between each write to achieve desired speed write?
Please advise
Thanks

Quote:40Mbit/s ==> 4.7683715820313 Mbyte/sec total 100Mbyte, block 128KB ==>0.125 Mbyte = 800IO's
4.7683715820313/0.125 = 38.1469726562504 frames per second
1000msec = 1 sec /38.1469726562504 frames = 26.21439999999973 Msec per frame write



blocksize = 128000

chunk = b'\xff'*blocksize

with open("//10.0.0.25/Software/Testfile/file.file", "wb") as f:
    seq= 0
    for num in range(1,800):

        f.write(chunk)
        f.seek(seq)
        seq = seq+blocksize
  ====> time.sleep(?)
Hello!
The time in time.sleep(1) means one second. So 0.001 means one ms.
(Apr-18-2017, 12:05 PM)wavic Wrote: [ -> ]Hello!
The time in time.sleep(1) means one second. So 0.001 means one ms.

Thanks for the tip , i know that , i need help with formula how to calculate the amount of sleep time to achieve desired speed
Thanks
You need to ensure that single loop of your for cycle takes computed time, so taking time and waiting could work:
    duration = 0.026214

    start_time = time.time()
    for num in range(1,800):        
        f.write(chunk)
        f.seek(seq)
        seq = seq+blocksize

        start_time += duration
        time.sleep(start_time - time.time())  # raise error if negative 
Hi,
Thanks for the answer code :-) , i also figure out that , better using bytes than MBytes
40 Mbit/s = 41943040a bit/s = 5242880 bytes/s
128 KB = 131072 bytes
5242880 / 131072 = 40 number of blocks you can write in a second
so the delay is 1000ms/40 = 25ms so this also can be my writes sleep