Dagger2Metrics Plugin

Dagger2 graph initialization performance metrics

License

License

Categories

Categories

Dagger Application Layer Libs Dependency Injection Metrics Application Testing & Monitoring Monitoring
GroupId

GroupId

com.frogermcs.dagger2metrics
ArtifactId

ArtifactId

dagger2metrics-plugin
Last Version

Last Version

0.2.1
Release Date

Release Date

Type

Type

jar
Description

Description

Dagger2Metrics Plugin
Dagger2 graph initialization performance metrics
Project URL

Project URL

https://github.com/frogermcs/dagger2metrics
Source Code Management

Source Code Management

https://github.com/frogermcs/dagger2metrics

Download dagger2metrics-plugin

How to add to project

<!-- https://jarcasting.com/artifacts/com.frogermcs.dagger2metrics/dagger2metrics-plugin/ -->
<dependency>
    <groupId>com.frogermcs.dagger2metrics</groupId>
    <artifactId>dagger2metrics-plugin</artifactId>
    <version>0.2.1</version>
</dependency>
// https://jarcasting.com/artifacts/com.frogermcs.dagger2metrics/dagger2metrics-plugin/
implementation 'com.frogermcs.dagger2metrics:dagger2metrics-plugin:0.2.1'
// https://jarcasting.com/artifacts/com.frogermcs.dagger2metrics/dagger2metrics-plugin/
implementation ("com.frogermcs.dagger2metrics:dagger2metrics-plugin:0.2.1")
'com.frogermcs.dagger2metrics:dagger2metrics-plugin:jar:0.2.1'
<dependency org="com.frogermcs.dagger2metrics" name="dagger2metrics-plugin" rev="0.2.1">
  <artifact name="dagger2metrics-plugin" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.frogermcs.dagger2metrics', module='dagger2metrics-plugin', version='0.2.1')
)
libraryDependencies += "com.frogermcs.dagger2metrics" % "dagger2metrics-plugin" % "0.2.1"
[com.frogermcs.dagger2metrics/dagger2metrics-plugin "0.2.1"]

Dependencies

compile (3)

Group / Artifact Type Version
org.aspectj : aspectjrt jar 1.8.8
org.aspectj : aspectjtools jar 1.8.8
com.android.tools.build : gradle jar 1.3.1

Project Modules

There are no modules declared in this project.

Deprecation

This project evolved to AndroidDevMetrics (metrics for Android Development, not only for Dagger 2) and now can be found here: AndroidDevMetrics

Dagger2Metrics

Performance metrics library for Dagger 2 initialization process.

Android Arsenal

If you use Dagger 2 for dependency injection in your Android app you probably know that it's super optimized and non-reflection piece of code served by great engineers from Google (and formerly from Square).

Even with all those optimizations and fully generated non-dynamic code, still there are potential performance issues hidden in our code and all 3rd parties injected via Dagger 2.

The problem with performance is that it often decreases slowly so in day-by-day development it's hard to notice that our app (or Activity or any other view) launches 50ms longer. And another 150ms longer, and another 100ms...

With Dagger2Metrics you will be able to see how much time was needed to initialize all requested dependencies (and dependencies of those dependencies).

screenshot.png

Getting started

In your build.gradle:

 buildscript {
  repositories {
    jcenter()
  }

  dependencies {
    classpath 'com.frogermcs.dagger2metrics:dagger2metrics-plugin:0.2.1'
  }
}

apply plugin: 'com.android.application'
apply plugin: 'com.frogermcs.dagger2metrics'

In your Application class:

public class ExampleApplication extends Application {

 @Override
 public void onCreate() {
     super.onCreate();
     //Use it only in debug builds
     if (BuildConfig.DEBUG) {
         Dagger2Metrics.enableCapturing(this);
     }
  }
 }

And that's all. In your app you will see notification which opens simple summary of all finished initializations.

notification.png

How does it work?

Dagger2Metrics captures all initializations from both - @Module -> @Provides annotated methods and @Inject annotated constructors.

In summary you will see the most-top injected dependencies with trees of their dependencies. Each of dependency shows how much time was needed to provide this object to Dagger 2 object graph (construction time itself and overall time with all dependencies).

screenshot.png

Why I don't see all (sub) dependencies?

Metric trees don't show dependencies which are already provided to Dagger's graph, so only those constructed from scratch will be visible. Mainly because of readability and from a simple reason - we don't want to measure Dagger 2 performance which in most cases won't be an issue.
Instead we should be sure that our code provides requested dependencies as fast as it's possible.

Customizing

Dagger2Metrics has three default levels of warnings:

Dagger2Metrics.WARNING_1_LIMIT_MILLIS // 30ms
Dagger2Metrics.WARNING_2_LIMIT_MILLIS // 50ms
Dagger2Metrics.WARNING_3_LIMIT_MILLIS // 100ms

You can adjust them to your needs.

Example app

You can check GithubClient project - example Android app which shows how to use Dagger 2. Most recent version uses Dagger2Metrics for measuring construction times.

More about Dagger 2

If you're just starting with Dagger 2, here is the list of resources which can help you with it:

GithubClient - example of Github API client implemented on top of Dagger 2 DI framework.

Blog posts:

License

Copyright 2016 Miroslaw Stanek

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Versions

Version
0.2.1
0.2