File tree Expand file tree Collapse file tree 1 file changed +37
-8
lines changed Expand file tree Collapse file tree 1 file changed +37
-8
lines changed Original file line number Diff line number Diff line change @@ -3,22 +3,51 @@ package wsjson_test
33import  (
44	"encoding/json" 
55	"io" 
6- 	"strings " 
6+ 	"strconv " 
77	"testing" 
8+ 
9+ 	"nhooyr.io/websocket/internal/test/xrand" 
810)
911
1012func  BenchmarkJSON (b  * testing.B ) {
11- 	msg  :=  []byte (strings .Repeat ("1234" , 128 ))
12- 	b .SetBytes (int64 (len (msg )))
13- 	b .ReportAllocs ()
13+ 	sizes  :=  []int {
14+ 		8 ,
15+ 		16 ,
16+ 		32 ,
17+ 		128 ,
18+ 		256 ,
19+ 		512 ,
20+ 		1024 ,
21+ 		2048 ,
22+ 		4096 ,
23+ 		8192 ,
24+ 		16384 ,
25+ 	}
26+ 
1427	b .Run ("json.Encoder" , func (b  * testing.B ) {
15- 		for  i  :=  0 ; i  <  b .N ; i ++  {
16- 			json .NewEncoder (io .Discard ).Encode (msg )
28+ 		for  _ , size  :=  range  sizes  {
29+ 			b .Run (strconv .Itoa (size ), func (b  * testing.B ) {
30+ 				msg  :=  xrand .String (size )
31+ 				b .SetBytes (int64 (size ))
32+ 				b .ReportAllocs ()
33+ 				b .ResetTimer ()
34+ 				for  i  :=  0 ; i  <  b .N ; i ++  {
35+ 					json .NewEncoder (io .Discard ).Encode (msg )
36+ 				}
37+ 			})
1738		}
1839	})
1940	b .Run ("json.Marshal" , func (b  * testing.B ) {
20- 		for  i  :=  0 ; i  <  b .N ; i ++  {
21- 			json .Marshal (msg )
41+ 		for  _ , size  :=  range  sizes  {
42+ 			b .Run (strconv .Itoa (size ), func (b  * testing.B ) {
43+ 				msg  :=  xrand .String (size )
44+ 				b .SetBytes (int64 (size ))
45+ 				b .ReportAllocs ()
46+ 				b .ResetTimer ()
47+ 				for  i  :=  0 ; i  <  b .N ; i ++  {
48+ 					json .Marshal (msg )
49+ 				}
50+ 			})
2251		}
2352	})
2453}
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments