本文整理汇总了Golang中runtime.ReadMemStats函数的典型用法代码示例。如果您正苦于以下问题:Golang ReadMemStats函数的具体用法?Golang ReadMemStats怎么用?Golang ReadMemStats使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了ReadMemStats函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: main
func main() {
cpus := runtime.NumCPU()
runtime.GOMAXPROCS(cpus)
count := 100000
die := make(chan bool)
var startMemory runtime.MemStats
runtime.ReadMemStats(&startMemory)
start := time.Now()
for i := 0; i < count; i++ {
go waitAround(die)
}
elapsed := time.Since(start)
var endMemory runtime.MemStats
runtime.ReadMemStats(&endMemory)
fmt.Printf("Started %d goroutines on %d CPUs in %f seconds \n",
count, cpus, elapsed.Seconds())
fmt.Printf("Memory before %d, memory after %d \n",
startMemory.Alloc, endMemory.Alloc)
fmt.Printf("%d goroutines running\n", runtime.NumGoroutine())
close(die)
}
示例2: TestReadLargeMemCheck
// Check that reading large files doesn't lead to large allocations.
func TestReadLargeMemCheck(t *testing.T) {
ts := NewTestCase(t)
defer ts.Cleanup()
content := RandomData(385 * 1023)
err := ioutil.WriteFile(ts.origFile, []byte(content), 0644)
if err != nil {
t.Fatalf("WriteFile failed: %v", err)
}
f, err := os.Open(ts.mountFile)
if err != nil {
t.Fatalf("Open failed: %v", err)
}
buf := make([]byte, len(content)+1024)
f.Read(buf)
if err != nil {
t.Fatalf("Read failed: %v", err)
}
f.Close()
runtime.GC()
var before, after runtime.MemStats
N := 100
runtime.ReadMemStats(&before)
for i := 0; i < N; i++ {
f, _ := os.Open(ts.mountFile)
f.Read(buf)
f.Close()
}
runtime.ReadMemStats(&after)
delta := int((after.TotalAlloc - before.TotalAlloc))
delta = (delta - 40000) / N
t.Logf("bytes per read loop: %d", delta)
}
示例3: UpdateIndex
func (c *Corpus) UpdateIndex() {
if c.Verbose {
log.Printf("updating index...")
}
start := time.Now()
throttle := c.IndexThrottle
if throttle <= 0 {
throttle = 0.9
} else if throttle > 1.0 {
throttle = 1.0
}
index := c.NewIndex()
stop := time.Now()
c.searchIndex.Set(index)
if c.Verbose {
secs := stop.Sub(start).Seconds()
stats := index.Stats()
log.Printf("index updated (%gs, %d bytes of source, %d files, %d lines, %d unique words, %d spots)",
secs, stats.Bytes, stats.Files, stats.Lines, stats.Words, stats.Spots)
}
memstats := new(runtime.MemStats)
runtime.ReadMemStats(memstats)
if c.Verbose {
log.Printf("before GC: bytes = %d footprint = %d", memstats.HeapAlloc, memstats.Sys)
}
runtime.GC()
runtime.ReadMemStats(memstats)
if c.Verbose {
log.Printf("after GC: bytes = %d footprint = %d", memstats.HeapAlloc, memstats.Sys)
}
}
示例4: PopulateTrie
/*
Populates a trie structure with the contents of a text file that
has the provided name
*/
func PopulateTrie(file string) {
trie = wordTrie.NewTrie()
// Open the text file
f, err := os.Open(file)
//f, err := os.Open(file)
check(err)
// Begin scanning the text file line by line, populate the trie
scanner := bufio.NewScanner(f)
// Initial available memory read prior to entering the while loop
runtime.ReadMemStats(&mem)
currentMemory := mem.Alloc
// Add entries to the trie as long as there is enough memory
for scanner.Scan() && currentMemory < memoryLimit {
runtime.ReadMemStats(&mem)
currentMemory = mem.TotalAlloc
trie.AddEntry(scanner.Text())
}
if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}
f.Close()
fmt.Println(trie.GetNumberOfItems()) //TESTING
}
示例5: TestPeriodicGC
func TestPeriodicGC(t *testing.T) {
// Make sure we're not in the middle of a GC.
runtime.GC()
var ms1, ms2 runtime.MemStats
runtime.ReadMemStats(&ms1)
// Make periodic GC run continuously.
orig := *runtime.ForceGCPeriod
*runtime.ForceGCPeriod = 0
// Let some periodic GCs happen. In a heavily loaded system,
// it's possible these will be delayed, so this is designed to
// succeed quickly if things are working, but to give it some
// slack if things are slow.
var numGCs uint32
const want = 2
for i := 0; i < 20 && numGCs < want; i++ {
time.Sleep(5 * time.Millisecond)
// Test that periodic GC actually happened.
runtime.ReadMemStats(&ms2)
numGCs = ms2.NumGC - ms1.NumGC
}
*runtime.ForceGCPeriod = orig
if numGCs < want {
t.Fatalf("no periodic GC: got %v GCs, want >= 2", numGCs)
}
}
示例6: BenchmarkExtract
func BenchmarkExtract(b *testing.B) {
config, err := ioutil.ReadFile("test/haproxy.csv")
if err != nil {
b.Fatalf("could not read config file: %v", err.Error())
}
h := newHaproxy(config)
defer h.Close()
e := NewExporter(h.URL, "", 5*time.Second)
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ch := make(chan prometheus.Metric)
go func(ch chan prometheus.Metric) {
for _ = range ch {
}
}(ch)
e.Collect(ch)
close(ch)
}
runtime.GC()
runtime.ReadMemStats(&after)
b.Logf("%d bytes used after %d runs", after.Alloc-before.Alloc, b.N)
}
示例7: gcHandler
func (a *Adminz) gcHandler(w http.ResponseWriter, r *http.Request) {
var mem runtime.MemStats
mb := uint64(1024 * 1024)
runtime.ReadMemStats(&mem)
fmt.Fprintln(w, "Before")
fmt.Fprintln(w, "\tAlloc\t", mem.Alloc/mb)
fmt.Fprintln(w, "\tTotalAlloc:\t", mem.TotalAlloc/mb)
fmt.Fprintln(w, "\tHeapAlloc:\t", mem.HeapAlloc/mb)
fmt.Fprintln(w, "\tHeapSys:\t", mem.HeapSys/mb)
fmt.Fprintln(w, "\tSys:\t", mem.Sys/mb)
a.Lock()
was := a.doPause()
runtime.GC()
if was {
a.doResume()
}
a.Unlock()
runtime.ReadMemStats(&mem)
fmt.Fprintln(w, "After")
fmt.Fprintln(w, "\tAlloc\t", mem.Alloc/mb)
fmt.Fprintln(w, "\tTotalAlloc:\t", mem.TotalAlloc/mb)
fmt.Fprintln(w, "\tHeapAlloc:\t", mem.HeapAlloc/mb)
fmt.Fprintln(w, "\tHeapSys:\t", mem.HeapSys/mb)
fmt.Fprintln(w, "\tSys:\t", mem.Sys/mb)
w.Write([]byte("OK"))
}
示例8: TestClean
func TestClean(t *testing.T) {
tests := cleantests
if runtime.GOOS == "windows" {
for i := range tests {
tests[i].result = filepath.FromSlash(tests[i].result)
}
tests = append(tests, wincleantests...)
}
for _, test := range tests {
if s := filepath.Clean(test.path); s != test.result {
t.Errorf("Clean(%q) = %q, want %q", test.path, s, test.result)
}
if s := filepath.Clean(test.result); s != test.result {
t.Errorf("Clean(%q) = %q, want %q", test.result, s, test.result)
}
}
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
allocs := -ms.Mallocs
const rounds = 100
for i := 0; i < rounds; i++ {
for _, test := range tests {
filepath.Clean(test.result)
}
}
runtime.ReadMemStats(&ms)
allocs += ms.Mallocs
if allocs >= rounds {
t.Errorf("Clean cleaned paths: %d allocations per test round, want zero", allocs/rounds)
}
}
示例9: writeHeader
func writeHeader(writer io.Writer) {
fmt.Fprintf(writer, "Start time: %s<br>\n", startTime.Format(timeFormat))
uptime := time.Since(startTime) + time.Millisecond*50
uptime = (uptime / time.Millisecond / 100) * time.Millisecond * 100
fmt.Fprintf(writer, "Uptime: %s<br>\n", uptime)
var rusage syscall.Rusage
syscall.Getrusage(syscall.RUSAGE_SELF, &rusage)
userCpuTime := time.Duration(rusage.Utime.Sec)*time.Second +
time.Duration(rusage.Utime.Usec)*time.Microsecond
sysCpuTime := time.Duration(rusage.Stime.Sec)*time.Second +
time.Duration(rusage.Stime.Usec)*time.Microsecond
cpuTime := rusage.Utime.Sec + rusage.Stime.Sec
fmt.Fprintf(writer, "CPU Time: %.1f%% (User: %s Sys: %s)<br>\n",
float64(cpuTime*100)/float64(uptime.Seconds()), userCpuTime, sysCpuTime)
var memStatsBeforeGC, memStatsAfterGC runtime.MemStats
runtime.ReadMemStats(&memStatsBeforeGC)
runtime.GC()
runtime.ReadMemStats(&memStatsAfterGC)
fmt.Fprintf(writer, "Allocated memory: %s (%s after GC)<br>\n",
format.FormatBytes(memStatsBeforeGC.Alloc),
format.FormatBytes(memStatsAfterGC.Alloc))
fmt.Fprintf(writer, "System memory: %s (%s after GC)<br>\n",
format.FormatBytes(memStatsBeforeGC.Sys),
format.FormatBytes(memStatsAfterGC.Sys))
fmt.Fprintln(writer, "Raw <a href=\"metrics\">metrics</a>")
}
示例10: main
func main() {
cpus := runtime.NumCPU()
runtime.GOMAXPROCS(cpus)
count := 1000 * 100
var startMemory runtime.MemStats
runtime.ReadMemStats(&startMemory)
start := time.Now()
fin := make(chan bool)
for i := 0; i < count; i++ {
go func() {
<-fin
}()
}
elapsed := time.Since(start)
var endMemory runtime.MemStats
runtime.ReadMemStats(&endMemory)
close(fin)
fmt.Printf(`
goroutine: %d
cpu: %d
time: %f
memory all: %f MB
memory: %f byte/goroutine
`,
count, cpus, elapsed.Seconds(),
float64(endMemory.Alloc-startMemory.Alloc)/float64(1024*1024),
float64(endMemory.Alloc-startMemory.Alloc)/float64(count))
}
示例11: TestCountDecodeMallocs
func TestCountDecodeMallocs(t *testing.T) {
var buf bytes.Buffer
enc := NewEncoder(&buf)
bench := &Bench{7, 3.2, "now is the time", []byte("for all good men")}
const count = 1000
for i := 0; i < count; i++ {
err := enc.Encode(bench)
if err != nil {
t.Fatal("encode:", err)
}
}
dec := NewDecoder(&buf)
memstats := new(runtime.MemStats)
runtime.ReadMemStats(memstats)
mallocs := 0 - memstats.Mallocs
for i := 0; i < count; i++ {
*bench = Bench{}
err := dec.Decode(&bench)
if err != nil {
t.Fatal("decode:", err)
}
}
runtime.ReadMemStats(memstats)
mallocs += memstats.Mallocs
fmt.Printf("mallocs per decode of type Bench: %d\n", mallocs/count)
}
示例12: TestChunkReaderAllocs
func TestChunkReaderAllocs(t *testing.T) {
// temporarily set GOMAXPROCS to 1 as we are testing memory allocations
defer runtime.GOMAXPROCS(runtime.GOMAXPROCS(1))
var buf bytes.Buffer
w := newChunkedWriter(&buf)
a, b, c := []byte("aaaaaa"), []byte("bbbbbbbbbbbb"), []byte("cccccccccccccccccccccccc")
w.Write(a)
w.Write(b)
w.Write(c)
w.Close()
r := newChunkedReader(&buf)
readBuf := make([]byte, len(a)+len(b)+len(c)+1)
var ms runtime.MemStats
runtime.ReadMemStats(&ms)
m0 := ms.Mallocs
n, err := io.ReadFull(r, readBuf)
runtime.ReadMemStats(&ms)
mallocs := ms.Mallocs - m0
if mallocs > 1 {
t.Errorf("%d mallocs; want <= 1", mallocs)
}
if n != len(readBuf)-1 {
t.Errorf("read %d bytes; want %d", n, len(readBuf)-1)
}
if err != io.ErrUnexpectedEOF {
t.Errorf("read error = %v; want ErrUnexpectedEOF", err)
}
}
示例13: BenchmarkExtract
func BenchmarkExtract(b *testing.B) {
config, err := ioutil.ReadFile("test/recursor_stats.json")
if err != nil {
b.Fatalf("could not read config file: %v", err.Error())
}
h := newPowerDNS(config)
defer h.Close()
hostURL, _ := url.Parse(h.URL)
e := NewExporter("12345", "recursor", hostURL)
var before, after runtime.MemStats
runtime.GC()
runtime.ReadMemStats(&before)
b.ResetTimer()
for i := 0; i < b.N; i++ {
ch := make(chan prometheus.Metric)
go func(ch chan prometheus.Metric) {
for _ = range ch {
}
}(ch)
e.Collect(ch)
close(ch)
}
runtime.GC()
runtime.ReadMemStats(&after)
b.Logf("%d bytes used after %d runs", after.Alloc-before.Alloc, b.N)
}
示例14: testCacheGetMemoryLeak
// Cache memory leak for get
func testCacheGetMemoryLeak() {
pc.OverrideLeaseSeconds(1)
defer pc.OverrideLeaseSeconds(0)
var memstats runtime.MemStats
var initAlloc, midAlloc, finalAlloc uint64
longValue := strings.Repeat("this sentence is 30 char long\n", 30)
// Run garbage collection and get memory stats
runtime.GC()
runtime.ReadMemStats(&memstats)
initAlloc = memstats.Alloc
// Cache a lot of data
for i := 0; i < 10000; i++ {
key := fmt.Sprintf("keymemleakget:%d", i)
pc.Reset()
forceCacheGet(key, longValue)
if pc.GetLeaseRequestCount() == 0 {
LOGE.Println("FAIL: not requesting leases")
failCount++
return
}
pc.Reset()
v, err := ls.Get(key)
if checkError(err, false) {
return
}
if v != longValue {
LOGE.Println("FAIL: got wrong value")
failCount++
return
}
if pc.GetRpcCount() > 0 {
LOGE.Println("FAIL: not caching data")
failCount++
return
}
}
runtime.GC()
runtime.ReadMemStats(&memstats)
midAlloc = memstats.Alloc
// Wait for data to expire and someone to cleanup
time.Sleep(20 * time.Second)
// Run garbage collection and get memory stats
runtime.GC()
runtime.ReadMemStats(&memstats)
finalAlloc = memstats.Alloc
if finalAlloc < initAlloc || (finalAlloc-initAlloc) < 5000000 {
fmt.Fprintln(output, "PASS")
passCount++
} else {
LOGE.Printf("FAIL: not cleaning cache - memory leak - init %d mid %d final %d\n", initAlloc, midAlloc, finalAlloc)
failCount++
}
}
示例15: BenchmarkWatch
func BenchmarkWatch(b *testing.B) {
b.StopTimer()
s := newStore()
kvs, _ := generateNRandomKV(b.N, 128)
b.StartTimer()
memStats := new(runtime.MemStats)
runtime.GC()
runtime.ReadMemStats(memStats)
for i := 0; i < b.N; i++ {
w, _ := s.Watch(kvs[i][0], false, false, 0)
e := newEvent("set", kvs[i][0], uint64(i+1), uint64(i+1))
s.WatcherHub.notify(e)
<-w.EventChan
s.CurrentIndex++
}
s.WatcherHub.EventHistory = nil
afterMemStats := new(runtime.MemStats)
runtime.GC()
runtime.ReadMemStats(afterMemStats)
fmt.Printf("\nBefore Alloc: %v; After Alloc: %v\n",
memStats.Alloc/1000, afterMemStats.Alloc/1000)
}