Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change: 优化, 使用"+"代替Sprintf #287

Merged
merged 1 commit into from
Nov 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions modules/graph/g/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import (
// RRDTOOL UTILS
// 监控数据对应的rrd文件名称
func RrdFileName(baseDir string, md5 string, dsType string, step int) string {
return fmt.Sprintf("%s/%s/%s_%s_%d.rrd", baseDir, md5[0:2], md5, dsType, step)
return baseDir + "/" + md5[0:2] + "/" +
md5 + "_" + dsType + "_" + strconv.Itoa(step) + ".rrd"
}

// rrd文件是否存在
Expand All @@ -36,7 +37,7 @@ func IsRrdFileExist(filename string) bool {

// 生成rrd缓存数据的key
func FormRrdCacheKey(md5 string, dsType string, step int) string {
return fmt.Sprintf("%s_%s_%d", md5, dsType, step)
return md5 + "_" + dsType + "_" + strconv.Itoa(step)
}
func SplitRrdCacheKey(ckey string) (md5 string, dsType string, step int, err error) {
ckey_slice := strings.Split(ckey, "_")
Expand Down
62 changes: 62 additions & 0 deletions modules/graph/g/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package g

import (
"fmt"
"testing"
)

func Test_RrdFileName(t *testing.T) {
if RrdFileName("/basedir", "b026324c6904b2a9cb4b88d6d61c81d1", "GAUGE", 10) !=
RrdFileName_orig("/basedir", "b026324c6904b2a9cb4b88d6d61c81d1", "GAUGE", 10) {
t.Error("not match with orig func")
}

if RrdFileName("/basedir", "b026324c6904b2a9cb4b88d6d61c81d1", "GAUGE", 10) !=
"/basedir/b0/b026324c6904b2a9cb4b88d6d61c81d1_GAUGE_10.rrd" {
t.Error("not match")
}
}

func Test_FormRrdCacheKey(t *testing.T) {
if FormRrdCacheKey("b026324c6904b2a9cb4b88d6d61c81d1", "GAUGE", 10) !=
FormRrdCacheKey_orig("b026324c6904b2a9cb4b88d6d61c81d1", "GAUGE", 10) {
t.Error("not match")
}

if FormRrdCacheKey("b026324c6904b2a9cb4b88d6d61c81d1", "GAUGE", 10) !=
"b026324c6904b2a9cb4b88d6d61c81d1_GAUGE_10" {
t.Error("not match")
}
}

func Benchmark_RrdFileName(b *testing.B) {
for i := 0; i < b.N; i++ {
RrdFileName("/basedir", "b026324c6904b2a9cb4b88d6d61c81d1", "GAUGE", 10)
}
}

func Benchmark_RrdFileName_orig(b *testing.B) {
for i := 0; i < b.N; i++ {
RrdFileName_orig("/basedir", "b026324c6904b2a9cb4b88d6d61c81d1", "GAUGE", 10)
}
}

func Benchmark_FormRrdCacheKey(b *testing.B) {
for i := 0; i < b.N; i++ {
FormRrdCacheKey("b026324c6904b2a9cb4b88d6d61c81d1", "GAUGE", 10)
}
}

func Benchmark_FormRrdCacheKey_orig(b *testing.B) {
for i := 0; i < b.N; i++ {
FormRrdCacheKey_orig("b026324c6904b2a9cb4b88d6d61c81d1", "GAUGE", 10)
}
}

func RrdFileName_orig(baseDir string, md5 string, dsType string, step int) string {
return fmt.Sprintf("%s/%s/%s_%s_%d.rrd", baseDir, md5[0:2], md5, dsType, step)
}

func FormRrdCacheKey_orig(md5 string, dsType string, step int) string {
return fmt.Sprintf("%s_%s_%d", md5, dsType, step)
}