Skip to content

Commit

Permalink
Merge pull request #6161 from robolectric/piper_351860731
Browse files Browse the repository at this point in the history
Collect stats about which packages are instrumented
  • Loading branch information
hoisie committed Jan 16, 2021
2 parents a3d94c2 + b94b2db commit e0e1b5e
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 e0e1b5e

Please sign in to comment.