BatteryMetrics module

BatteryMetrics Battery instrumentation for Android

License

License

Categories

Categories

Metrics Application Testing & Monitoring Monitoring
GroupId

GroupId

com.facebook.battery
ArtifactId

ArtifactId

metrics
Last Version

Last Version

1.0.0
Release Date

Release Date

Type

Type

aar
Description

Description

BatteryMetrics module
BatteryMetrics Battery instrumentation for Android
Project URL

Project URL

https://github.com/facebookincubator/Battery-Metrics
Source Code Management

Source Code Management

https://github.com/facebookincubator/Battery-Metrics.git

Download metrics

How to add to project

<!-- https://jarcasting.com/artifacts/com.facebook.battery/metrics/ -->
<dependency>
    <groupId>com.facebook.battery</groupId>
    <artifactId>metrics</artifactId>
    <version>1.0.0</version>
    <type>aar</type>
</dependency>
// https://jarcasting.com/artifacts/com.facebook.battery/metrics/
implementation 'com.facebook.battery:metrics:1.0.0'
// https://jarcasting.com/artifacts/com.facebook.battery/metrics/
implementation ("com.facebook.battery:metrics:1.0.0")
'com.facebook.battery:metrics:aar:1.0.0'
<dependency org="com.facebook.battery" name="metrics" rev="1.0.0">
  <artifact name="metrics" type="aar" />
</dependency>
@Grapes(
@Grab(group='com.facebook.battery', module='metrics', version='1.0.0')
)
libraryDependencies += "com.facebook.battery" % "metrics" % "1.0.0"
[com.facebook.battery/metrics "1.0.0"]

Dependencies

compile (2)

Group / Artifact Type Version
com.facebook.infer.annotation : infer-annotation jar 0.11.0
com.android.support » support-core-utils jar 26.1.0

Project Modules

There are no modules declared in this project.

Battery Metrics

Battery Metrics is a lightweight android library to quickly instrument several metrics for understanding battery consumption.

As a developer, it's surprisingly hard to understand how your application affects battery life on Android — relying on the operating system level reported battery level tends to be inaccurate (because the reported levels are fairly coarse, affected by every app running on the device and smoothed out) and while it's possible to get really good measurements locally you don't really know what sort of experience users are having in the wild.

The library helps instrument hardware utilization to be able to understand how the application is behaving -- most of the underlying hardware metrics are either exposed by the OS, or not directly accessible -- which is where this library comes into play. We've written several metrics collectors that read from procfiles, or provide a consistent way to call into to instrument your application.

Adding BatteryMetrics

Add jcenter to your repositories –

repositories {
  jcenter()
}

And add dependencies on the projects you'd like to use in build.gradle

dependencies {
    implementation 'com.facebook.battery:metrics:1.0.0'
    implementation 'com.facebook.battery:reporters:1.0.0'   // optional
    implementation 'com.facebook.battery:serializers:1.0.0' // optional
}

Quick Start

As a simple quickstart, let's instrument sample activity to check CPU time while the activity is being used in the foreground --

class SampleActivity extends Activity {

  private static final CpuMetricsCollector sCollector = new CpuMetricsCollector();
  private final CpuMetrics mInitialMetrics = sCollector.createMetrics();
  private final CpuMetrics mFinalMetrics = sCollector.createMetrics();

  @Override
  protected void onResume() {
    super.onResume();
    sCollector.getSnapshot(mInitialMetrics);
  }

  @Override
  protected void onPause() {
    super.onPause();
    sCollector.getSnapshot(mFinalMetrics);

    Log.d("BatteryMetrics", mFinalMetrics.diff(mInitialMetrics).toString());
  }
}

And foregrounding and background the application prints the metrics to logcat --

CpuMetrics{userTimeS=0.06, systemTimeS=0.04, childUserTimeS=0.0, childSystemTimeS=0.0}

Sample App

Building further on this, there are many more metrics to collect, and some utility classes to reduce boilerplate -- a more detailed deep dive into using the API is in the sample app: check out sample/../BatteryApplication.java.

You can quickly install and run the app --

./gradlew :sample:installDebug

Documentation

Community

License

BatteryMetrics is MIT-licensed.

com.facebook.battery

Facebook Incubator

We work hard to contribute our work back to the web, mobile, big data, & infrastructure communities. NB: members must have two-factor auth.

Versions

Version
1.0.0
0.0.1