]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Add useful new metrics to connection
authorMatt Joiner <anacrolix@gmail.com>
Wed, 27 Aug 2014 23:32:49 +0000 (09:32 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 27 Aug 2014 23:32:49 +0000 (09:32 +1000)
client.go
connection.go

index bd49d9782fc030cf17a7693dbeb5c29acdf948fc..920875478f83f9ca5d4677e8f5e4af0d0cc758f3 100644 (file)
--- a/client.go
+++ b/client.go
@@ -1255,8 +1255,10 @@ func (me *Client) replenishConnRequests(t *torrent, c *connection) {
        }
 }
 
+// Handle a received chunk from a peer.
 func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) error {
        chunksDownloadedCount.Add(1)
+       c.ChunksReceived++
 
        req := newRequest(msg.Index, msg.Begin, pp.Integer(len(msg.Piece)))
 
@@ -1271,6 +1273,9 @@ func (me *Client) downloadedChunk(t *torrent, c *connection, msg *pp.Message) er
                return nil
        }
 
+       c.UsefulChunksReceived++
+       c.lastUsefulChunkReceived = time.Now()
+
        // Write the chunk out.
        err := t.WriteChunk(int(msg.Index), int64(msg.Begin), msg.Piece)
        if err != nil {
index 323f88edb131360926e490bedc3e3064ce3b84eb..6f84c854c26fd47f030c5f1ff530993166a1bfe5 100644 (file)
@@ -33,6 +33,13 @@ type connection struct {
        post      chan pp.Message
        writeCh   chan []byte
 
+       ChunksReceived       int
+       UsefulChunksReceived int
+
+       lastMessageReceived     time.Time
+       completedHandshake      time.Time
+       lastUsefulChunkReceived time.Time
+
        // Stuff controlled by the local peer.
        Interested bool
        Choked     bool
@@ -64,6 +71,8 @@ func newConnection(sock net.Conn, peb peerExtensionBytes, peerID [20]byte) (c *c
                closing: make(chan struct{}),
                writeCh: make(chan []byte),
                post:    make(chan pp.Message),
+
+               completedHandshake: time.Now(),
        }
        go c.writer()
        go c.writeOptimizer(time.Minute)
@@ -119,7 +128,7 @@ func (cn *connection) setNumPieces(num int) error {
 }
 
 func (cn *connection) WriteStatus(w io.Writer) {
-       fmt.Fprintf(w, "%q: %s-%s: %s completed, reqs: %d-%d, flags: ", cn.PeerID, cn.Socket.LocalAddr(), cn.Socket.RemoteAddr(), cn.completedString(), len(cn.Requests), len(cn.PeerRequests))
+       fmt.Fprintf(w, "%-90s: %s completed, good chunks: %d/%d reqs: %d-%d, last msg: %.0fs ago age: %.1fmin last useful chunk: %s ago flags: ", fmt.Sprintf("%q: %s-%s", cn.PeerID, cn.Socket.LocalAddr(), cn.Socket.RemoteAddr()), cn.completedString(), cn.UsefulChunksReceived, cn.ChunksReceived, len(cn.Requests), len(cn.PeerRequests), time.Now().Sub(cn.lastMessageReceived).Seconds(), time.Now().Sub(cn.completedHandshake).Minutes(), time.Now().Sub(cn.lastUsefulChunkReceived))
        c := func(b byte) {
                fmt.Fprintf(w, "%c", b)
        }