github.com/maypok86/otter@v1.2.1/internal/unixtime/unixtime_bench_test.go (about)

     1  // Copyright (c) 2023 Alexey Mayshev. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package unixtime
    16  
    17  import (
    18  	"sync/atomic"
    19  	"testing"
    20  	"time"
    21  )
    22  
    23  func BenchmarkNow(b *testing.B) {
    24  	Start()
    25  
    26  	b.ReportAllocs()
    27  	b.RunParallel(func(pb *testing.PB) {
    28  		var ts uint32
    29  		for pb.Next() {
    30  			ts += Now()
    31  		}
    32  		atomic.StoreUint32(&sink, ts)
    33  	})
    34  
    35  	Stop()
    36  }
    37  
    38  func BenchmarkTimeNowUnix(b *testing.B) {
    39  	b.ReportAllocs()
    40  	b.RunParallel(func(pb *testing.PB) {
    41  		var ts uint32
    42  		for pb.Next() {
    43  			ts += uint32(time.Now().Unix())
    44  		}
    45  		atomic.StoreUint32(&sink, ts)
    46  	})
    47  }
    48  
    49  // sink should prevent from code elimination by optimizing compiler.
    50  var sink uint32