How to instrument Dagger 2 to measure/trace creation time of different parts of the dependency graph?
Asked Answered
H

1

6

Does anyone know anyway or library to instrument Dagger 2 and set traces to see how long each module takes to create on startup?

We are trying to reduce cold startup time of an Android time and we would like to be able to measure how much time is spent on creating each module that is built on startup so that we can track over time which modules are taking up more time, and find out where is it worth to spend time trying to defer some initializations.

I have found this online: http://frogermcs.github.io/dagger2metrics-measure-performance-of-graph-initialization/ but couldn’t really make it work

I skimmed through Dagger2 code, considering to try to add traces to the code generated tasks, but it seemed like a daunting task and we don't want to maintain a fork.

Halves answered 12/2, 2019 at 12:14 Comment(0)
S
0

I created the Gretel gradle plugin that instruments android artifacts.

Gretel provides a DSL that enables you to add system traces to various parts of an android app during compile time. This can be used to inject traces into the code that is generated by Dagger 2 to facilitate the dependency injection.

For example, I applied the Gretel plugin to the Star Map app, which uses Dagger, with the following Gretel setup:

plugins {
    id 'de.awenger.gretel' version '0.2.0'
}
android {
    gretel {
        traces = [
                defineTrace {
                    // Add traces to all methods in all classes that extend Application
                    classes = classes(null, type("android.app", "Application"), null)
                },
                defineTrace {
                    // Add traces to all methods in all classes that extend Activity
                    classes = classes(null, type("android.app", "Activity"), null)
                },
                defineTrace {
                    // Add traces to all methods in all classes that extend ApplicationComponent
                    // Dagger Application Component of the Star Map app
                    // The implementation of this interface is generated at compile time
                    classes = classes(null, type("com.google.android.stardroid", "ApplicationComponent"), null)
                },
                defineTrace {
                    // Add traces to all get methods of all classes that extend Provider/Factory
                    // providing the dependencies for dagger
                    classes = classes(null, type("javax.inject", "Provider"), null)
                    methods = [method("get")]
                }
        ]
    }
}

The following screenshot shows the system trace for the app start, first without the Gretel plugin and then with the plugin applied: System trace of the Star Map app with and without the Gretel plugin

Sheley answered 1/4, 2024 at 10:30 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.