當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO Read用法及代碼示例

GO語言"runtime/metrics"包中"Read"函數的用法及代碼示例。

用法:

func Read(m []Sample)

讀取填充給定度量樣本切片中的每個值字段。

所需的指標應該以適當的名稱出現在切片中。鼓勵此 API 的用戶在調用之間重複使用相同的切片以提高效率,但不是必須這樣做。

請注意,重用有一些注意事項。值得注意的是,當具有該值的讀取未完成時,不應讀取或操作該值;這是一場數據競賽。此屬性包括指針類型的值(例如,Float64Histogram),其底層存儲將在可能的情況下由 Read 重用。要在並發設置中安全地使用這些值,所有數據都必須是deep-copied。

並發執行多個 Read 調用是安全的,但它們的參數不能共享底層內存。如有疑問,請從頭開始創建一個新的 []Sample,這始終是安全的,盡管可能效率低下。

名稱未出現在 All 中的示例值將其 Value 填充為 KindBad 以指示名稱未知。

示例(ReadingAllMetrics):

package main

import (
	"fmt"
	"runtime/metrics"
)

func main() {
	// Get descriptions for all supported metrics.
	descs := metrics.All()

	// Create a sample for each metric.
	samples := make([]metrics.Sample, len(descs))
	for i := range samples {
		samples[i].Name = descs[i].Name
	}

	// Sample the metrics. Re-use the samples slice if you can!
	metrics.Read(samples)

	// Iterate over all results.
	for _, sample := range samples {
		// Pull out the name and value.
		name, value := sample.Name, sample.Value

		// Handle each sample.
		switch value.Kind() {
		case metrics.KindUint64:
			fmt.Printf("%s: %d\n", name, value.Uint64())
		case metrics.KindFloat64:
			fmt.Printf("%s: %f\n", name, value.Float64())
		case metrics.KindFloat64Histogram:
			// The histogram may be quite large, so let's just pull out
			// a crude estimate for the median for the sake of this example.
			fmt.Printf("%s: %f\n", name, medianBucket(value.Float64Histogram()))
		case metrics.KindBad:
			// This should never happen because all metrics are supported
			// by construction.
			panic("bug in runtime/metrics package!")
		default:
			// This may happen as new metrics get added.
			//
			// The safest thing to do here is to simply log it somewhere
			// as something to look into, but ignore it for now.
			// In the worst case, you might temporarily miss out on a new metric.
			fmt.Printf("%s: unexpected metric Kind: %v\n", name, value.Kind())
		}
	}
}

func medianBucket(h *metrics.Float64Histogram) float64 {
	total := uint64(0)
	for _, count := range h.Counts {
		total += count
	}
	thresh := total / 2
	total = 0
	for i, count := range h.Counts {
		total += count
		if total >= thresh {
			return h.Buckets[i]
		}
	}
	panic("should not happen")
}

示例(ReadingOneMetric):

package main

import (
	"fmt"
	"runtime/metrics"
)

func main() {
	// Name of the metric we want to read.
	const myMetric = "/memory/classes/heap/free:bytes"

	// Create a sample for the metric.
	sample := make([]metrics.Sample, 1)
	sample[0].Name = myMetric

	// Sample the metric.
	metrics.Read(sample)

	// Check if the metric is actually supported.
	// If it's not, the resulting value will always have
	// kind KindBad.
	if sample[0].Value.Kind() == metrics.KindBad {
		panic(fmt.Sprintf("metric %q no longer supported", myMetric))
	}

	// Handle the result.
	//
	// It's OK to assume a particular Kind for a metric;
	// they're guaranteed not to change.
	freeBytes := sample[0].Value.Uint64()

	fmt.Printf("free but not released memory: %d\n", freeBytes)
}

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 Read。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。