Imported Upstream version 4.8.1
[platform/upstream/gcc48.git] / gcc / testsuite / go.test / test / bench / garbage / parser.go
1 // Copyright 2010 The Go Authors.  All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 // Garbage collection benchmark: parse Go packages repeatedly.
6
7 package main
8
9 import (
10         "flag"
11         "fmt"
12         "go/ast"
13         "go/parser"
14         "go/token"
15         "log"
16         "net/http"
17         _ "net/http/pprof"
18         "os"
19         "path"
20         "runtime"
21         "strings"
22         "time"
23 )
24
25 var serve = flag.String("serve", "", "serve http on this address at end")
26
27 func isGoFile(dir os.FileInfo) bool {
28         return !dir.IsDir() &&
29                 !strings.HasPrefix(dir.Name(), ".") && // ignore .files
30                 path.Ext(dir.Name()) == ".go"
31 }
32
33 func isPkgFile(dir os.FileInfo) bool {
34         return isGoFile(dir) &&
35                 !strings.HasSuffix(dir.Name(), "_test.go") // ignore test files
36 }
37
38 func pkgName(filename string) string {
39         file, err := parser.ParseFile(token.NewFileSet(), filename, nil, parser.PackageClauseOnly)
40         if err != nil || file == nil {
41                 return ""
42         }
43         return file.Name.Name
44 }
45
46 func parseDir(dirpath string) map[string]*ast.Package {
47         // the package name is the directory name within its parent
48         // (use dirname instead of path because dirname is clean; i.e. has no trailing '/')
49         _, pkgname := path.Split(dirpath)
50
51         // filter function to select the desired .go files
52         filter := func(d os.FileInfo) bool {
53                 if isPkgFile(d) {
54                         // Some directories contain main packages: Only accept
55                         // files that belong to the expected package so that
56                         // parser.ParsePackage doesn't return "multiple packages
57                         // found" errors.
58                         // Additionally, accept the special package name
59                         // fakePkgName if we are looking at cmd documentation.
60                         name := pkgName(dirpath + "/" + d.Name())
61                         return name == pkgname
62                 }
63                 return false
64         }
65
66         // get package AST
67         pkgs, err := parser.ParseDir(token.NewFileSet(), dirpath, filter, parser.ParseComments)
68         if err != nil {
69                 println("parse", dirpath, err.Error())
70                 panic("fail")
71         }
72         return pkgs
73 }
74
75 func main() {
76         st := new(runtime.MemStats)
77         packages = append(packages, packages...)
78         packages = append(packages, packages...)
79         n := flag.Int("n", 4, "iterations")
80         p := flag.Int("p", len(packages), "# of packages to keep in memory")
81         flag.BoolVar(&st.DebugGC, "d", st.DebugGC, "print GC debugging info (pause times)")
82         flag.Parse()
83
84         var lastParsed []map[string]*ast.Package
85         var t0 time.Time
86         var numGC uint32
87         var pauseTotalNs uint64
88         pkgroot := runtime.GOROOT() + "/src/pkg/"
89         for pass := 0; pass < 2; pass++ {
90                 // Once the heap is grown to full size, reset counters.
91                 // This hides the start-up pauses, which are much smaller
92                 // than the normal pauses and would otherwise make
93                 // the average look much better than it actually is.
94                 runtime.ReadMemStats(st)
95                 numGC = st.NumGC
96                 pauseTotalNs = st.PauseTotalNs
97                 t0 = time.Now()
98
99                 for i := 0; i < *n; i++ {
100                         parsed := make([]map[string]*ast.Package, *p)
101                         for j := range parsed {
102                                 parsed[j] = parseDir(pkgroot + packages[j%len(packages)])
103                         }
104                         if i+1 == *n && *serve != "" {
105                                 lastParsed = parsed
106                         }
107                 }
108                 runtime.GC()
109                 runtime.GC()
110         }
111         t1 := time.Now()
112
113         runtime.ReadMemStats(st)
114         st.NumGC -= numGC
115         st.PauseTotalNs -= pauseTotalNs
116         fmt.Printf("Alloc=%d/%d Heap=%d Mallocs=%d PauseTime=%.3f/%d = %.3f\n",
117                 st.Alloc, st.TotalAlloc,
118                 st.Sys,
119                 st.Mallocs, float64(st.PauseTotalNs)/1e9,
120                 st.NumGC, float64(st.PauseTotalNs)/1e9/float64(st.NumGC))
121
122         /*
123                 fmt.Printf("%10s %10s %10s\n", "size", "#alloc", "#free")
124                 for _, s := range st.BySize {
125                         fmt.Printf("%10d %10d %10d\n", s.Size, s.Mallocs, s.Frees)
126                 }
127         */
128         // Standard gotest benchmark output, collected by build dashboard.
129         gcstats("BenchmarkParser", *n, t1.Sub(t0))
130
131         if *serve != "" {
132                 log.Fatal(http.ListenAndServe(*serve, nil))
133                 println(lastParsed)
134         }
135 }
136
137 var packages = []string{
138         "archive/tar",
139         "encoding/asn1",
140         "math/big",
141         "bufio",
142         "bytes",
143         "math/cmplx",
144         "compress/flate",
145         "compress/gzip",
146         "compress/zlib",
147         "container/heap",
148         "container/list",
149         "container/ring",
150         "crypto/aes",
151         "crypto/hmac",
152         "crypto/md5",
153         "crypto/rand",
154         "crypto/rc4",
155         "crypto/rsa",
156         "crypto/sha1",
157         "crypto/sha256",
158         "crypto/sha512",
159         "crypto/subtle",
160         "crypto/tls",
161         "crypto/x509",
162         "debug/dwarf",
163         "debug/macho",
164         "debug/elf",
165         "debug/gosym",
166         "exp/ebnf",
167         "encoding/ascii85",
168         "encoding/base64",
169         "encoding/binary",
170         "encoding/hex",
171         "encoding/pem",
172         "os/exec",
173         "flag",
174         "fmt",
175         "go/ast",
176         "go/doc",
177         "go/parser",
178         "go/printer",
179         "go/scanner",
180         "go/token",
181         "encoding/gob",
182         "hash",
183         "hash/adler32",
184         "hash/crc32",
185         "hash/crc64",
186         "net/http",
187         "image",
188         "image/jpeg",
189         "image/png",
190         "io",
191         "io/ioutil",
192         "encoding/json",
193         "log",
194         "math",
195         "mime",
196         "net",
197         "os",
198         "path",
199         "math/rand",
200         "reflect",
201         "regexp",
202         "net/rpc",
203         "runtime",
204         "text/scanner",
205         "sort",
206         "net/smtp",
207         "strconv",
208         "strings",
209         "sync",
210         "syscall",
211         "log/syslog",
212         "text/tabwriter",
213         "text/template",
214         "testing",
215         "testing/iotest",
216         "testing/quick",
217         "time",
218         "unicode",
219         "unicode/utf8",
220         "unicode/utf16",
221         "encoding/xml",
222 }