Example #1
0
func formatMemory(m report.Metric) (report.Metric, string) {
	s := m.LastSample()
	if s == nil {
		return m, ""
	}
	human, divisor := memoryScale(s.Value)
	return m.Div(divisor), fmt.Sprintf("%0.2f %s", s.Value/divisor, human)
}
Example #2
0
func TestMetricMarshalling(t *testing.T) {
	t1 := time.Now().UTC()
	t2 := time.Now().UTC().Add(1 * time.Minute)
	t3 := time.Now().UTC().Add(2 * time.Minute)
	t4 := time.Now().UTC().Add(3 * time.Minute)

	wantSamples := []report.Sample{
		{Timestamp: t1, Value: 0.1},
		{Timestamp: t2, Value: 0.2},
		{Timestamp: t3, Value: 0.3},
		{Timestamp: t4, Value: 0.4},
	}

	want := report.MakeMetric()
	for _, sample := range wantSamples {
		want = want.Add(sample.Timestamp, sample.Value)
	}

	// gob
	{
		gobs, err := want.GobEncode()
		if err != nil {
			t.Fatal(err)
		}
		var have report.Metric
		have.GobDecode(gobs)
		if !reflect.DeepEqual(want, have) {
			t.Error(test.Diff(want, have))
		}
	}

	// others
	{

		for _, h := range []codec.Handle{
			codec.Handle(&codec.MsgpackHandle{}),
			codec.Handle(&codec.JsonHandle{}),
		} {
			buf := &bytes.Buffer{}
			encoder := codec.NewEncoder(buf, h)
			want.CodecEncodeSelf(encoder)
			bufCopy := bytes.NewBuffer(buf.Bytes())

			decoder := codec.NewDecoder(buf, h)
			var have report.Metric
			have.CodecDecodeSelf(decoder)

			if !reflect.DeepEqual(want, have) {
				t.Error(test.Diff(want, have))
			}

			// extra check for samples
			decoder = codec.NewDecoder(bufCopy, h)
			var wire struct {
				Samples []report.Sample `json:"samples"`
			}
			if err := decoder.Decode(&wire); err != nil {
				t.Error(err)
			}
			if !reflect.DeepEqual(wantSamples, wire.Samples) {
				t.Error(test.Diff(wantSamples, wire.Samples))
			}
		}
	}

}
Example #3
0
func formatPercent(m report.Metric) (report.Metric, string) {
	if s := m.LastSample(); s != nil {
		return m, fmt.Sprintf("%0.2f%%", s.Value)
	}
	return m, ""
}