| LaMont Jones | 54b01cd | 2024-10-23 13:59:40 -0700 | [diff] [blame^] | 1 | // 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. |
| 14 | package 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 | |
| 19 | import ( |
| 20 | "reflect" |
| 21 | "testing" |
| 22 | |
| 23 | "android/soong/finder/fs" |
| 24 | ) |
| 25 | |
| 26 | func 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 | |
| 49 | func 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 | |
| 72 | var cpuData = []byte(`processor : 0 |
| 73 | vendor_id : %%VENDOR%% |
| 74 | cpu family : 123 |
| 75 | model : 456 |
| 76 | model name : %%CPU MODEL NAME%% |
| 77 | stepping : 0 |
| 78 | cpu MHz : 5555.555 |
| 79 | cache size : 512 KB |
| 80 | physical id : 0 |
| 81 | siblings : 128 |
| 82 | core id : 0 |
| 83 | cpu cores : 64 |
| 84 | apicid : 0 |
| 85 | initial apicid : 0 |
| 86 | fpu : yes |
| 87 | fpu_exception : yes |
| 88 | cpuid level : 789 |
| 89 | wp : yes |
| 90 | flags : %%cpu flags go here%% |
| 91 | bugs : %%bugs go here%% |
| 92 | |
| 93 | processor : 1 |
| 94 | vendor_id : %%BADVENDOR%% |
| 95 | cpu family : 234 |
| 96 | model : 567 |
| 97 | model name : %%BAD MODEL NAME%% |
| 98 | flags : %%BAD cpu flags go here%% |
| 99 | `) |
| 100 | |
| 101 | var expectedCpuInfo = &CpuInfo{ |
| 102 | VendorId: "%%VENDOR%%", |
| 103 | ModelName: "%%CPU MODEL NAME%%", |
| 104 | CpuCores: 64, |
| 105 | Flags: "%%cpu flags go here%%", |
| 106 | } |
| 107 | |
| 108 | var memData = []byte(`MemTotal: 1000 mB |
| 109 | MemFree: 10240000 |
| 110 | MemAvailable: 3000 kB |
| 111 | Buffers: 7177844 kB |
| 112 | `) |
| 113 | |
| 114 | var expectedMemInfo = &MemInfo{ |
| 115 | MemTotal: 1048576000, |
| 116 | MemFree: 10240000, |
| 117 | MemAvailable: 3072000, |
| 118 | } |