Skip to content

Commit

Permalink
Collect stats about number of classes instrumented per package
Browse files Browse the repository at this point in the history
Currently there is no way to collect data about how many classes
are instrumented by Robolectric in a particular package.

PiperOrigin-RevId: 351860731
  • Loading branch information
hoisie committed Jan 16, 2021
1 parent 98d1eca commit b94b2db
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public void record(long elapsedNs) {
count++;
}

public void incrementCount() {
this.count++;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,17 @@ public byte[] instrument(
MutableClass mutableClass =
perfStats.measure(
"analyze class", () -> analyzeClass(origBytes, config, classNodeProvider));
return perfStats.measure("instrument class", () -> instrumentToBytes(mutableClass));
byte[] instrumentedBytes =
perfStats.measure("instrument class", () -> instrumentToBytes(mutableClass));
recordPackageStats(perfStats, mutableClass);
return instrumentedBytes;
}

private void recordPackageStats(PerfStatsCollector perfStats, MutableClass mutableClass) {
String className = mutableClass.getName();
for (int i = className.indexOf('.'); i != -1; i = className.indexOf('.', i + 1)) {
perfStats.incrementCount("instrument package " + className.substring(0, i));
}
}

public void instrument(MutableClass mutableClass) {
Expand Down
11 changes: 11 additions & 0 deletions utils/src/main/java/org/robolectric/util/PerfStatsCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ public <T, E extends Exception> T measure(String eventName, ThrowingSupplier<T,
}
}

public void incrementCount(String eventName) {
synchronized (PerfStatsCollector.this) {
MetricKey key = new MetricKey(eventName, true);
Metric metric = metricMap.get(key);
if (metric == null) {
metricMap.put(key, metric = new Metric(key.name, key.success));
}
metric.incrementCount();
}
}

/**
* Supplier that throws an exception.
*/
Expand Down

0 comments on commit b94b2db

Please sign in to comment.