File: speed.go

package info (click to toggle)
golang-github-performancecopilot-speed 4.0.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 508 kB
  • sloc: makefile: 38
file content (57 lines) | stat: -rw-r--r-- 1,546 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
// Package speed implements a golang client for the Performance Co-Pilot
// instrumentation API.
//
// It is based on the C/Perl/Python API implemented in PCP core as well as the
// Java API implemented by `parfait`, a separate project.
//
// Some examples on using the API are implemented as executable go programs in the
// `examples` subdirectory.
package speed

import (
	"fmt"
	"hash/fnv"
	"os"

	"github.com/pkg/errors"
)

// Version is the last tagged version of the package
const Version = "4.0.0"

var histogramInstances = []string{"min", "max", "mean", "variance", "standard_deviation"}
var histogramIndom *PCPInstanceDomain

// init maintains a central location of all things that happen when the package is initialized
// instead of everything being scattered in multiple source files
func init() {
	if err := initConfig(); err != nil {
		fmt.Fprintln(os.Stderr, errors.Errorf("error initializing config. maybe PCP isn't installed properly"))
	}

	var err error
	histogramIndom, err = NewPCPInstanceDomain("histogram", histogramInstances)
	if err != nil {
		fmt.Fprintln(os.Stderr, errors.Errorf("could not initialize an instance domain for histograms"))
	}
}

// generate a unique hash for a string of the specified bit length
// NOTE: make sure this is as fast as possible
//
// see: http://programmers.stackexchange.com/a/145633
func hash(s string, b uint32) uint32 {
	h := fnv.New32a()

	_, err := h.Write([]byte(s))
	if err != nil {
		panic(err)
	}

	val := h.Sum32()
	if b == 0 {
		return val
	}

	return val & ((1 << b) - 1)
}