2

我正在创建一个 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()

我怎样才能做到这一点,以便读取速度正确?

非常感谢

4

1 回答 1

0

尝试实际读取文件:

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

或者(如果文件太大而无法放入内存):

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

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

如果您只想要网络速度,则需要在 LAN 上使用 2 台独立的机器。例如,在一台机器上运行echo 服务器(例如,通过启用Simple TCP/IP 服务或编写和运行您自己的)。然后在另一台向回显服务器发送一些数据的机器上运行Python 回显客户端,确保它接收到相同的数据并测量周转时间。

于 2013-09-17T12:05:03.177 回答