github.com/v2pro/plz@v0.0.0-20221028024117-e5f9aec5b631/witch/witch_test.go (about)

     1  package witch
     2  
     3  import (
     4  	"github.com/v2pro/plz/countlog"
     5  	"math/rand"
     6  	"testing"
     7  	"time"
     8  	"github.com/v2pro/plz/dump"
     9  	"expvar"
    10  	"unsafe"
    11  )
    12  
    13  // A header for a Go map.
    14  type hmap struct {
    15  	count     int // # live cells == size of map.  Must be first (used by len() builtin)
    16  	flags     uint8
    17  	B         uint8  // log_2 of # of buckets (can hold up to loadFactor * 2^B items)
    18  	noverflow uint16 // approximate number of overflow buckets; see incrnoverflow for details
    19  	hash0     uint32 // hash seed
    20  
    21  	buckets    unsafe.Pointer // array of 2^B Buckets. may be nil if count==0.
    22  	oldbuckets unsafe.Pointer // previous bucket array of half the size, non-nil only when growing
    23  	nevacuate  uintptr        // progress counter for evacuation (buckets less than this have been evacuated)
    24  
    25  	extra *mapextra // optional fields
    26  }
    27  
    28  // mapextra holds fields that are not present on all maps.
    29  type mapextra struct {
    30  	// If both key and value do not contain pointers and are inline, then we mark bucket
    31  	// type as containing no pointers. This avoids scanning such maps.
    32  	// However, bmap.overflow is a pointer. In order to keep overflow buckets
    33  	// alive, we store pointers to all overflow buckets in hmap.overflow and h.map.oldoverflow.
    34  	// overflow and oldoverflow are only used if key and value do not contain pointers.
    35  	// overflow contains overflow buckets for hmap.buckets.
    36  	// oldoverflow contains overflow buckets for hmap.oldbuckets.
    37  	// The indirection allows to store a pointer to the slice in hiter.
    38  	overflow    *[]unsafe.Pointer
    39  	oldoverflow *[]unsafe.Pointer
    40  
    41  	// nextOverflow holds a pointer to a free overflow bucket.
    42  	nextOverflow unsafe.Pointer
    43  }
    44  
    45  func init() {
    46  	m := map[int]int{}
    47  	m[1] = 1
    48  	m[2] = 4
    49  	m[3] = 9
    50  	expvar.Publish("map before delete", dump.Snapshot(m))
    51  	delete(m, 2)
    52  	expvar.Publish("map after delete", dump.Snapshot(m))
    53  	//hm := (*hmap)(reflect2.PtrOf(m))
    54  	//for i := 1; i < 30; i++ {
    55  	//	m[i] = i * i
    56  	//	expvar.Publish(fmt.Sprintf("map%v", i), dump.Snapshot(m))
    57  	//	if hm.oldbuckets != nil {
    58  	//		fmt.Println("!!!!", i)
    59  	//		break
    60  	//	}
    61  	//}
    62  }
    63  
    64  func Test_witch(t *testing.T) {
    65  	fakeValues := []string{"tom", "jerry", "william", "lily"}
    66  	Start("192.168.3.33:8318")
    67  	go func() {
    68  		defer func() {
    69  			recovered := recover()
    70  			countlog.LogPanic(recovered)
    71  		}()
    72  		for {
    73  			response := []byte{}
    74  			for i := int32(0); i < rand.Int31n(1024*256); i++ {
    75  				response = append(response, fakeValues[rand.Int31n(4)]...)
    76  			}
    77  			//countlog.Debug("event!hello", "user", fakeValues[rand.Int31n(4)],
    78  			//	"response", string(response))
    79  			time.Sleep(time.Millisecond * 500)
    80  		}
    81  	}()
    82  	time.Sleep(time.Hour)
    83  }