2017-08-21 28 views
0

私はサーバーとクライアントの間のスループットを見つけるためにPythonコードを書いています。速度を計算するためのダミーファイルを送信するspeedtest.netの機能に基づいています。私が直面している問題は、信頼性の低いスループット出力です。私は同じことについてあなたの提案を感謝します。ここにコードがあります。Pythonのスループット値が正しくありません

server.py

import socket 

import os 

port = 60000 
s = socket.socket() 
host = socket.gethostname() 
s.bind((host, port)) 
s.listen(5) 

print 'Server listening....' 

while True: 
    conn, addr = s.accept()  
    print 'Got connection from', addr 
    data = conn.recv(1024) 
    print('Server received', repr(data)) 

    filename='akki.txt' 
    b = os.path.getsize(filename) 
    f = open(filename,'rb') 
    l = f.read(b) 

    while (l): 

     conn.send(l) 

     l = f.read(b) 
    f.close() 

    print('Done sending') 
    conn.send('Thank you for connecting') 
    conn.close() 

Client.py

import socket 
import time 
import os 

s = socket.socket() 
host = socket.gethostname() 
port = 60000 

t1 = time.time() 
s.connect((host, port)) 
s.send("Hello server!") 

with open('received_file', 'wb') as f: 
    print 'file opened' 
    t2 = time.time() 
    while True: 

     data = s.recv(1024) 

     if not data: 
      break 

     f.write(data) 
     t3 = time.time() 

print data 
print 'Total:', t3 - t1 
print 'Throughput:', round((1024.0 * 0.001)/(t3 - t1), 3), 
print 'K/sec.' 
f.close() 
print('Successfully received the file') 
s.close() 
print('connection closed') 

出力

akki.txtサーバー出力

を送ります
Server listening.... 
Got connection from ('10.143.47.165', 60902) 
('Server received', "'Hello server!'") 
Done sending 

クライアント出力 ファイルが

Raw timers: 1503350568.11 1503350568.11 1503350568.11 
Total: 0.00499987602234 
**Throughput: 204.805 K/sec.** 
Successfully received the file 
connection closed 

(大きなファイルです)ak.zip用の出力

クライアント出力 ファイルが

Total: 0.0499999523163 
**Throughput: 20.48 K/sec.** 
Successfully received the file 
connection closed 

答えて

0

ショートを開設開設しましたアンズr:ファイルサイズを考慮する必要があります。

詳細: スループットはデータ/時間です。あなたの計算:

ラウンド((* 0.001 1024.0)/(T3 - T1)、3)

を考慮にファイルサイズを取ることはありません。大きなファイルを送信するには時間がかかりますので、 't3-t1'は大きく、スループットは小さくなります(分母が大きいほど同じ分子)。数式にファイルサイズを追加してみると、さらに一定の結果が得られるはずです。

これが役に立ちます。