github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/pool/bufferpool/bufferpool_test.go (about)

     1  package bufferpool
     2  
     3  import (
     4  	"bytes"
     5  	"testing"
     6  )
     7  
     8  var count = 5
     9  var str = `{"errNo":0,"errStr":"fsd","data":{"105229_0_10":"{\"examInfo\":{\"bindInfo\":{\"bindId\":105229,\"bindType\":0,\"createTime\":1535797800,\"examId\":30188,\"examType\":10,\"operatorName\":\"鲍伟\",\"operatorUid\":18,\"props\":{\"duration\":0,\"maxTryNum\":0,\"passScore\":0},\"relationId\":28418,\"updateTime\":1537023831,\"userKv\":{\"examStatus\":2,\"startTime\":1537023586}},\"examInfo\":\"{\\\"examId\\\":30188,\\\"examType\\\":10,\\\"title\\\":\\\"\\\\u79cb1\\\\u9ad8\\\\u4e09\\\\u7269\\\\u7406\\\\u540c\\\\u6b65(\\\\u5c16\\\\u7aef\\\\u57f9\\\\u4f18\\\\u3001\\\\u5f3a\\\\u5316\\\\u63d0\\\\u5347)\\\\u7b2c2\\\\u8bb2\\\\u5802\\\\u5802\\\\u6d4b\\\",\\\"tidList\\\":{\\\"376085497\\\":{\\\"score\\\":50,\\\"type\\\":2},\\\"375574847\\\":{\\\"score\\\":50,\\\"type\\\":1}},\\\"totalScore\\\":100,\\\"props\\\":[],\\\"userKv\\\":[],\\\"grade\\\":7,\\\"subject\\\":4,\\\"ruleInfo\\\":{\\\"duration\\\":0,\\\"passScore\\\":0,\\\"maxTryNum\\\":0},\\\"extData\\\":[]}\",\"path\":\"c:0-l:0-cpu:0\"},\"questionList\":{}}","105229_0_13":"{\"examInfo\":{},\"questionList\":{}}","105229_0_7":"{\"examInfo\":{\"bindInfo\":{\"bindId\":105229,\"bindType\":0,\"createTime\":1535797998,\"examId\":100724,\"examType\":7,\"operatorName\":\"鲍伟\",\"operatorUid\":18,\"props\":{\"duration\":0,\"maxTryNum\":0,\"passScore\":0},\"relationId\":38712,\"updateTime\":1535797998,\"userKv\":[]},\"examInfo\":\"{\\\"examId\\\":100724,\\\"examType\\\":7,\\\"title\\\":\\\"\\\\u79cb1\\\\u9ad8\\\\u4e09\\\\u7269\\\\u7406\\\\u540c\\\\u6b65(\\\\u5c16\\\\u7aef\\\\u57f9\\\\u4f18\\\\u3001\\\\u5f3a\\\\u5316\\\\u63d0\\\\u5347)\\\\u7b2c2\\\\u8bb2\\\\u8bfe\\\\u540e\\\\u4f5c\\\\u4e1a\\\",\\\"tidList\\\":{\\\"375479289\\\":{\\\"score\\\":0,\\\"type\\\":2},\\\"375574847\\\":{\\\"score\\\":0,\\\"type\\\":1}},\\\"totalScore\\\":0,\\\"props\\\":[],\\\"userKv\\\":[],\\\"grade\\\":7,\\\"subject\\\":4,\\\"ruleInfo\\\":{\\\"duration\\\":0,\\\"passScore\\\":0,\\\"maxTryNum\\\":0},\\\"extData\\\":[]}\",\"path\":\"c:0-l:0-cpu:0\"},\"questionList\":{}}"}}`
    10  
    11  func Benchmark_TestStringAppend(b *testing.B) {
    12  	for i := 0; i < b.N; i++ {
    13  		s := ""
    14  		for j := 0; j < count; j++ {
    15  			s = s + str
    16  		}
    17  	}
    18  }
    19  
    20  func Benchmark_TestBufferPool(b *testing.B) {
    21  	b.StopTimer() //调用该函数停止压力测试的时间计数
    22  	var buffers = NewBufferPool(516)
    23  	b.StartTimer() //重新开始时间
    24  	for i := 0; i < b.N; i++ {
    25  		buf := buffers.Get()
    26  		for j := 0; j < count; j++ {
    27  			buf.WriteString(str)
    28  		}
    29  		buffers.Put(buf)
    30  	}
    31  }
    32  
    33  func Benchmark_TestBuffer(b *testing.B) {
    34  	for i := 0; i < b.N; i++ {
    35  		buf := bytes.NewBufferString("")
    36  		for j := 0; j < count; j++ {
    37  			buf.WriteString(str)
    38  		}
    39  	}
    40  }
    41  
    42  func BenchmarkJI_TestBufferPool_Parallel(b *testing.B) {
    43  	b.StopTimer() //调用该函数停止压力测试的时间计数
    44  	var buffers = NewBufferPool(516)
    45  	b.StartTimer() //重新开始时间
    46  	b.RunParallel(func(pb *testing.PB) {
    47  		for pb.Next() {
    48  			buf := buffers.Get()
    49  			for j := 0; j < count; j++ {
    50  				buf.WriteString(str)
    51  			}
    52  			buffers.Put(buf)
    53  		}
    54  	})
    55  }
    56  
    57  func BenchmarkJI_TestBuffer_Parallel(b *testing.B) {
    58  	b.RunParallel(func(pb *testing.PB) {
    59  		for pb.Next() {
    60  			buf := bytes.NewBufferString("")
    61  			for j := 0; j < count; j++ {
    62  				buf.WriteString(str)
    63  			}
    64  		}
    65  	})
    66  }
    67  
    68  func BenchmarkJI_TestStringAppend_Parallel(b *testing.B) {
    69  	b.RunParallel(func(pb *testing.PB) {
    70  		for pb.Next() {
    71  			s := ""
    72  			for j := 0; j < count; j++ {
    73  				s = s + str
    74  			}
    75  		}
    76  	})
    77  }