封装的基础函数
package pchart
import (
"gonum.org/v1/plot"
"gonum.org/v1/plot/font"
"gonum.org/v1/plot/plotter"
"gonum.org/v1/plot/vg"
"io/ioutil"
"log"
"golang.org/x/image/font/opentype"
)
// 生成散列图
func BuildPlot(title, xlabel, ylabel, filename string, xy plotter.XYer) {
plotter.DefaultGlyphStyle.Radius = vg.Points(0.5)
font.DefaultCache.Add([]font.Face{
{
Font: font.Font{Typeface: "YaHei"},
Face: GetZWFont2(),
},
})
plotter.DefaultFont = font.Font{Typeface: "YaHei"}
plot.DefaultFont = font.Font{Typeface: "YaHei"}
p := plot.New()
p.Title.Text = title
p.X.Label.Text = xlabel
p.Y.Label.Text = ylabel
scatter2, _ := plotter.NewScatter(xy)
p.Add(scatter2)
// Save the plot to a PNG file.
if err := p.Save(8*vg.Inch, 8*vg.Inch, filename); err != nil {
panic(err)
}
}
const (
fontFile = "/Library/Fonts/Microsoft-YaHei.ttf" // 需要使用的字体文件
)
func GetZWFont2() *opentype.Font {
// 读字体数据
fontBytes, err := ioutil.ReadFile(fontFile)
if err != nil {
log.Println(err)
return nil
}
font, err := opentype.Parse(fontBytes)
if err != nil {
log.Println(err)
return nil
}
return font
}
绘图
package report
import (
"fmt"
"log"
"sankuai/analysis"
"sankuai/pchart"
"sankuai/tools"
"time"
"gonum.org/v1/plot/plotter"
)
// 生成 吞吐量和 cpubusy的散点图
func BuildCapacityScatterPlot1(cad *analysis.CacheAppkeyData) string {
centerTime, err := tools.GetCenterTime(cad.BeginTime, cad.EndTime)
if err != nil {
log.Println(err)
}
path := pathCheck(centerTime)
fileName := fmt.Sprintf("%s/csp-%s-%s.png", path, cad.Appkey, centerTime.Format("20060102"))
pts := plotter.XYs{}
for curr := cad.BeginTime; !curr.After(cad.EndTime); curr = curr.Add(time.Minute) {
// 计算吞吐量
throughput := float64(0.0)
avg1, ex11 := cad.TransactionAvgMap[curr]
qpm1, ex12 := cad.TransactionQPMMap[curr]
avg2, ex21 := cad.TransactionAvgpTestMap[curr]
qpm2, ex22 := cad.TransactionQPMpTestMap[curr]
if ex11 && ex12 {
throughput += avg1 * qpm1
}
if ex21 && ex22 {
throughput += avg2 * qpm2
}
// log.Println(throughput)
// 计算合计 cpubusy
cpubusy := float64(0.0)
for _, cmap := range cad.HostsCPUBusyMap {
cb, ex3 := cmap[curr]
if ex3 {
cpubusy += cb
}
}
pts = append(pts, plotter.XY{X: throughput, Y: cpubusy})
}
pchart.BuildPlot("容量散点图", "吞吐量", "cpu.busy", fileName, pts)
return fileName
}