gpt4 book ai didi

python 3.3 : LAN speed test which calculates the read & write speeds and then returns the figure to Excel

转载 作者:行者123 更新时间:2023-11-28 20:15:40 29 4
gpt4 key购买 nike

我正在创建一个 LAN 速度测试,它在指定位置创建一个指定大小的数据文件,并记录创建/读取它的速度。在大多数情况下,这是正常工作的,只有一个问题:读取速度快得离谱,因为它所做的只是计算文件打开所需的时间,而不是文件实际可读所需的时间(如果这有意义?)。

到目前为止我有这个:

import time
import pythoncom
from win32com.client import Dispatch
import os

# create file - write speed
myPath = input('Where do you want to write the file?')
size_MB = int(input('What sized file do you want to test with? (MB)'))
size_B = size_MB * 1024 * 1024
fName = '\pydatafile'
#start timer
start = time.clock()
f = open(myPath + fName,'w')
f.write("\x00" * size_B)
f.close()

# how much time it took
elapsed = (time.clock() -start)
print ("It took", elapsed, "seconds to write the", size_MB, "MB file")
time.sleep(1)
writeMBps = size_MB / elapsed
print("That's", writeMBps, "MBps.")
time.sleep(1)
writeMbps = writeMBps * 8
print("Or", writeMbps, "Mbps.")
time.sleep(2)

# open file - read speed
startRead = time.clock()
f = open(myPath + fName,'r')

# how much time it took
elapsedRead = (time.clock() - startRead)
print("It took", elapsedRead,"seconds to read the", size_MB,"MB file")
time.sleep(1)
readMBps = size_MB / elapsedRead
print("That's", readMBps,"MBps.")
time.sleep(1)
readMbps = readMBps * 8
print("Or", readMbps,"Mbps.")
time.sleep(2)
f.close()

# delete the data file
os.remove(myPath + fName)

# record results on Excel
xl = Dispatch('Excel.Application')
xl.visible= 0
wb = xl.Workbooks.Add(r'C:\File\Location')
ws = wb.Worksheets(1)

# Write speed result
#
# loop until empty cell is found in column
col = 1
row = 1
empty = False

while not empty:
val = ws.Cells(row,col).value
print("Looking for next available cell to write to...")
if val == None:
print("Writing result to cell")
ws.Cells(row,col).value = writeMbps
empty = True
row += 1

# Read speed result
#
# loop until empty cell is found in column
col = 2
row = 1
empty = False

while not empty:
val = ws.Cells(row,col).value
print("Looking for next available cell to write to...")
if val == None:
print("Writing result to cell")
ws.Cells(row,col).value = readMbps
empty = True
row += 1


xl.Run('Save')
xl.Quit()

pythoncom.CoUninitialize()

我怎样才能使读取速度正确?

非常感谢

最佳答案

尝试实际读取文件:

f = open(myPath + fName, 'r')
f.read()

或者(如果文件太大,内存放不下):

f = open(myPath + fName, 'r')
while f.read(1024 * 1024):
pass

但是操作系统仍然可以通过缓存文件内容来提高读取速度。你刚刚把它写在那里!即使您设法禁用缓存,您的测量(除了网络速度之外)也可能包括将数据写入文件服务器磁盘的时间。

如果你只想要网络速度,你需要在局域网上使用 2 台独立的机器。例如。运行 echo server在一台机器上(例如通过启用 Simple TCP/IP services 或编写并运行 your own )。然后运行 ​​Python echo client在另一台向回显服务器发送一些数据的机器上,确保它收到相同的数据并测量周转时间。

关于 python 3.3 : LAN speed test which calculates the read & write speeds and then returns the figure to Excel,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18849198/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com