blob: c44e45327fa0e3fed523e6ce6ca2698a405a949c [file] [log] [blame]
LaMont Jones54b01cd2024-10-23 13:59:40 -07001// Copyright 2024 Google Inc. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package metrics
15
16// This file contain code to extract host information on linux from
17// /proc/cpuinfo and /proc/meminfo relevant to machine performance
18
19import (
20 "reflect"
21 "testing"
22
23 "android/soong/finder/fs"
24)
25
26func TestNewCpuInfo(t *testing.T) {
27 fs := fs.NewMockFs(nil)
28
29 if err := fs.MkDirs("/proc"); err != nil {
30 t.Fatalf("failed to create /proc dir: %v", err)
31 }
32 cpuFileName := "/proc/cpuinfo"
33
34 if err := fs.WriteFile(cpuFileName, cpuData, 0644); err != nil {
35 t.Fatalf("failed to write file %s: %v", cpuFileName, err)
36 }
37
38 cpuInfo, err := NewCpuInfo(fs)
39 if err != nil {
40 t.Fatalf("got %v, want nil for error", err)
41 }
42
43 if !reflect.DeepEqual(cpuInfo, expectedCpuInfo) {
44 t.Errorf("got %v, expecting %v for CpuInfo", cpuInfo, expectedCpuInfo)
45 }
46
47}
48
49func TestNewMemInfo(t *testing.T) {
50 fs := fs.NewMockFs(nil)
51
52 if err := fs.MkDirs("/proc"); err != nil {
53 t.Fatalf("failed to create /proc dir: %v", err)
54 }
55 memFileName := "/proc/meminfo"
56
57 if err := fs.WriteFile(memFileName, memData, 0644); err != nil {
58 t.Fatalf("failed to write file %s: %v", memFileName, err)
59 }
60
61 memInfo, err := NewMemInfo(fs)
62 if err != nil {
63 t.Fatalf("got %v, want nil for error", err)
64 }
65
66 if !reflect.DeepEqual(memInfo, expectedMemInfo) {
67 t.Errorf("got %v, expecting %v for MemInfo", memInfo, expectedMemInfo)
68 }
69
70}
71
72var cpuData = []byte(`processor : 0
73vendor_id : %%VENDOR%%
74cpu family : 123
75model : 456
76model name : %%CPU MODEL NAME%%
77stepping : 0
78cpu MHz : 5555.555
79cache size : 512 KB
80physical id : 0
81siblings : 128
82core id : 0
83cpu cores : 64
84apicid : 0
85initial apicid : 0
86fpu : yes
87fpu_exception : yes
88cpuid level : 789
89wp : yes
90flags : %%cpu flags go here%%
91bugs : %%bugs go here%%
92
93processor : 1
94vendor_id : %%BADVENDOR%%
95cpu family : 234
96model : 567
97model name : %%BAD MODEL NAME%%
98flags : %%BAD cpu flags go here%%
99`)
100
101var expectedCpuInfo = &CpuInfo{
102 VendorId: "%%VENDOR%%",
103 ModelName: "%%CPU MODEL NAME%%",
104 CpuCores: 64,
105 Flags: "%%cpu flags go here%%",
106}
107
108var memData = []byte(`MemTotal: 1000 mB
109MemFree: 10240000
110MemAvailable: 3000 kB
111Buffers: 7177844 kB
112`)
113
114var expectedMemInfo = &MemInfo{
115 MemTotal: 1048576000,
116 MemFree: 10240000,
117 MemAvailable: 3072000,
118}