Skip to content

Commit f5b01e3

Browse files
committed
beautiful output for statistic
1 parent 991d546 commit f5b01e3

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

misc.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"container/ring"
66
"encoding/base64"
77
"errors"
8+
"fmt"
89
"io"
910
"os"
1011
"strings"
@@ -15,6 +16,42 @@ import (
1516

1617
const DefaultStatsSecond = 60
1718

19+
type ByteSize float64
20+
21+
const (
22+
_ = iota // ignore first value by assigning to blank identifier
23+
KB ByteSize = 1 << (10 * iota)
24+
MB
25+
GB
26+
TB
27+
PB
28+
EB
29+
ZB
30+
YB
31+
)
32+
33+
func (b ByteSize) String() string {
34+
switch {
35+
case b >= YB:
36+
return fmt.Sprintf("%.2fYB", b/YB)
37+
case b >= ZB:
38+
return fmt.Sprintf("%.2fZB", b/ZB)
39+
case b >= EB:
40+
return fmt.Sprintf("%.2fEB", b/EB)
41+
case b >= PB:
42+
return fmt.Sprintf("%.2fPB", b/PB)
43+
case b >= TB:
44+
return fmt.Sprintf("%.2fTB", b/TB)
45+
case b >= GB:
46+
return fmt.Sprintf("%.2fGB", b/GB)
47+
case b >= MB:
48+
return fmt.Sprintf("%.2fMB", b/MB)
49+
case b >= KB:
50+
return fmt.Sprintf("%.2fKB", b/KB)
51+
}
52+
return fmt.Sprintf("%.2fB", b)
53+
}
54+
1855
// Statistic contains the traffic status
1956
type Statistic struct {
2057
SentBytes uint64

tunnel.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -556,14 +556,20 @@ func (t *TunnelTransport) SetCCIDChannel(c chan CCID) {
556556
}
557557

558558
func (t *TunnelTransport) logStats() {
559+
conn, ok := t.rw.(net.Conn)
560+
if !ok {
561+
return
562+
}
563+
info := fmt.Sprintf("local: %s, remote: %s", conn.LocalAddr(), conn.RemoteAddr())
559564
for {
560565
select {
561566
case <-time.After(DefaultStatsSecond * time.Second):
562-
log.Infof("TT: Statistic - send: %d, received: %d, total send speed: %d, total receive speed: %d",
563-
t.stats.SentBytes, t.stats.ReceivedBytes, t.stats.SendSpeed(), t.stats.ReceiveSpeed())
564-
log.Infof("TT: Statistic - current send speed in %d seconds: %d, current receive speed in %d seconds: %d",
565-
DefaultStatsSecond, t.stats.SendSpeedSecond(DefaultStatsSecond),
566-
DefaultStatsSecond, t.stats.ReceiveSpeedSecond(DefaultStatsSecond))
567+
log.Infof("TT: Statistic - %s - send: %s, receive: %s, total send speed: %s/s, total receive speed: %s/s",
568+
info, ByteSize(t.stats.SentBytes).String(), ByteSize(t.stats.ReceivedBytes).String(),
569+
ByteSize(t.stats.SendSpeed()).String(), ByteSize(t.stats.ReceiveSpeed()).String())
570+
log.Infof("TT: Statistic - %s - current send speed in %ds: %s/s, current receive speed in %ds: %s/s",
571+
info, DefaultStatsSecond, ByteSize(t.stats.SendSpeedSecond(DefaultStatsSecond)).String(),
572+
DefaultStatsSecond, ByteSize(t.stats.ReceiveSpeedSecond(DefaultStatsSecond)).String())
567573
case <-t.quit:
568574
return
569575
}

0 commit comments

Comments
 (0)