新增系统概况

This commit is contained in:
george
2018-08-13 17:15:06 +08:00
parent e16067c2dc
commit 2728edf7b0
6 changed files with 307 additions and 110 deletions

88
libs/file.go Normal file
View File

@@ -0,0 +1,88 @@
/************************************************************
** @Description: libs
** @Author: george hao
** @Date: 2018-08-13 11:17
** @Last Modified by: george hao
** @Last Modified time: 2018-08-13 11:17
*************************************************************/
package libs
import (
"fmt"
"math"
"net/http"
"os"
"path/filepath"
"strings"
)
func Exist(path string) bool {
_, err := os.Stat(path)
return err == nil || os.IsExist(err)
}
func GetCurrentDirectory() (string, error) {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
return "", err
}
return filepath.Clean(strings.Replace(dir, "\\", "/", -1)), nil
}
func IsTextFile(data []byte) bool {
if len(data) == 0 {
return true
}
return strings.Contains(http.DetectContentType(data), "text/")
}
func IsImageFile(data []byte) bool {
return strings.Contains(http.DetectContentType(data), "image/")
}
func IsPDFFile(data []byte) bool {
return strings.Contains(http.DetectContentType(data), "application/pdf")
}
func IsVideoFile(data []byte) bool {
return strings.Contains(http.DetectContentType(data), "video/")
}
const (
Byte = 1
KByte = Byte * 1024
MByte = KByte * 1024
GByte = MByte * 1024
TByte = GByte * 1024
PByte = TByte * 1024
EByte = PByte * 1024
)
var bytesSizeTable = map[string]uint64{
"b": Byte,
"kb": KByte,
"mb": MByte,
"gb": GByte,
"tb": TByte,
"pb": PByte,
"eb": EByte,
}
func logn(n, b float64) float64 {
return math.Log(n) / math.Log(b)
}
func humanateBytes(s uint64, base float64, sizes []string) string {
if s < 10 {
return fmt.Sprintf("%d B", s)
}
e := math.Floor(logn(float64(s), base))
suffix := sizes[int(e)]
val := float64(s) / math.Pow(base, math.Floor(e))
f := "%.0f"
if val < 10 {
f = "%.1f"
}
return fmt.Sprintf(f+" %s", val, suffix)
}
func FileSize(s int64) string {
sizes := []string{"B", "KB", "MB", "GB", "TB", "PB", "EB"}
return humanateBytes(uint64(s), 1024, sizes)
}

65
libs/monitor.go Normal file
View File

@@ -0,0 +1,65 @@
/************************************************************
** @Description: libs
** @Author: george hao
** @Date: 2018-08-13 11:16
** @Last Modified by: george hao
** @Last Modified time: 2018-08-13 11:16
*************************************************************/
package libs
import (
"fmt"
"runtime"
"time"
)
func SystemInfo(startTime int64) map[string]interface{} {
var afterLastGC string
goNum := runtime.NumGoroutine()
cpuNum := runtime.NumCPU()
mstat := &runtime.MemStats{}
runtime.ReadMemStats(mstat)
now := time.Now().Unix()
//costTime := int(time.Now().Sub(startTime).Seconds())
costTime := int(now - startTime)
mb := 1024 * 1024
if mstat.LastGC != 0 {
afterLastGC = fmt.Sprintf("%.1fs", float64(time.Now().UnixNano()-int64(mstat.LastGC))/1000/1000/1000)
} else {
afterLastGC = "0"
}
fmt.Println()
fmt.Println()
fmt.Println()
fmt.Println()
//fmt.Println(startTime)
//fmt.Println(now)
//fmt.Println(costTime)
return map[string]interface{}{
"服务运行时间": fmt.Sprintf("%d天%d小时%d分%d秒", costTime/(3600*24), costTime%(3600*24)/3600, costTime%3600/60, costTime%(60)),
"goroute数量": goNum,
"cpu核心数": cpuNum,
"当前内存使用量": FileSize(int64(mstat.Alloc)),
"所有被分配的内存": FileSize(int64(mstat.TotalAlloc)),
"内存占用量": FileSize(int64(mstat.Sys)),
"指针查找次数": mstat.Lookups,
"内存分配次数": mstat.Mallocs,
"内存释放次数": mstat.Frees,
"距离上次GC时间": afterLastGC,
// "当前 Heap 内存使用量": file.FileSize(int64(mstat.HeapAlloc)),
// "Heap 内存占用量": file.FileSize(int64(mstat.HeapSys)),
// "Heap 内存空闲量": file.FileSize(int64(mstat.HeapIdle)),
// "正在使用的 Heap 内存": file.FileSize(int64(mstat.HeapInuse)),
// "被释放的 Heap 内存": file.FileSize(int64(mstat.HeapReleased)),
// "Heap 对象数量": mstat.HeapObjects,
"下次GC内存回收量": fmt.Sprintf("%.3fMB", float64(mstat.NextGC)/float64(mb)),
"GC暂停时间总量": fmt.Sprintf("%.3fs", float64(mstat.PauseTotalNs)/1000/1000/1000),
"上次GC暂停时间": fmt.Sprintf("%.3fs", float64(mstat.PauseNs[(mstat.NumGC+255)%256])/1000/1000/1000),
}
}