github.com/yandex/pandora@v0.5.32/core/aggregator/netsample/sample_test.go (about)

     1  package netsample
     2  
     3  import (
     4  	"fmt"
     5  	"net"
     6  	"net/http"
     7  	"os"
     8  	"strconv"
     9  	"strings"
    10  	"syscall"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/facebookgo/stackerr"
    15  	"github.com/pkg/errors"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func TestSampleBehaviour(t *testing.T) {
    20  	const tag = "test"
    21  	const tag2 = "test2"
    22  	const id = 42
    23  	sample := Acquire(tag)
    24  	sample.AddTag(tag2)
    25  	sample.SetID(id)
    26  	const sleep = time.Millisecond
    27  	time.Sleep(sleep)
    28  	sample.SetErr(syscall.EINVAL)
    29  	rtt := time.Duration(sample.get(keyRTTMicro)) * time.Microsecond
    30  	assert.NotZero(t, rtt)
    31  	assert.True(t, rtt <= time.Since(sample.timeStamp), "expected: %v; actual: %v", rtt, time.Since(sample.timeStamp))
    32  	assert.True(t, sleep <= rtt)
    33  	sample.SetProtoCode(http.StatusBadRequest)
    34  	expectedTimeStamp := fmt.Sprintf("%v.%3.f",
    35  		sample.timeStamp.Unix(),
    36  		float32((sample.timeStamp.UnixNano()/1e6)%1000))
    37  	// 1484660999.  2 -> 1484660999.002
    38  	expectedTimeStamp = strings.Replace(expectedTimeStamp, " ", "0", -1)
    39  
    40  	expected := fmt.Sprintf("%s\t%s|%s#%v\t%v\t0\t0\t0\t0\t0\t0\t0\t%v\t%v",
    41  		expectedTimeStamp,
    42  		tag, tag2, id,
    43  		sample.get(keyRTTMicro),
    44  		int(syscall.EINVAL), http.StatusBadRequest,
    45  	)
    46  	assert.Equal(t, expected, sample.String())
    47  }
    48  
    49  func TestCustomSets(t *testing.T) {
    50  	const tag = "UserDefine"
    51  	s := Acquire(tag)
    52  
    53  	userDuration := 100 * time.Millisecond
    54  	s.SetUserDuration(userDuration)
    55  
    56  	s.SetUserProto(0)
    57  	s.SetUserNet(110)
    58  
    59  	latency := 200 * time.Millisecond
    60  	s.SetLatency(latency)
    61  
    62  	reqBytes := 4
    63  	s.SetRequestBytes(reqBytes)
    64  
    65  	respBytes := 8
    66  	s.SetResponseBytes(respBytes)
    67  
    68  	expectedTimeStamp := fmt.Sprintf("%v.%3.f",
    69  		s.timeStamp.Unix(),
    70  		float32((s.timeStamp.UnixNano()/1e6)%1000))
    71  	expectedTimeStamp = strings.Replace(expectedTimeStamp, " ", "0", -1)
    72  	expected := fmt.Sprintf("%s\t%s#0\t%v\t0\t0\t%v\t0\t0\t%v\t%v\t%v\t%v",
    73  		expectedTimeStamp,
    74  		tag,
    75  		int(userDuration.Nanoseconds()/1000), // keyRTTMicro
    76  		int(latency.Nanoseconds()/1000),      // keyLatencyMicro
    77  		reqBytes,                             // keyRequestBytes
    78  		respBytes,                            // keyResponseBytes
    79  		110,
    80  		0,
    81  	)
    82  	assert.Equal(t, s.String(), expected)
    83  }
    84  
    85  func TestGetErrno(t *testing.T) {
    86  	var err error = syscall.EINVAL
    87  	err = &os.SyscallError{Err: err}
    88  	err = &net.OpError{Err: err}
    89  	err = errors.WithStack(err)
    90  	err = stackerr.Wrap(err)
    91  	assert.NotZero(t, getErrno(err))
    92  	assert.Equal(t, int(syscall.EINVAL), getErrno(err))
    93  }
    94  
    95  // TODO (skipor): test getErrno on some real net error from stdlib.
    96  
    97  func BenchmarkAppendTimestamp(b *testing.B) {
    98  	dst := make([]byte, 0, 512)
    99  	ts := time.Now()
   100  	b.Run("DotInsert", func(b *testing.B) {
   101  		for i := 0; i < b.N; i++ {
   102  			appendTimestamp(ts, dst)
   103  			dst = dst[:0]
   104  		}
   105  	})
   106  	b.Run("UseMod", func(b *testing.B) {
   107  		for i := 0; i < b.N; i++ {
   108  			dst = strconv.AppendInt(dst, ts.Unix(), 10)
   109  			dst = append(dst, '.')
   110  			dst = strconv.AppendInt(dst, (ts.UnixNano()/1e3)%1e3, 10)
   111  			dst = dst[:0]
   112  		}
   113  	})
   114  	b.Run("NoDot", func(b *testing.B) {
   115  		for i := 0; i < b.N; i++ {
   116  			dst = strconv.AppendInt(dst, ts.UnixNano()/1e3, 10)
   117  			dst = dst[:0]
   118  		}
   119  	})
   120  }