Playbook: debug a slow path
The question, in the user's words: "Adding a credential to a user is slow. What does that code do, and where does it go to the database?"
What you get at the end: what the slow method calls, a few levels down, which of those touch a data store, and the few methods worth reading. Niro does not time anything: it tells you where to look, and a profiler tells you how long it takes.
Words used on this page:
- a callee is a method that another method calls;
- a profiler is a tool that measures where a running program spends its time;
- a named query is a database query written once on a JPA entity class and run by its name (JPA is Java's standard library that maps classes to tables).
The steps
1. How much does the method reach?
niro ask get_call_chain method_fqn=org.keycloak.models.jpa.JpaUserCredentialStore.createCredentialEntity direction=callees scope=transitive transitive_depth=2 answer=count
18 callees
confidence: complete | has_more: false — this is the COMPLETE call chain. Use it directly.
2. Which callees, and where is each called?
The full answer gives the line of every call, which matters for a slow path: a call inside a loop, or the same query twice.
niro ask get_call_chain method_fqn=org.keycloak.models.jpa.JpaUserCredentialStore.createCredentialEntity direction=callees
JpaUserCredentialStore.createCredentialEntity has 17 callees.
1. JpaUserCredentialStore.validateDuplicateCredential(RealmModel, UserModel, String, String, String) (line 147) private — called at line 162 — .../JpaUserCredentialStore.java:147-159
...
16. JpaUserCredentialStore.getStoredCredentialEntities(RealmModel, UserModel) (line 123) private — called at line 175 — .../JpaUserCredentialStore.java:123-128
17. CredentialEntity.setPriority(int) (line 144) public — called at line 177 — ...
Most of the 17 are getters and setters. Two are worth reading: validateDuplicateCredential and getStoredCredentialEntities.
3. Read the two, and only the two
niro ask get_source_code fqn=org.keycloak.models.jpa.JpaUserCredentialStore.getStoredCredentialEntities
getStoredCredentialEntities runs the named query credentialByUser, which loads every credential of the user. validateDuplicateCredential calls it when the new credential has a label (line 149), and createCredentialEntity calls it again at line 175 to work out the priority. So adding a labelled credential loads the user's whole credential list twice.
That is a lead, not a measurement. Confirm it with a profiler, or by logging the SQL the query runs.
4. Which stores does the path touch?
niro ask get_data_resource_usage resource_fqn=CREDENTIAL
In this release the answer lists the em.persist and em.find calls but not the named query, so getStoredCredentialEntities is missing from its readers. That is a known gap; step 3 found the read by reading the method.
Why the answer is complete, and where it is not
- The callee list is complete for calls the code states, with the line of each call. Library calls (
em.persist) are not listed as callees of your code, because the library is not indexed. - Niro holds no timings. A slow path found here is a place to measure, not a measured fact.
- The
answer=namesform of a deeper walk can show a direct callee at depth 2 when another direct callee also calls it (a known display problem). The count is right; use the full answer when the depth matters.
What grep would do instead
To list what a method calls, grep has nothing to search for: the callees are inside the method. The by-hand route is to read the method, then each method it calls. Here that is the 21-line method plus two private methods in the same file, which is cheap. The graph pays off when the path crosses files and interfaces, as in Playbook: change code safely.
Cost
For a short method, reading the whole method by hand can cost less than asking Niro. This playbook is about finding the methods worth a closer look, not about tokens. What these steps cost depends on your code and your question. How Niro counts the size of its answers, and how to measure the cost on your own tasks, is in Tokens, speed and accuracy.
Related
Playbook: debug a wrong value, Playbook: data stores and events.