Python Forum
Trying to simulate different file write speed
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to simulate different file write speed
#1
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(?)
Reply
#2
Hello!
The time in time.sleep(1) means one second. So 0.001 means one ms.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
(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
Reply
#4
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 
Reply
#5
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Last record in file doesn't write to newline gonksoup 3 404 Jan-22-2024, 12:56 PM
Last Post: deanhystad
  write to csv file problem jacksfrustration 11 1,503 Nov-09-2023, 01:56 PM
Last Post: deanhystad
  python Read each xlsx file and write it into csv with pipe delimiter mg24 4 1,429 Nov-09-2023, 10:56 AM
Last Post: mg24
  How do I read and write a binary file in Python? blackears 6 6,512 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,091 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  Read text file, modify it then write back Pavel_47 5 1,588 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  how to read txt file, and write into excel with multiply sheet jacklee26 14 9,902 Jan-21-2023, 06:57 AM
Last Post: jacklee26
  How to write in text file - indented block Joni_Engr 4 6,440 Jul-18-2022, 09:09 AM
Last Post: Hathemand
  Upgrading from 2 to 3 and having file write problems KenHorse 2 1,478 May-08-2022, 09:47 PM
Last Post: KenHorse
  Cursor write 3rd file empty paulo79 3 1,872 Mar-10-2022, 02:51 PM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020