fix(driver): a CPU sample the driver never took is absent, not zero

CDP exposes no per-page CPU, so chrome's Metrics filled CPUPercent with a zero,
which is the same answer an idle app gives. Making the field optional is the
same fix as the iOS reads: absence is the only honest way to say nothing looked.
This commit is contained in:
pj committed 2026-08-22 21:41:40 +05:30
1 parent 577993b540
commit 1fb910e0a2
3 files changed
+8 -3

No files matched your search

+4 -1
View File
@@ -183,7 +183,10 @@ type Health struct {
} }
type Metrics struct { type Metrics struct {
CPUPercent float64 // CPUPercent is nil on a driver that samples memory but not CPU, which
// Chrome does: zero is a reading an idle app can produce, so a driver that
// never looked has to answer with nothing rather than with that.
CPUPercent *float64
HeapBytes int64 HeapBytes int64
TotalMemoryBytes int64 TotalMemoryBytes int64
} }
+2 -1
View File
@@ -300,8 +300,9 @@ func (c *Client) Metrics(ctx context.Context, bundleID string) (driver.Metrics,
if err != nil { if err != nil {
return driver.Metrics{}, err return driver.Metrics{}, err
} }
cpuPercent := response.GetCpuPercent()
return driver.Metrics{ return driver.Metrics{
CPUPercent: response.GetCpuPercent(), CPUPercent: &cpuPercent,
HeapBytes: response.GetHeapBytes(), HeapBytes: response.GetHeapBytes(),
TotalMemoryBytes: response.GetTotalMemoryBytes(), TotalMemoryBytes: response.GetTotalMemoryBytes(),
}, nil }, nil
+2 -1
View File
@@ -651,7 +651,8 @@ func TestClient_MetricsMapsResponseFields(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if got.CPUPercent != 12.5 || got.HeapBytes != 100 || got.TotalMemoryBytes != 200 { if got.CPUPercent == nil || *got.CPUPercent != 12.5 ||
got.HeapBytes != 100 || got.TotalMemoryBytes != 200 {
t.Errorf("metrics mapping wrong: %+v", got) t.Errorf("metrics mapping wrong: %+v", got)
} }
if len(state.fake.metricsBundles) != 1 || state.fake.metricsBundles[0] != "com.example" { if len(state.fake.metricsBundles) != 1 || state.fake.metricsBundles[0] != "com.example" {