]> Sergey Matveev's repositories - btrtrc.git/commitdiff
cmd/torrent: Refactor and set progress interval to 3s
authorMatt Joiner <anacrolix@gmail.com>
Mon, 20 Sep 2021 12:01:18 +0000 (22:01 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 21 Sep 2021 00:48:16 +0000 (10:48 +1000)
cmd/torrent/main.go

index 385d732cae02bb34dad192f844f0263794d3d72e..f27a8538cbb570986b6b7a05f1e29e5562b7a712 100644 (file)
@@ -42,7 +42,8 @@ func torrentBar(t *torrent.Torrent, pieceStates bool) {
                }
                lastStats := t.Stats()
                var lastLine string
-               for range time.Tick(time.Second) {
+               interval := 3 * time.Second
+               for range time.Tick(interval) {
                        var completedPieces, partialPieces int
                        psrs := t.PieceStateRuns()
                        for _, r := range psrs {
@@ -54,6 +55,9 @@ func torrentBar(t *torrent.Torrent, pieceStates bool) {
                                }
                        }
                        stats := t.Stats()
+                       byteRate := int64(time.Second)
+                       byteRate *= stats.BytesReadUsefulData.Int64() - lastStats.BytesReadUsefulData.Int64()
+                       byteRate /= int64(interval)
                        line := fmt.Sprintf(
                                "%v: downloading %q: %s/%s, %d/%d pieces completed (%d partial): %v/s\n",
                                time.Since(start),
@@ -63,7 +67,7 @@ func torrentBar(t *torrent.Torrent, pieceStates bool) {
                                completedPieces,
                                t.NumPieces(),
                                partialPieces,
-                               humanize.Bytes(uint64(stats.BytesReadUsefulData.Int64()-lastStats.BytesReadUsefulData.Int64())),
+                               humanize.Bytes(uint64(byteRate)),
                        )
                        if line != lastLine {
                                lastLine = line