github.com/NebulousLabs/Sia@v1.3.7/profile/timing.go (about)

     1  package profile
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  )
     7  
     8  var (
     9  	activeTimers = make(map[string]int64)
    10  	times        = make(map[string]int64)
    11  	uptime       int64
    12  )
    13  
    14  // Uptime() returns the number of nanoseconds that have passed since the first
    15  // call to uptime.
    16  func Uptime() int64 {
    17  	if uptime == 0 {
    18  		uptime = time.Now().UnixNano()
    19  		return 0
    20  	}
    21  	return (time.Now().UnixNano() - uptime) / 1e6
    22  }
    23  
    24  // PrintTimes prints how much time has passed at each timer.
    25  func PrintTimes() string {
    26  	s := "Printing Timers:\n"
    27  	for name, time := range times {
    28  		s += fmt.Sprintf("\t%v: %v\n", name, time/1e6)
    29  	}
    30  	return s
    31  }
    32  
    33  // ToggleTimer actives a timer known by a given string. If the timer does not
    34  // yet exist, it is created.
    35  func ToggleTimer(s string) {
    36  	toggleTime, exists := activeTimers[s]
    37  	if exists {
    38  		times[s] = times[s] + (time.Now().UnixNano() - toggleTime)
    39  		delete(activeTimers, s)
    40  	} else {
    41  		activeTimers[s] = time.Now().UnixNano()
    42  	}
    43  }