github.com/asynkron/protoactor-go@v0.0.0-20240308120642-ef91a6abee75/actor/pidset_test.go (about)

     1  package actor
     2  
     3  import (
     4  	"math/rand"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func TestPIDSet_Empty(t *testing.T) {
    12  	var s PIDSet
    13  	assert.True(t, s.Empty())
    14  }
    15  
    16  func TestPIDSet_Clear(t *testing.T) {
    17  	var s PIDSet
    18  	s.Add(NewPID(localAddress, "p1"))
    19  	s.Add(NewPID(localAddress, "p2"))
    20  	s.Add(NewPID(localAddress, "p3"))
    21  	assert.Equal(t, 3, s.Len())
    22  	s.Clear()
    23  	assert.True(t, s.Empty())
    24  	assert.Len(t, s.pids, 0)
    25  }
    26  
    27  func TestPIDSet_Remove(t *testing.T) {
    28  	var s PIDSet
    29  	s.Add(NewPID(localAddress, "p1"))
    30  	s.Add(NewPID(localAddress, "p2"))
    31  	s.Add(NewPID(localAddress, "p3"))
    32  	assert.Equal(t, 3, s.Len())
    33  
    34  	s.Remove(NewPID(localAddress, "p3"))
    35  	assert.Equal(t, 2, s.Len())
    36  	assert.False(t, s.Contains(NewPID(localAddress, "p3")))
    37  }
    38  
    39  func TestPIDSet_AddSmall(t *testing.T) {
    40  	s := NewPIDSet()
    41  	p1 := NewPID(localAddress, "p1")
    42  	s.Add(p1)
    43  	assert.False(t, s.Empty())
    44  	p1 = NewPID(localAddress, "p1")
    45  	s.Add(p1)
    46  	assert.Equal(t, 1, s.Len())
    47  }
    48  
    49  func TestPIDSet_Values(t *testing.T) {
    50  	var s PIDSet
    51  	s.Add(NewPID(localAddress, "p1"))
    52  	s.Add(NewPID(localAddress, "p2"))
    53  	s.Add(NewPID(localAddress, "p3"))
    54  	assert.False(t, s.Empty())
    55  
    56  	r := s.Values()
    57  	assert.Len(t, r, 3)
    58  }
    59  
    60  func TestPIDSet_AddMap(t *testing.T) {
    61  	s := NewPIDSet()
    62  	p1 := NewPID(localAddress, "p1")
    63  	s.Add(p1)
    64  	assert.False(t, s.Empty())
    65  	p1 = NewPID(localAddress, "p1")
    66  	s.Add(p1)
    67  	assert.Equal(t, 1, s.Len())
    68  }
    69  
    70  var pids []*PID
    71  
    72  func init() {
    73  	for i := 0; i < 100000; i++ {
    74  		pids = append(pids, NewPID(localAddress, "p"+strconv.Itoa(i)))
    75  	}
    76  }
    77  
    78  func BenchmarkPIDSet_Add(b *testing.B) {
    79  	cases := []struct {
    80  		l int
    81  	}{
    82  		{l: 1},
    83  		{l: 5},
    84  		{l: 20},
    85  		{l: 500},
    86  	}
    87  
    88  	for _, tc := range cases {
    89  		b.Run("len "+strconv.Itoa(tc.l), func(b *testing.B) {
    90  			pidSetAdd(b, pids[:tc.l])
    91  		})
    92  	}
    93  }
    94  
    95  func pidSetAdd(b *testing.B, data []*PID) {
    96  	for i := 0; i < b.N; i++ {
    97  		var s PIDSet
    98  		for j := 0; j < len(data); j++ {
    99  			s.Add(data[j])
   100  		}
   101  	}
   102  }
   103  
   104  func BenchmarkPIDSet_AddRemove(b *testing.B) {
   105  	cases := []struct {
   106  		l int
   107  	}{
   108  		{l: 1},
   109  		{l: 5},
   110  		{l: 20},
   111  		{l: 500},
   112  	}
   113  
   114  	for _, tc := range cases {
   115  		b.Run("len "+strconv.Itoa(tc.l), func(b *testing.B) {
   116  			pidSetAddRemove(b, pids[:tc.l])
   117  		})
   118  	}
   119  }
   120  
   121  func pidSetAddRemove(b *testing.B, data []*PID) {
   122  	for i := 0; i < b.N; i++ {
   123  		var s PIDSet
   124  		for j := 0; j < len(data); j++ {
   125  			s.Add(data[j])
   126  		}
   127  		for j := 0; j < len(data); j++ {
   128  			s.Remove(data[j])
   129  		}
   130  	}
   131  }
   132  
   133  func BenchmarkPIDSet(b *testing.B) {
   134  	cases := []struct {
   135  		l int
   136  	}{
   137  		{l: 1},
   138  		{l: 5},
   139  		{l: 20},
   140  		{l: 500},
   141  		{l: 1000},
   142  		{l: 10000},
   143  		{l: 100000},
   144  	}
   145  
   146  	for _, tc := range cases {
   147  		b.Run("len "+strconv.Itoa(tc.l), func(b *testing.B) {
   148  			b.StopTimer()
   149  			var s PIDSet
   150  			for i := 0; i < tc.l; i++ {
   151  				s.Add(pids[i])
   152  			}
   153  			b.StartTimer()
   154  
   155  			for i := 0; i < b.N; i++ {
   156  				pid := pids[rand.Intn(len(pids))]
   157  
   158  				s.Add(pid)
   159  
   160  				s.Remove(s.Get(rand.Intn(s.Len())))
   161  			}
   162  		})
   163  	}
   164  }