Beispiel #1
0
func TestEchoStruct(t *testing.T) {

	var results = struct {
		content map[string]int
		sync.Mutex
	}{content: make(map[string]int, 1000)}

	var counter = NewCallCalculator()
	var wgCreate sync.WaitGroup
	var wgFinish sync.WaitGroup
	var startRequestCh = make(chan struct{})
	for i := 0; i < ExecGoroutines; i++ {
		wgCreate.Add(1)
		go func() {
			wgCreate.Done()
			wgFinish.Add(1)
			defer wgFinish.Done()
			<-startRequestCh
			for i := 0; i < ExecPerGoroutines; i++ {
				var res string
				id := counter.Start()
				err := client.CallWithAddress("127.0.0.1:6668", "TestRpcInt", "EchoStruct", TestABC{"aaa", "bbb", "ccc"}, &res)
				counter.End(id)
				if err != nil {
					results.Lock()
					results.content[err.Error()] += 1
					results.Unlock()
					continue
				}
				results.Lock()
				results.content[res] += 1
				results.Unlock()
			}

		}()
	}
	wgCreate.Wait()
	// pprof result
	pprof.MemStats()
	// start to send request
	close(startRequestCh)
	wgFinish.Wait()
	close(StopClient2)
	pprof.MemStats()
	pprof.StatIncrement(pprof.HeapObjects, pprof.TotalAlloc, pprof.PauseTotalMs, pprof.NumGC)

	// output rpc result
	if len(results.content) > 1 {
		t.Error("have failed call")
	}
	for result, count := range results.content {
		t.Logf("TestEchoStruct result: %s ,count: %d \n", result, count)
	}
	// client request result
	counter.Summary()
	fmt.Printf("Max Client Qps: %d \n", MaxQps)
	time.Sleep(time.Microsecond)
}
Beispiel #2
0
// test malloc use object pool
func TestObjectPool(t *testing.T) {
	time.Sleep(time.Second)
	amount := 1000
	var wg sync.WaitGroup
	for i := 0; i < 5; i++ {
		fmt.Println("--------------malloc object without object pool-------------")
		pprof.MemStats()
		for i := 0; i < amount; i++ {
			wg.Add(1)
			go func() {
				defer wg.Done()
				for i := 0; i < 1000; i++ {
					ob := new(Object)
					if ob.a == "aaa" {
						ob.a = "bbb"
					}
				}

			}()

		}
		wg.Wait()
		pprof.MemStats()
		pprof.StatIncrement(pprof.HeapObjects, pprof.TotalAlloc)
	}
	for i := 0; i < 5; i++ {
		fmt.Println("----------------malloc object use object pool------------ ")
		pprof.MemStats()
		for i := 0; i < amount; i++ {
			wg.Add(1)
			go func() {
				defer wg.Done()
				for i := 0; i < 1000; i++ {
					ob := newObjectFromPool()
					if ob.a == "aaa" {
						ob.a = "bbb"
					}
					freeObject(ob)
				}

			}()
		}
		wg.Wait()
		pprof.MemStats()
		pprof.StatIncrement(pprof.HeapObjects, pprof.TotalAlloc)
	}
	fmt.Println("\n\n")
}
Beispiel #3
0
// list amount of goroutines and memory occupied
func TestGoroutineCost(t *testing.T) {
	amount := 100000
	pprof.MemStats()
	fmt.Println(pprof.Current())
	fmt.Printf("create goroutines amount: %d\n", amount)
	var wg sync.WaitGroup
	for i := 0; i < amount; i++ {
		wg.Add(1)
		go func() {
			defer wg.Done()
			time.Sleep(time.Second * 10)
		}()
	}
	wg.Wait()
	fmt.Println(pprof.Current())
	pprof.ProcessStats()

	pprof.MemStats()
	pprof.StatIncrement(pprof.TotalAlloc)
	fmt.Print("\n\n")
}
Beispiel #4
0
// test malloc in heap or stack function
func TestIfObjectInStack(t *testing.T) {
	amount := 10000
	for i := 0; i < amount; i++ {
		ob := Object{}
		if ob.a == "aaa" {
			ob.a = "bbb"
		}
		freeObject(&ob)
	}
	fmt.Println(lenPool())

	ob := &Object{}
	fmt.Println("----------------expect malloc in heap--------------")
	pprof.MemStats()

	for i := 0; i < amount; i++ {
		s := expectMallocInHeap(ob)
		// never hit this statment use s to prevent compile optimize code
		if s.a == "aaa" {
			fmt.Println("sssss")
		}
	}

	pprof.MemStats()
	pprof.StatIncrement(pprof.HeapObjects, pprof.TotalAlloc)
	fmt.Println("--------------  expect malloc in stack--------------")
	pprof.MemStats()

	for i := 0; i < amount; i++ {
		expectMallocInStack(ob)
	}
	pprof.MemStats()
	pprof.StatIncrement(pprof.HeapObjects, pprof.TotalAlloc)
	pprof.ProcessStats()

	fmt.Println("----------------expect malloc in mem pool--------------")
	pprof.MemStats()
	for i := 0; i < amount; i++ {
		s := newObjectFromPool()
		// never hit this statment use s to prevent compile optimize code
		if s.a == "aaa" {
			fmt.Println("sssss")
		}
		freeObject(s)
	}
	pprof.MemStats()
	pprof.StatIncrement(pprof.HeapObjects, pprof.TotalAlloc)
	fmt.Print("\n\n")
}