blob: aac1ea558f5573ba27814c28e441ffc12694ca73 [file] [log] [blame]
Alan Donovan96222932018-06-05 15:28:39 -04001// 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 Donovan96222932018-06-05 15:28:39 -04005package packages_test
6
7import (
Alan Donovan96222932018-06-05 15:28:39 -04008 "runtime"
Alan Donovan96222932018-06-05 15:28:39 -04009 "testing"
10 "time"
11
12 "golang.org/x/tools/go/packages"
Bryan C. Millsc17b0402019-08-29 16:22:26 -040013 "golang.org/x/tools/internal/testenv"
Alan Donovan96222932018-06-05 15:28:39 -040014)
15
16// This test loads the metadata for the standard library,
17func TestStdlibMetadata(t *testing.T) {
Bryan C. Millsc17b0402019-08-29 16:22:26 -040018 testenv.NeedsGoPackages(t)
19
Alan Donovan96222932018-06-05 15:28:39 -040020 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 Matloob0aa4b882018-08-21 17:35:57 -040027 cfg := &packages.Config{Mode: packages.LoadAllSyntax}
Ian Cottrellaf5e7882018-08-06 23:01:37 -040028 pkgs, err := packages.Load(cfg, "std")
Alan Donovan96222932018-06-05 15:28:39 -040029 if err != nil {
30 t.Fatalf("failed to load metadata: %v", err)
31 }
Alan Donovan5a00de92018-11-12 19:18:07 -050032 if packages.PrintErrors(pkgs) > 0 {
33 t.Fatal("there were errors loading standard library")
34 }
Alan Donovan96222932018-06-05 15:28:39 -040035
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 Fitzpatrickfd2d2c42018-07-14 19:31:00 +000043
44 want := 150 // 186 on linux, 185 on windows.
45 if numPkgs < want {
Alan Donovan96222932018-06-05 15:28:39 -040046 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}