-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathgoker_evaluation.go
94 lines (85 loc) · 2.17 KB
/
goker_evaluation.go
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
package gobench
import (
"bytes"
"go/ast"
"go/parser"
"go/printer"
"go/token"
"golang.org/x/tools/go/ast/astutil"
"io/ioutil"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)
func InstrumentGoleak(file string) {
fset := token.NewFileSet()
astF, err := parser.ParseFile(fset, file, nil, 0)
if err != nil {
panic(err)
}
var entryFunc *ast.FuncDecl
astutil.Apply(astF, func(cur *astutil.Cursor) bool {
if node, ok := cur.Node().(*ast.FuncDecl); ok {
if strings.HasPrefix(node.Name.Name, "Test") {
entryFunc = node
return false
}
}
return true
}, nil)
entryFunc.Body.List = append([]ast.Stmt{&ast.DeferStmt{
Call: &ast.CallExpr{
Fun: &ast.SelectorExpr{
X: &ast.Ident{Name: "goleak"},
Sel: &ast.Ident{Name: "VerifyNone"},
},
Args: []ast.Expr{
&ast.BasicLit{Value: "t"},
},
}}}, entryFunc.Body.List...)
var buf bytes.Buffer
astutil.AddImport(fset, astF, "go.uber.org/goleak")
printer.Fprint(&buf, fset, astF)
ioutil.WriteFile(file, buf.Bytes(), 0644)
}
func InstrumentGoDeadlock(file string) {
commands := [][]string{
[]string{"sed", "-i", "s/sync.RWMutex/deadlock.RWMutex/", file},
[]string{"sed", "-i", "s/sync.Mutex/deadlock.Mutex/", file},
}
for _, args := range commands {
if out, err := exec.Command(args[0], args[1:]...).CombinedOutput(); err != nil {
log.Println(string(out))
panic(err)
}
}
abspath, _ := filepath.Abs(file)
cmd := exec.Command("goimports", "-w", abspath)
cmd.Dir = os.Getenv("HOME")
if out, err := cmd.CombinedOutput(); err != nil {
log.Println(string(out))
panic(err)
}
}
func InstrumentMainFunc(file string) {
commands := [][]string{
[]string{"sed", "-i", "s/^func Test.*/func main() {\\n/", file},
[]string{"sed", "-i", "s/^package.*/package main\\n/", file},
[]string{"sed", "-i", "/*\"testing\"*/d", file},
}
for _, args := range commands {
if out, err := exec.Command(args[0], args[1:]...).CombinedOutput(); err != nil {
log.Println(string(out))
panic(err)
}
}
abspath, _ := filepath.Abs(file)
cmd := exec.Command("goimports", "-w", abspath)
cmd.Dir = os.Getenv("HOME")
if out, err := cmd.CombinedOutput(); err != nil {
log.Println(string(out))
panic(err)
}
}