github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/metrics/memory.go (about)

     1  package metrics
     2  
     3  import (
     4  	"runtime"
     5  	"strings"
     6  )
     7  
     8  var sizeKB float64 = 1 << (10 * 1)
     9  var sizeMB float64 = 1 << (10 * 2)
    10  var sizeGB float64 = 1 << (10 * 3)
    11  
    12  //MemoryReport Contrato de Memory Check
    13  type MemoryReport struct {
    14  	Goroutines     int     `json:"goroutines"`
    15  	HeapAllocated  float64 `json:"heapAllocated"`
    16  	HeapInUse      float64 `json:"heapInUse"`
    17  	StackAllocated float64 `json:"stackAllocated"`
    18  	StackInUse     float64 `json:"stackInUse"`
    19  	TotalAllocated float64 `json:"totalAllocated"`
    20  	TotalInUse     float64 `json:"totalInUse"`
    21  	MemoryUnit     string  `json:"memoryUnit"`
    22  }
    23  
    24  //GetMemoryReport Obtém dados de Memory Check
    25  func GetMemoryReport(u string) MemoryReport {
    26  	var m runtime.MemStats
    27  	runtime.ReadMemStats(&m)
    28  
    29  	var unit float64
    30  
    31  	switch strings.ToUpper(u) {
    32  	case "GB":
    33  		unit = sizeGB
    34  		break
    35  	case "KB":
    36  		unit = sizeKB
    37  	default:
    38  		u = "MB"
    39  		unit = sizeMB
    40  	}
    41  
    42  	return MemoryReport{
    43  		Goroutines:     runtime.NumGoroutine(),
    44  		HeapAllocated:  float64(m.HeapSys-m.HeapReleased) / unit,
    45  		HeapInUse:      float64(m.HeapInuse) / unit,
    46  		StackAllocated: float64(m.StackSys) / unit,
    47  		StackInUse:     float64(m.StackInuse) / unit,
    48  		TotalAllocated: float64(m.HeapSys+m.StackSys-m.HeapReleased) / unit,
    49  		TotalInUse:     float64(m.HeapInuse+m.StackInuse) / unit,
    50  		MemoryUnit:     strings.ToUpper(u),
    51  	}
    52  }