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

标签 python performance excel testing

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

相关文章:

python - 适用于 PostgreSQL 和 MySQL 的纯 python SQL 解决方案?

android - 多步骤流程中的单个 Activity 多个 fragment/单独的 Activity ?

javascript - 我如何以非阻塞方式处理数字?

c# - 使用 C# 将 Sumif 公式添加到单元格错误

excel - 如何测试范围是否为空?

Python 终端仿真

python - 在 Flask shell 中运行 Flask 应用程序时出现 AttributeError

python - 使用自定义估算器控制纪元

performance - 是否有用于在没有(慢速)COM API(互操作)的情况下操作 Excel .xlsb (BIFF12) 文件的库?

python - 在 XlsxWriter 中写入后将格式应用于单元格