github.com/cloudwego/kitex@v0.9.0/pkg/rpcinfo/rpcstats_test.go (about)

     1  /*
     2   * Copyright 2021 CloudWeGo Authors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package rpcinfo_test
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"testing"
    23  
    24  	"github.com/cloudwego/kitex/internal"
    25  	"github.com/cloudwego/kitex/internal/test"
    26  	"github.com/cloudwego/kitex/pkg/rpcinfo"
    27  	"github.com/cloudwego/kitex/pkg/stats"
    28  )
    29  
    30  func TestRPCStats(t *testing.T) {
    31  	st := rpcinfo.NewRPCStats()
    32  	st.Record(context.TODO(), stats.RPCStart, stats.StatusInfo, "nothing")
    33  	test.Assert(t, st.Level() == stats.LevelDisabled)
    34  	test.Assert(t, st != nil)
    35  	test.Assert(t, st.GetEvent(stats.RPCStart) == nil)
    36  	test.Assert(t, st.SendSize() == 0)
    37  	test.Assert(t, st.RecvSize() == 0)
    38  	test.Assert(t, st.Error() == nil)
    39  	ok, err := st.Panicked()
    40  	test.Assert(t, !ok && err == nil)
    41  
    42  	st.(internal.Reusable).Recycle()
    43  	mst := rpcinfo.AsMutableRPCStats(st)
    44  	mst.SetLevel(stats.LevelBase)
    45  	st.Record(context.TODO(), stats.RPCStart, stats.StatusInfo, "start rpc")
    46  	test.Assert(t, st.GetEvent(stats.RPCStart) != nil)
    47  	mst.SetSendSize(1024)
    48  	test.Assert(t, st.SendSize() == 1024)
    49  	mst.SetRecvSize(1024)
    50  	test.Assert(t, st.RecvSize() == 1024)
    51  	mst.SetError(errors.New("err"))
    52  	test.Assert(t, st.Error() != nil)
    53  	mst.SetPanicked(errors.New("err"))
    54  	ok, err = st.Panicked()
    55  	test.Assert(t, ok && err != nil)
    56  
    57  	st.(internal.Reusable).Recycle()
    58  	test.Assert(t, st.Level() == stats.LevelDisabled)
    59  	test.Assert(t, st.GetEvent(stats.RPCStart) == nil)
    60  	test.Assert(t, st.SendSize() == 0)
    61  	test.Assert(t, st.RecvSize() == 0)
    62  	test.Assert(t, st.Error() == nil)
    63  	ok, err = st.Panicked()
    64  	test.Assert(t, !ok && err == nil)
    65  
    66  	t.Run("IncrSendSize", func(t *testing.T) {
    67  		st := rpcinfo.NewRPCStats()
    68  
    69  		rpcinfo.AsMutableRPCStats(st).IncrSendSize(10)
    70  		test.Assert(t, st.SendSize() == 10, st.SendSize())
    71  		test.Assert(t, st.LastSendSize() == 10, st.LastSendSize())
    72  
    73  		rpcinfo.AsMutableRPCStats(st).IncrSendSize(15)
    74  		test.Assert(t, st.SendSize() == 25, st.SendSize())
    75  		test.Assert(t, st.LastSendSize() == 15, st.LastSendSize())
    76  	})
    77  
    78  	t.Run("IncrRecvSize", func(t *testing.T) {
    79  		st := rpcinfo.NewRPCStats()
    80  
    81  		rpcinfo.AsMutableRPCStats(st).IncrRecvSize(10)
    82  		test.Assert(t, st.RecvSize() == 10, st.RecvSize())
    83  		test.Assert(t, st.LastRecvSize() == 10, st.LastRecvSize())
    84  
    85  		rpcinfo.AsMutableRPCStats(st).IncrRecvSize(15)
    86  		test.Assert(t, st.RecvSize() == 25, st.RecvSize())
    87  		test.Assert(t, st.LastRecvSize() == 15, st.LastRecvSize())
    88  	})
    89  }
    90  
    91  func BenchmarkCopyForRetry(b *testing.B) {
    92  	b.Run("BenchmarkNewRPCStats", func(b *testing.B) {
    93  		for i := 0; i < b.N; i++ {
    94  			_ = rpcinfo.NewRPCStats()
    95  		}
    96  	})
    97  
    98  	s := rpcinfo.NewRPCStats()
    99  	b.Run("BenchmarkCopyForRetry", func(b *testing.B) {
   100  		s.Record(context.Background(), stats.RPCStart, stats.StatusInfo, "")
   101  		for i := 0; i < b.N; i++ {
   102  			_ = s.CopyForRetry()
   103  		}
   104  	})
   105  }
   106  
   107  func TestNewEvent(t *testing.T) {
   108  	evt := rpcinfo.NewEvent(stats.RPCStart, stats.StatusError, "err")
   109  	test.Assert(t, evt.Event() == stats.RPCStart)
   110  	test.Assert(t, evt.Status() == stats.StatusError)
   111  	test.Assert(t, evt.Info() == "err")
   112  	test.Assert(t, !evt.Time().IsZero())
   113  	test.Assert(t, !evt.IsNil())
   114  }
   115  
   116  func Test_rpcStats_LastSendSize(t *testing.T) {
   117  	stat := rpcinfo.NewRPCStats()
   118  	rpcinfo.AsMutableRPCStats(stat).IncrSendSize(10)
   119  	test.Assert(t, stat.LastSendSize() == 10)
   120  	test.Assert(t, stat.SendSize() == 10)
   121  	rpcinfo.AsMutableRPCStats(stat).IncrSendSize(10)
   122  	test.Assert(t, stat.LastSendSize() == 10)
   123  	test.Assert(t, stat.SendSize() == 20)
   124  }
   125  
   126  func Test_rpcStats_LastRecvSize(t *testing.T) {
   127  	stat := rpcinfo.NewRPCStats()
   128  	rpcinfo.AsMutableRPCStats(stat).IncrRecvSize(10)
   129  	test.Assert(t, stat.LastRecvSize() == 10)
   130  	test.Assert(t, stat.RecvSize() == 10)
   131  	rpcinfo.AsMutableRPCStats(stat).IncrRecvSize(10)
   132  	test.Assert(t, stat.LastRecvSize() == 10)
   133  	test.Assert(t, stat.RecvSize() == 20)
   134  }