Test Memory on go

前端之家收集整理的这篇文章主要介绍了Test Memory on go前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 
 
package main
 
import (
"container/list"
"fmt"
"net/http"
"os"
"runtime"
"strconv"
// "strings"
"io"
"log"
"sync"
"syscall"
"time"
)
 
var lockger sync.Mutex
var fansMap map[int]*list.List
 
func root(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Hello %s!</h1>", r.URL.Path[1:])
url := "get?key=123"
fmt.Fprintf(w, "<a href=%s>%s</a><br />", url, url)
url = "set?key=123&v=100"
fmt.Fprintf(w, url)
url = "stop"
fmt.Fprintf(w, url)
}
func setData(w http.ResponseWriter, "...")
runtime.GC()
}
func getData(w http.ResponseWriter, r *http.Request) {
key := r.FormValue("key")
i, _ := strconv.Atoi(key)
fmt.Fprintf(w, "val:%d", i)
lockger.Lock()
l, ok := fansMap[i]
lockger.Unlock()
if ok {
fmt.Fprintf(w, "mapok\n")
for e := l.Front(); e != nil; e = e.Next() {
// do something with l.Value
v := e.Value.(int)
fmt.Fprintf(w, "%d,", v)
}
}
//r.ParseForm()
//for k, v := range r.Form {
//    fmt.Fprintf(w, "key:%s", k)
//    fmt.Fprintf(w, "val:%d", v)
//    //i, _ := strconv.Atoi(v)
//}
}
 
// getEnv Request Handler
func getEnv(writer http.ResponseWriter, req *http.Request) {
env := os.Environ()
writer.Write([]byte("<h1>??????</h1><br>"))
for _, v := range env {
writer.Write([]byte(v + "<br>"))
}
writer.Write([]byte("<br>"))
}
func StopServer(w http.ResponseWriter, req *http.Request) {
responseString := "STOP"
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.Header().Set("Content-Length", strconv.Itoa(len(responseString)))
io.WriteString(w, responseString)
f, canFlush := w.(http.Flusher)
if canFlush {
f.Flush()
}
conn, _, err := w.(http.Hijacker).Hijack()
if err != nil {
log.Fatalf("error while shutting down: %v", err)
}
conn.Close()
log.Println("Shutting down")
os.Exit(0)
}
func main() {
np := runtime.Numcpu()
runtime.GOMAXPROCS(np - 1)
println(np)
 
go ReportStatus()
fansMap = make(map[int]*list.List)
t := time.Now()
var i int
var fans *list.List
max := 1000000
for i = 0; i < max; i++ {
fans = list.New()
fansMap[i] = fans
for j := 1; j <= 200; j++ {
_ = fans.PushFront(j)
}
fans.PushFront(i)
 
if i%1000 == 0 {
fmt.Println("i=", i)
}
 
memstatus := MemStat()
if memstatus.Free < 512000000 {
fmt.Println("OUT OF MEMORY")
break
}
}
 
fmt.Println("list: " + time.Now().Sub(t).String())
http.HandleFunc("/", root)
http.HandleFunc("/get", getData)
http.HandleFunc("/set", setData)
http.HandleFunc("/stop", StopServer)
http.HandleFunc("/env", getEnv)
http.ListenAndServe(":8080", nil)
}
 
type MemStatus struct {
All  uint32 `json:"all"`
Used uint32 `json:"used"`
Free uint32 `json:"free"`
Self uint64 `json:"self"`
}
 
func MemStat() MemStatus {
//自身占用
memStat := new(runtime.MemStats)
runtime.ReadMemStats(memStat)
mem := MemStatus{}
mem.Self = memStat.Alloc
 
//系统占用,仅linux/mac下有效
//system memory usage
sysInfo := new(syscall.Sysinfo_t)
err := syscall.Sysinfo(sysInfo)
if err == nil {
mem.All = sysInfo.Totalram * uint32(syscall.Getpagesize())
mem.Free = sysInfo.Freeram * uint32(syscall.Getpagesize())
mem.Used = mem.All - mem.Free
}
return mem
}
func ReportStatus() {
memStats := &runtime.MemStats{}
runtime.ReadMemStats(memStats)
nsInMs := float64(time.Millisecond)
prefix := "MT"
for {
//now := time.Now()
fmt.Println("=========")
 
fmt.Println(fmt.Sprintf("%s.goroutines", prefix),
float64(runtime.NumGoroutine()))
fmt.Println(fmt.Sprintf("%s.memory.allocated",
float64(memStats.Alloc))
fmt.Println(fmt.Sprintf("%s.memory.mallocs",
float64(memStats.Mallocs))
fmt.Println(fmt.Sprintf("%s.memory.frees",
float64(memStats.Frees))
fmt.Println(fmt.Sprintf("%s.memory.gc.total_pause",
float64(memStats.PauseTotalNs)/nsInMs)
fmt.Println(fmt.Sprintf("%s.memory.heap",
float64(memStats.HeapAlloc))
fmt.Println(fmt.Sprintf("%s.memory.stack",
float64(memStats.StackInuse))
time.Sleep(10e8)
}
 
}
 
//func   reportRuntimeStats(prefix, context string,
//  dimensions Dimensions, sleep time.Duration) {
 
//  memStats := &runtime.MemStats{}
//  lastSampleTime := time.Now()
//  var lastPauseNs uint64 = 0
//  var lastNumGc uint32 = 0
 
//  nsInMs := float64(time.Millisecond)
 
//  for self.runtimeStatsRunning {
//    runtime.ReadMemStats(memStats)
 
//    now := time.Now()
 
//    Println(fmt.Sprintf("%s.goroutines", prefix),
//      float64(runtime.NumGoroutine()), now, context, dimensions)
//    Println(fmt.Sprintf("%s.memory.allocated",
//      float64(memStats.Alloc), dimensions)
//    Println(fmt.Sprintf("%s.memory.mallocs",
//      float64(memStats.Mallocs), dimensions)
//    Println(fmt.Sprintf("%s.memory.frees",
//      float64(memStats.Frees), dimensions)
//    Println(fmt.Sprintf("%s.memory.gc.total_pause",
//      float64(memStats.PauseTotalNs)/nsInMs, dimensions)
//    Println(fmt.Sprintf("%s.memory.heap",
//      float64(memStats.HeapAlloc), dimensions)
//    Println(fmt.Sprintf("%s.memory.stack",
//      float64(memStats.StackInuse), dimensions)
 
//    if lastPauseNs > 0 {
//      pauseSinceLastSample := memStats.PauseTotalNs - lastPauseNs
//      Println(fmt.Sprintf("%s.memory.gc.pause_per_second",
//        float64(pauseSinceLastSample)/nsInMs/sleep.Seconds(), dimensions)
//    }
//    lastPauseNs = memStats.PauseTotalNs
 
//    countGc := int(memStats.NumGC - lastNumGc)
//    if lastNumGc > 0 {
//      diff := float64(countGc)
//      diffTime := now.Sub(lastSampleTime).Seconds()
//      Println(fmt.Sprintf("%s.memory.gc.gc_per_second",
//        diff/diffTime, dimensions)
//    }
 
//    // get the individual pause times
//    if countGc > 0 {
//      if countGc > 256 {
//        fmt.Fprintf(os.Stderr, "We're missing some gc pause times")
//        countGc = 256
//      }
 
//      for i := 0; i < countGc; i++ {
//        idx := int((memStats.NumGC-uint32(i))+255) % 256
//        pause := float64(memStats.PauseNs[idx])
//        self.Aggregate(fmt.Sprintf("%s.memory.gc.pause",
//          pause/nsInMs, dimensions)
//      }
//    }
 
//    // keep track of the prevIoUs state
//    lastNumGc = memStats.NumGC
//    lastSampleTime = now
 
//    time.Sleep(sleep)
//  }
//}
原文链接:https://www.f2er.com/go/191236.html

猜你在找的Go相关文章