github.com/blend/go-sdk@v1.20220411.3/assert/stats.go (about)

     1  /*
     2  
     3  Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved
     4  Use of this source code is governed by a MIT license that can be found in the LICENSE file.
     5  
     6  */
     7  
     8  package assert
     9  
    10  import (
    11  	"fmt"
    12  	"os"
    13  	"sync/atomic"
    14  	"time"
    15  )
    16  
    17  // assertcount is the total number of assetions run during the package lifetime.
    18  var assertCount int32
    19  
    20  // Increment increments the global assertion count.
    21  func Increment() {
    22  	atomic.AddInt32(&assertCount, int32(1))
    23  }
    24  
    25  // Count returns the total number of assertions.
    26  func Count() int {
    27  	return int(atomic.LoadInt32(&assertCount))
    28  }
    29  
    30  // started is when the package started.
    31  var started time.Time
    32  
    33  // Started marks a started time.
    34  func Started() {
    35  	started = time.Now()
    36  }
    37  
    38  // Elapsed returns the time since `Started()`
    39  func Elapsed() time.Duration {
    40  	return time.Since(started)
    41  }
    42  
    43  // Rate returns the assertions per second.
    44  func Rate() float64 {
    45  	elapsedSeconds := (float64(Elapsed()) / float64(time.Second))
    46  	return float64(atomic.LoadInt32(&assertCount)) / elapsedSeconds
    47  }
    48  
    49  // ReportRate writes the rate summary to stdout.
    50  func ReportRate() {
    51  	fmt.Fprintf(os.Stdout, "asserts: %d Δt: %v λ: %0.2f assert/sec\n", Count(), Elapsed(), Rate())
    52  }