Alan Donovan | 9622293 | 2018-06-05 15:28:39 -0400 | [diff] [blame] | 1 | // Copyright 2018 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 | |
Alan Donovan | 9622293 | 2018-06-05 15:28:39 -0400 | [diff] [blame] | 5 | package packages_test |
| 6 | |
| 7 | import ( |
Alan Donovan | 9622293 | 2018-06-05 15:28:39 -0400 | [diff] [blame] | 8 | "runtime" |
Alan Donovan | 9622293 | 2018-06-05 15:28:39 -0400 | [diff] [blame] | 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "golang.org/x/tools/go/packages" |
Bryan C. Mills | c17b040 | 2019-08-29 16:22:26 -0400 | [diff] [blame] | 13 | "golang.org/x/tools/internal/testenv" |
Alan Donovan | 9622293 | 2018-06-05 15:28:39 -0400 | [diff] [blame] | 14 | ) |
| 15 | |
| 16 | // This test loads the metadata for the standard library, |
| 17 | func TestStdlibMetadata(t *testing.T) { |
Bryan C. Mills | c17b040 | 2019-08-29 16:22:26 -0400 | [diff] [blame] | 18 | testenv.NeedsGoPackages(t) |
| 19 | |
Alan Donovan | 9622293 | 2018-06-05 15:28:39 -0400 | [diff] [blame] | 20 | runtime.GC() |
| 21 | t0 := time.Now() |
| 22 | var memstats runtime.MemStats |
| 23 | runtime.ReadMemStats(&memstats) |
| 24 | alloc := memstats.Alloc |
| 25 | |
| 26 | // Load, parse and type-check the program. |
Michael Matloob | 0aa4b88 | 2018-08-21 17:35:57 -0400 | [diff] [blame] | 27 | cfg := &packages.Config{Mode: packages.LoadAllSyntax} |
Ian Cottrell | af5e788 | 2018-08-06 23:01:37 -0400 | [diff] [blame] | 28 | pkgs, err := packages.Load(cfg, "std") |
Alan Donovan | 9622293 | 2018-06-05 15:28:39 -0400 | [diff] [blame] | 29 | if err != nil { |
| 30 | t.Fatalf("failed to load metadata: %v", err) |
| 31 | } |
Alan Donovan | 5a00de9 | 2018-11-12 19:18:07 -0500 | [diff] [blame] | 32 | if packages.PrintErrors(pkgs) > 0 { |
| 33 | t.Fatal("there were errors loading standard library") |
| 34 | } |
Alan Donovan | 9622293 | 2018-06-05 15:28:39 -0400 | [diff] [blame] | 35 | |
| 36 | t1 := time.Now() |
| 37 | runtime.GC() |
| 38 | runtime.ReadMemStats(&memstats) |
| 39 | runtime.KeepAlive(pkgs) |
| 40 | |
| 41 | t.Logf("Loaded %d packages", len(pkgs)) |
| 42 | numPkgs := len(pkgs) |
Brad Fitzpatrick | fd2d2c4 | 2018-07-14 19:31:00 +0000 | [diff] [blame] | 43 | |
| 44 | want := 150 // 186 on linux, 185 on windows. |
| 45 | if numPkgs < want { |
Alan Donovan | 9622293 | 2018-06-05 15:28:39 -0400 | [diff] [blame] | 46 | t.Errorf("Loaded only %d packages, want at least %d", numPkgs, want) |
| 47 | } |
| 48 | |
| 49 | t.Log("GOMAXPROCS: ", runtime.GOMAXPROCS(0)) |
| 50 | t.Log("Metadata: ", t1.Sub(t0)) // ~800ms on 12 threads |
| 51 | t.Log("#MB: ", int64(memstats.Alloc-alloc)/1000000) // ~1MB |
| 52 | } |