Skip to content

Commit eece43b

Browse files
authored
latency: don't wrap when all the latencies are zero (#7506)
1 parent 366decf commit eece43b

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

benchmark/latency/latency.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,18 @@ var (
7373
Longhaul = Network{1000 * 1024, 200 * time.Millisecond, 9000}
7474
)
7575

76+
func (n *Network) isLocal() bool {
77+
return *n == Local
78+
}
79+
7680
// Conn returns a net.Conn that wraps c and injects n's latency into that
7781
// connection. This function also imposes latency for connection creation.
7882
// If n's Latency is lower than the measured latency in c, an error is
7983
// returned.
8084
func (n *Network) Conn(c net.Conn) (net.Conn, error) {
85+
if n.isLocal() {
86+
return c, nil
87+
}
8188
start := now()
8289
nc := &conn{Conn: c, network: n, readBuf: new(bytes.Buffer)}
8390
if err := nc.sync(); err != nil {
@@ -246,6 +253,9 @@ func (c *conn) sync() error {
246253
// Listener returns a net.Listener that wraps l and injects n's latency in its
247254
// connections.
248255
func (n *Network) Listener(l net.Listener) net.Listener {
256+
if n.isLocal() {
257+
return l
258+
}
249259
return &listener{Listener: l, network: n}
250260
}
251261

@@ -265,6 +275,9 @@ func (l *listener) Accept() (net.Conn, error) {
265275
// Dialer returns a Dialer that wraps d and injects n's latency in its
266276
// connections. n's Latency is also injected to the connection's creation.
267277
func (n *Network) Dialer(d Dialer) Dialer {
278+
if n.isLocal() {
279+
return d
280+
}
268281
return func(network, address string) (net.Conn, error) {
269282
conn, err := d(network, address)
270283
if err != nil {
@@ -278,6 +291,9 @@ func (n *Network) Dialer(d Dialer) Dialer {
278291
// in its connections. n's Latency is also injected to the connection's
279292
// creation.
280293
func (n *Network) TimeoutDialer(d TimeoutDialer) TimeoutDialer {
294+
if n.isLocal() {
295+
return d
296+
}
281297
return func(network, address string, timeout time.Duration) (net.Conn, error) {
282298
conn, err := d(network, address, timeout)
283299
if err != nil {
@@ -291,6 +307,9 @@ func (n *Network) TimeoutDialer(d TimeoutDialer) TimeoutDialer {
291307
// in its connections. n's Latency is also injected to the connection's
292308
// creation.
293309
func (n *Network) ContextDialer(d ContextDialer) ContextDialer {
310+
if n.isLocal() {
311+
return d
312+
}
294313
return func(ctx context.Context, network, address string) (net.Conn, error) {
295314
conn, err := d(ctx, network, address)
296315
if err != nil {

0 commit comments

Comments
 (0)