github.com/amarpal/go-tools@v0.0.0-20240422043104-40142f59f616/lintcmd/runner/stats.go (about)

     1  package runner
     2  
     3  import (
     4  	"sync/atomic"
     5  	"time"
     6  
     7  	"github.com/amarpal/go-tools/go/loader"
     8  
     9  	"golang.org/x/tools/go/analysis"
    10  )
    11  
    12  const (
    13  	StateInitializing = iota
    14  	StateLoadPackageGraph
    15  	StateBuildActionGraph
    16  	StateProcessing
    17  	StateFinalizing
    18  )
    19  
    20  type Stats struct {
    21  	state                    uint32
    22  	initialPackages          uint32
    23  	totalPackages            uint32
    24  	processedPackages        uint32
    25  	processedInitialPackages uint32
    26  
    27  	// optional function to call every time an analyzer has finished analyzing a package.
    28  	PrintAnalyzerMeasurement func(*analysis.Analyzer, *loader.PackageSpec, time.Duration)
    29  }
    30  
    31  func (s *Stats) setState(state uint32)    { atomic.StoreUint32(&s.state, state) }
    32  func (s *Stats) State() int               { return int(atomic.LoadUint32(&s.state)) }
    33  func (s *Stats) setInitialPackages(n int) { atomic.StoreUint32(&s.initialPackages, uint32(n)) }
    34  func (s *Stats) InitialPackages() int     { return int(atomic.LoadUint32(&s.initialPackages)) }
    35  func (s *Stats) setTotalPackages(n int)   { atomic.StoreUint32(&s.totalPackages, uint32(n)) }
    36  func (s *Stats) TotalPackages() int       { return int(atomic.LoadUint32(&s.totalPackages)) }
    37  
    38  func (s *Stats) finishPackage()         { atomic.AddUint32(&s.processedPackages, 1) }
    39  func (s *Stats) finishInitialPackage()  { atomic.AddUint32(&s.processedInitialPackages, 1) }
    40  func (s *Stats) ProcessedPackages() int { return int(atomic.LoadUint32(&s.processedPackages)) }
    41  func (s *Stats) ProcessedInitialPackages() int {
    42  	return int(atomic.LoadUint32(&s.processedInitialPackages))
    43  }
    44  
    45  func (s *Stats) measureAnalyzer(analysis *analysis.Analyzer, pkg *loader.PackageSpec, d time.Duration) {
    46  	if s.PrintAnalyzerMeasurement != nil {
    47  		s.PrintAnalyzerMeasurement(analysis, pkg, d)
    48  	}
    49  }