ネットワーク上の場所に100MBのファイルを書き込み、書き込みと読み取りにかかった時間を記録するPythonスクリプト

ネットワーク上の場所に100MBのファイルを書き込み、書き込みと読み取りにかかった時間を記録するPythonスクリプトに関する情報です。最近、Pythonを学ぶようになって、色んな使い方を模索していますが、色々とパーツを知っているとのちに助かるので、使えそうな情報を備忘録代わりに掲載しています。

 

そして、ネットワーク上の書き込みにどれくらいかかるかの数字を出すのに使えそうなPythonスクリプトです。

 

情報元はこちら。

 

ネットワークの場所からファイルの読み取り/書き込み速度を取得します※元は英語です

https://community.spiceworks.com/scripts/show/2490-get-the-file-read-write-speed-from-network-locations

 

 

スクリプトの説明の抜粋です。

 

結果をExcelスプレッドシートに入力する前に、ネットワーク上の場所に200MBのファイルを書き込み、ファイルの書き込みと読み取りにかかった時間を記録するために作成した短いPythonスクリプト

これを機能させるには、pywin32をインストールする必要があります!!!

また、実行を開始するときはExcelを閉じる必要があります。これは、終了時に作業を保存するように求めずに自動的に閉じるように設定されているため、悪いことが起こる可能性があるためです。

事前にExcelを開いていて、スクリプトを中断した場合は、Excelプロセスを手動で強制終了する必要があります。そうしないと、問題が発生する可能性があります。

 

続いてコードの抜粋です。

 

------

# import modules
import time, pythoncom, os, datetime
from win32com.client import Dispatch

now = datetime.datetime.now()

# define speedTest function
def speedTest(myPath, writeCol, readCol):
# create file - 'write' speed test
size_MB = int(200)
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()

#time taken
elapsed = (time.clock() -start)
writeMBps = size_MB / elapsed
writeMbps = writeMBps * 8


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

#time taken
elapsedRead = (time.clock() -startRead)
readMBps = size_MB / elapsedRead
readMbps = readMBps * 8
f.close()

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

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

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

#Read speed result
#
#loop until empty cell is found in column
row = 1400
empty = False

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

#print results on screen
if writeMbps < 150: #obviously 150 is relative to how you expect your network to perform
print("WARNING: Write Speed extremely low!:", writeMbps)
else:
print("Write Speed:\t",writeMbps)

if readMbps < 150:
print("WARNING: Read Speed extremely low!:", readMbps)
else:
print("Read Speed:\t",readMbps)

#write date to Excel
#initialise values
col = 1
row = 2
empty = False

#start excel
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Add(r'#location of the excel file you want to record the results in')
ws = wb.Worksheets(1)

# loop until empty cell is found in date column
# writes data and time in the last empty cell in the column
while not empty:
val = ws.Cells(row,col).value
if val == None:
ws.Cells(row,col).value = now.strftime("%d/%m/%Y %H:%M")
empty = True
row += 1


#call speedTest

print("Network Location 1")
speedTest(r"UNC path to folder for location 1",2,3)

print("Network Location 2")
speedTest(r"UNC path to folder for location 2",4,5)

print("Network Location 3")
speedTest(r"UNC path to folder for location 3",6,7)

print("Network Location 4")
speedTest(r"UNC path to folder for location 4",8,9)

# you can add or remove speedTests to suit your needs

 

#save & exit Excel
xl.DisplayAlerts = False
wb.SaveAs("#location of the excel file you want to record the results in")
wb.Close()
xl.Quit()

pythoncom.CoUninitialize()

# input to exit so the user can review the test before it disappears
input("Press enter to exit.")

------

 

ファイルの読み取り、書き込みがどれくらいで完了したかを確認するのにいいかもしれないですね。

 

【Amazon.co.jp 限定】Dell モニター 23.8インチ SE2416H(3年間交換保証/CIE1976 84%/広視野角/フレームレス/フルHD/IPS非光沢/HDMI,D-Sub15ピン)