github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/issue_test/issue123_test.go (about)

     1  /*
     2   * Copyright 2021 ByteDance Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package issue_test
    18  
    19  import (
    20  	`encoding/json`
    21  	`fmt`
    22  	`sync`
    23  	`testing`
    24  
    25  	`github.com/bytedance/sonic`
    26  )
    27  
    28  func TestGcWriteBarrier(t *testing.T) {
    29  	// debug.SetGCPercent(-1)
    30  	size := 10_0000
    31  	data := make([]int, size)
    32  	date, _ := json.Marshal(data)	
    33  
    34  	// debug.SetGCPercent(-1)
    35  	wg := sync.WaitGroup{}
    36  
    37  	for i := 0; i < 1000; i++ {
    38  		wg.Add(1)
    39  		cd := make([]byte, len(date))
    40  		copy(cd, date)
    41  		go func() {
    42  			var w []int
    43  			defer wg.Done()
    44  			// Decode
    45  			if err := sonic.Unmarshal(cd, &w); err != nil {
    46  				fmt.Println(err)
    47  			}
    48  		}()
    49  	}
    50  	wg.Wait()
    51  }
    52  
    53  func BenchmarkGcGuard(b *testing.B) {
    54  	size := 10_0000
    55  	data := make([]int, size)
    56  	date, _ := json.Marshal(data)
    57  	b.RunParallel(func(p *testing.PB) {
    58  		for p.Next() {
    59  			var w []int
    60  			_ = sonic.Unmarshal(date, &w)
    61  		}
    62  	})
    63  }