OSDN Git Service

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