项目 / facebookincubator/Battery-Metrics

facebookincubator/Battery-Metrics

行业推荐
语言 Java标签 —数据更新 2026-11-25 06:49:28 · 仓库 2026-11-09

一段话介绍

Library that helps in instrumenting battery related system metrics.

本站整理,非 GitHub 官方 description。

官方 description(GitHub API 原样)
Library that helps in instrumenting battery related system metrics.
771累计星
80Fork
—峰值日增
0上榜次数
总榜
暂未入榜
日榜
暂未入榜
周榜
暂未入榜
月榜
暂未入榜

官方快照(gh_history.db,2026-11-25):总榜 未进 Top50 · 日榜 未进 Top50 · 周榜未满 7 天基线(官方不生成) · 月榜暂无官方口径;下方四卡为站内趋势聚合(周卡按上榜次数,非官方 7 日增量窗)。

行业与场景

按行业选型 →

数据仓库建模与指标治理

适配系统:数据仓库、指标管理平台 · 得分 1.3

提供分层建模与指标定义能力,可支撑口径统一、血缘追踪与自助分析。

更多信息

以下三段按该项目自己的 GitHub README 章节摘录(可合理截断并标明);README 没有对应章节的留空,不编造。

我的用法
可创建CpuMetricsCollector并保存两次快照,对比前后指标得出活动前台期间CPU耗时并输出日志。
特点或设计要点
源文说明,仅看系统电量读数往往不够精细,本地测量也难以反映用户侧的真实情况,因此该库通过读取 proc 文件或提供统一的采集方式,对硬件利用情况进行插桩。使用时可在生命周期节点抓取快照并比较差值,例如用 `CpuMetricsCollector` 观察前台活动期间的 CPU 时间。可选组件还包括 reporters 与 serializers。详细 API 见[API 说明](https://github.com/facebookincubator/Battery-Metrics/blob/master/docs/API.md)。
星标走势还差一天的数据,明天就能看到曲线
每天 10:30 记一次星标,连续两天后自动生成曲线
首次发现 — · 最近在榜 2026-11-12 · 语言 Java · 贡献者 —
sdk

GitHub 项目主页介绍

来自 GitHub 仓库 README / 主页介绍正文,原样摘录,共 4,441 字节。

# Battery Metrics
<p align="center">
  <img src="/logo/logo.png?raw=true" title="Battery Metrics Logo"/>
</p>

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 &mdash; 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 –

```groovy
repositories {
  jcenter()
}
```

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

```groovy
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 --

```java
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 --

```java
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](https://github.com/facebookincubator/Battery-Metrics/blob/master/sample/src/main/java/com/facebook/battery/sample/BatteryApplication.java).

You can quickly install and run the app --
```
./gradlew :sample:installDebug
```

## Documentation
- [JavaDocs](https://facebookincubator.github.io/Battery-Metrics/) -- a reasonably comprehensive reference to all the exposed APIs.
- [Roadmap](https://github.com/facebookincubator/Battery-Metrics/blob/master/docs/roadmap.md) -- our planned, but unprioritized roadmap. Give us feedback!
- [Additional reading](https://github.com/facebookincubator/Battery-Metrics/blob/master/docs/references.md) -- some sources we've found useful.
- [API description](https://github.com/facebookincubator/Battery-Metrics/blob/master/docs/API.md) -- a brief description of the terms used throughout the project.
- [Getting started](https://github.com/facebookincubator/Battery-Metrics/blob/master/docs/gettingstarted.md) -- the core of the sample app described. Make sure you check out [sample/../BatteryApplication.java](https://github.com/facebookincubator/Battery-Metrics/blob/master/sample/src/main/java/com/facebook/battery/sample/BatteryApplication.java).
- [Mistrusting battery level](https://github.com/facebookincubator/Battery-Metrics/blob/master/docs/mistrustbatterylevel.md) -- why we don't rely on the os reported battery level
- [Contribution guidelines](https://github.com/facebookincubator/Battery-Metrics/blob/master/CONTRIBUTING.md) -- we'd love to see contributions to the project.

## Community
- Find us on (unsurprisingly) [Facebook](https://www.facebook.com/groups/batterymetrics/?ref=bookmarks).
- And [Slack](https://batterymetrics.slack.com/) -- and the [invite](https://goo.gl/Rb3kty).

## License
BatteryMetrics is MIT-licensed.
↑