Dong.Ge

左手代码 右手江湖

新技能-func当做变量

东哥's Avatar 2020-08-17

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package main

import (
"flag"
"fmt"
"io/ioutil"
"runtime"
"strings"
"time"

"github.com/astaxie/beego/orm"
"github.com/garyburd/redigo/redis"

_ "github.com/go-sql-driver/mysql"
)

var vsMode string

var (
// 定义常量
RedisClient *redis.Pool
REDIS_HOST string = "127.0.0.1:6379"
REDIS_DB int = 0
)

const ExecTimes = 10000 * 100

type Ids struct {
Id int
Name string
}

func (m *Ids) TableName() string {
return "ids"
}

func init() {
flag.StringVar(&vsMode, "mode", "", " file db redis")
flag.Parse()

dbhost := "127.0.0.1"
dbport := "3306"
dbuser := "root"
dbpassword := "root"
dbname := "wcd"
if dbport == "" {
dbport = "3306"
}
dsn := dbuser + ":" + dbpassword + "@tcp(" + dbhost + ":" + dbport + ")/" + dbname + "?charset=utf8&loc=Asia%2FShanghai"
orm.RegisterDataBase("default", "mysql", dsn)
orm.RegisterModel(new(Ids))

RedisClient = &redis.Pool{
// 从配置文件获取maxidle以及maxactive,取不到则用后面的默认值
MaxIdle: 1,
MaxActive: 10,
IdleTimeout: 180 * time.Second,
Dial: func() (redis.Conn, error) {
c, err := redis.Dial("tcp", REDIS_HOST)
if err != nil {
return nil, err
}
// 选择db
c.Do("SELECT", REDIS_DB)
return c, nil
},
}

}

func main() {
if vsMode == "" {
return
}
startTime := time.Now().Unix()
var hiFunc func() = func() {}
switch vsMode {
case "file":
hiFunc = fileMode
case "db":
hiFunc = dbMode
case "redis":
hiFunc = redisMode
}
for i := 0; i < ExecTimes; i++ {
hiFunc()
}
endTime := time.Now().Unix()
usetime := endTime - startTime
fmt.Println("vsMode:", vsMode, " usetime:", usetime)
}

func main2() {
if vsMode == "" {
return
}
startTime := time.Now().Unix()

switch vsMode {
case "file":
for i := 0; i < ExecTimes; i++ {
fileMode()
}
case "db":
for i := 0; i < ExecTimes; i++ {
dbMode()
}
case "redis":
for i := 0; i < ExecTimes; i++ {
redisMode()
}
}
endTime := time.Now().Unix()
usetime := endTime - startTime
fmt.Println("vsMode:", vsMode, " usetime:", usetime)
}

//16
func fileMode() {
b, err := ioutil.ReadFile("/Users/weichangdong/wcd/code/go/src/tools/zap-2019-08-09T08-35-15.713.log") // just pass the file name
if err != nil {
fmt.Print(err)
}
str := string(b)
_ = str
}

//81
func dbMode() {
Mydb := orm.NewOrm()
var id int64
Mydb.Raw("select id from ids limit 1").QueryRow(&id)
_ = id
}

//37
func redisMode() {
rc := RedisClient.Get()
info, _ := rc.Do("get", "wcd")
_ = info
//fmt.Println("info:", string(info.([]uint8)), " err:", err)
rc.Close()
}

func GetFuncName() string {
pc := make([]uintptr, 1)
runtime.Callers(2, pc)
if len(pc) > 0 {
f := runtime.FuncForPC(pc[0])
fullName := f.Name()
tmpName := strings.Split(fullName, ".")
if len(tmpName) > 0 {
return tmpName[len(tmpName)-1]
}
return fullName
}
return "GetFuncName"
}

本文最后更新于 天前,文中所描述的信息可能已发生改变