Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More โ†’
Socket
Sign inDemoInstall
Socket

github.com/loov/hrtime

Package Overview
Dependencies
Alerts
File Explorer
Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/loov/hrtime

  • v1.0.3
  • Source
  • Go
  • Socket score

Version published
Created
Source

hrtime

GoDoc

Package hrtime implements high-resolution timing functions and benchmarking utilities.

hrtime relies on using the best timing mechanism on a particular system. At the moment, for Windows it is using Performance Counters and on other platforms standard time.Now (since it's good enough).

Package also supports using hardware time stamp counters (TSC). They offer better accuracy and on some platforms correspond to the processor cycles. However, they are not supported on all platforms.

For example measuring time.Sleep on Mac and Windows.

Example

package main

import (
    "fmt"
    "time"

    "github.com/loov/hrtime"
)

func main() {
    start := hrtime.Now()
    time.Sleep(1000 * time.Nanosecond)
    fmt.Println(hrtime.Since(start))

    const numberOfExperiments = 4096

    bench := hrtime.NewBenchmark(numberOfExperiments)
    for bench.Next() {
        time.Sleep(1000 * time.Nanosecond)
    }
    fmt.Println(bench.Histogram(10))
}

Output on Mac:

12ยตs
  avg 14.5ยตs;  min 2ยตs;  p50 12ยตs;  max 74ยตs;
  p90 22ยตs;  p99 44ยตs;  p999 69ยตs;  p9999 74ยตs;
        2ยตs [ 229] โ–ˆโ–ˆโ–Œ
       10ยตs [3239] โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
       20ยตs [ 483] โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
       30ยตs [  80] โ–ˆ
       40ยตs [  39] โ–Œ
       50ยตs [  17] โ–Œ
       60ยตs [   6]
       70ยตs [   3]
       80ยตs [   0]
       90ยตs [   0]

Output on Windows:

1.5155ms
  avg 1.49ms;  min 576ยตs;  p50 1.17ms;  max 2.47ms;
  p90 2.02ms;  p99 2.3ms;  p999 2.37ms;  p9999 2.47ms;
      577ยตs [   1]
      600ยตs [  57] โ–ˆโ–Œ
      800ยตs [ 599] โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
        1ms [1399] โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
      1.2ms [  35] โ–ˆ
      1.4ms [   7]
      1.6ms [  91] โ–ˆโ–ˆโ–Œ
      1.8ms [ 995] โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
        2ms [ 778] โ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆโ–ˆ
      2.2ms [ 134] โ–ˆโ–ˆโ–ˆโ–Œ

A full explanation why it outputs this is out of the scope of this document. However, all sleep instructions have a specified granularity and time.Sleep actual sleeping time is requested time ยฑ sleep granularity. There are also other explanations to that behavior.

Benchmarking

hrtime/hrtesting can be used to supplement existing benchmarks with more details:

package hrtesting_test

import (
	"fmt"
	"runtime"
	"testing"

	"github.com/loov/hrtime/hrtesting"
)

func BenchmarkReport(b *testing.B) {
	bench := hrtesting.NewBenchmark(b)
	defer bench.Report()

	for bench.Next() {
		r := fmt.Sprintf("hello, world %d", 123)
		runtime.KeepAlive(r)
	}
}

FAQs

Package last updated on 16 Oct 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with โšก๏ธ by Socket Inc