1#!/usr/bin/env python3 2# Copyright (C) 2023 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License a 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16from python.generators.diff_tests.testing import Path, DataPath, Metric 17from python.generators.diff_tests.testing import Csv, Json, TextProto 18from python.generators.diff_tests.testing import DiffTestBlueprint 19from python.generators.diff_tests.testing import TestSuite 20 21 22class LinuxSysfsPower(TestSuite): 23 24 # Test basic battery counters. 25 def test_counters(self): 26 return DiffTestBlueprint( 27 trace=TextProto(""" 28 packet { 29 timestamp: 3000000 30 battery { 31 charge_counter_uah: 3005000 32 capacity_percent: 100.000000 33 current_ua: 0 34 } 35 } 36 """), 37 query=""" 38 SELECT * FROM ( 39 (SELECT AVG(value) AS capacity_percent FROM counters 40 WHERE name='batt.capacity_pct'), 41 (SELECT AVG(value) AS charge_uah FROM counters 42 WHERE name='batt.charge_uah'), 43 (SELECT AVG(value) AS current_ua FROM counters 44 WHERE name='batt.current_ua') 45 ); 46 """, 47 out=Csv(""" 48 "capacity_percent","charge_uah","current_ua" 49 100.000000,3005000.000000,0.000000 50 """)) 51 52 # Test multiple batteries. 53 def test_multiple_batteries(self): 54 return DiffTestBlueprint( 55 trace=TextProto(""" 56 packet { 57 timestamp: 3000000 58 battery { 59 charge_counter_uah: 3005000 60 capacity_percent: 100.000000 61 current_ua: 0 62 name: "BAT0" 63 } 64 } 65 packet { 66 timestamp: 3000000 67 battery { 68 capacity_percent: 90.000000 69 name: "BAT1" 70 } 71 } 72 """), 73 query=""" 74 SELECT name, value FROM counters WHERE name like "batt.%" ORDER BY name 75 """, 76 out=Csv(""" 77 "name","value" 78 "batt.BAT0.capacity_pct",100.000000 79 "batt.BAT0.charge_uah",3005000.000000 80 "batt.BAT0.current_ua",0.000000 81 "batt.BAT1.capacity_pct",90.000000 82 """)) 83 84 # Test convertion to charge counter from energy and voltage. 85 def test_charge_from_energy_and_voltage(self): 86 return DiffTestBlueprint( 87 trace=TextProto(""" 88 packet { 89 timestamp: 3000000 90 battery { 91 energy_counter_uwh: 56680000 92 voltage_uv: 17356000 93 } 94 } 95 packet { 96 timestamp: 4000000 97 battery { 98 energy_counter_uwh: 56600000 99 voltage_uv: 17356000 100 } 101 } 102 """), 103 query=""" 104 SELECT value 105 FROM counters 106 WHERE name = "batt.charge_uah" 107 """, 108 out=Csv(""" 109 "value" 110 3265729.000000 111 3261120.000000 112 """)) 113 114 # Test convertion to charge counter from energy and voltage: bad voltage 115 # value. 116 def test_charge_from_energy_and_bad_voltage(self): 117 return DiffTestBlueprint( 118 trace=TextProto(""" 119 packet { 120 timestamp: 3000000 121 battery { 122 energy_counter_uwh: 56680000 123 voltage_uv: 0 124 } 125 } 126 """), 127 query=""" 128 SELECT value 129 FROM counters 130 WHERE name = "batt.charge_uah" 131 """, 132 out=Csv(""" 133 "value" 134 """)) 135