refactor: move bytecounter to internal (#391)

It's generic enough to live outside of engine/netx.

Occurred to me while working on https://github.com/ooni/probe/issues/1687.
This commit is contained in:
Simone Basso
2021-06-22 13:00:29 +02:00
committed by GitHub
parent 520398dd8e
commit 23bc261464
14 changed files with 23 additions and 20 deletions
+1 -1
View File
@@ -9,9 +9,9 @@ import (
"os"
"time"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
@@ -6,8 +6,8 @@ import (
"time"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
)
// resolvemaker contains rules for making a resolver.
@@ -6,7 +6,7 @@ import (
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
)
func TestDefaultByteCounter(t *testing.T) {
@@ -33,7 +33,7 @@ import (
"sync"
"time"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/multierror"
"github.com/ooni/probe-cli/v3/internal/runtimex"
)
@@ -1,54 +0,0 @@
package bytecounter
import "github.com/ooni/probe-cli/v3/internal/atomicx"
// Counter counts bytes sent and received.
type Counter struct {
Received *atomicx.Int64
Sent *atomicx.Int64
}
// New creates a new Counter.
func New() *Counter {
return &Counter{Received: &atomicx.Int64{}, Sent: &atomicx.Int64{}}
}
// CountBytesSent adds count to the bytes sent counter.
func (c *Counter) CountBytesSent(count int) {
c.Sent.Add(int64(count))
}
// CountKibiBytesSent adds 1024*count to the bytes sent counter.
func (c *Counter) CountKibiBytesSent(count float64) {
c.Sent.Add(int64(1024 * count))
}
// BytesSent returns the bytes sent so far.
func (c *Counter) BytesSent() int64 {
return c.Sent.Load()
}
// KibiBytesSent returns the KiB sent so far.
func (c *Counter) KibiBytesSent() float64 {
return float64(c.BytesSent()) / 1024
}
// CountBytesReceived adds count to the bytes received counter.
func (c *Counter) CountBytesReceived(count int) {
c.Received.Add(int64(count))
}
// CountKibiBytesReceived adds 1024*count to the bytes received counter.
func (c *Counter) CountKibiBytesReceived(count float64) {
c.Received.Add(int64(1024 * count))
}
// BytesReceived returns the bytes received so far.
func (c *Counter) BytesReceived() int64 {
return c.Received.Load()
}
// KibiBytesReceived returns the KiB received so far.
func (c *Counter) KibiBytesReceived() float64 {
return float64(c.BytesReceived()) / 1024
}
@@ -1,27 +0,0 @@
package bytecounter_test
import (
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
)
func TestGood(t *testing.T) {
counter := bytecounter.New()
counter.CountBytesReceived(16384)
counter.CountKibiBytesReceived(10)
counter.CountBytesSent(2048)
counter.CountKibiBytesSent(10)
if counter.BytesSent() != 12288 {
t.Fatal("invalid bytes sent")
}
if counter.BytesReceived() != 26624 {
t.Fatal("invalid bytes received")
}
if v := counter.KibiBytesSent(); v < 11.9 || v > 12.1 {
t.Fatal("invalid kibibytes sent")
}
if v := counter.KibiBytesReceived(); v < 25.9 || v > 26.1 {
t.Fatal("invalid kibibytes received")
}
}
+1 -1
View File
@@ -4,7 +4,7 @@ import (
"context"
"net"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
)
// byteCounterDialer is a byte-counting-aware dialer. To perform byte counting, you
@@ -8,7 +8,7 @@ import (
"net/http"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx/mockablex"
"github.com/ooni/probe-cli/v3/internal/iox"
)
@@ -4,7 +4,7 @@ import (
"io"
"net/http"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
)
// ByteCountingTransport is a RoundTripper that counts bytes.
@@ -8,7 +8,7 @@ import (
"strings"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
"github.com/ooni/probe-cli/v3/internal/iox"
)
+1 -1
View File
@@ -7,8 +7,8 @@ import (
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx/errorx"
"github.com/ooni/probe-cli/v3/internal/engine/netx/trace"
"github.com/ooni/probe-cli/v3/internal/iox"
+1 -1
View File
@@ -31,7 +31,7 @@ import (
"net/url"
"github.com/lucas-clemente/quic-go"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx/dialer"
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
"github.com/ooni/probe-cli/v3/internal/engine/netx/quicdialer"
+1 -1
View File
@@ -8,8 +8,8 @@ import (
"testing"
"github.com/apex/log"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/netx/httptransport"
"github.com/ooni/probe-cli/v3/internal/engine/netx/resolver"
"github.com/ooni/probe-cli/v3/internal/engine/netx/tlsdialer"
+1 -1
View File
@@ -11,11 +11,11 @@ import (
"sync"
"github.com/ooni/probe-cli/v3/internal/atomicx"
"github.com/ooni/probe-cli/v3/internal/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/geolocate"
"github.com/ooni/probe-cli/v3/internal/engine/internal/sessionresolver"
"github.com/ooni/probe-cli/v3/internal/engine/model"
"github.com/ooni/probe-cli/v3/internal/engine/netx"
"github.com/ooni/probe-cli/v3/internal/engine/netx/bytecounter"
"github.com/ooni/probe-cli/v3/internal/engine/probeservices"
"github.com/ooni/probe-cli/v3/internal/kvstore"
"github.com/ooni/probe-cli/v3/internal/platform"