Skip to content

Commit

Permalink
Performance adjustment.
Browse files Browse the repository at this point in the history
  • Loading branch information
jawalonoski authored and hadleynet committed Apr 3, 2024
1 parent 2112a85 commit 3762fd3
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 6 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/mitre/synthea/engine/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ public boolean process(Person person, long time, boolean terminateOnDeath) {
person.attributes.put(historyKey, person.history);
/* TODO - determining whether or not this the first time a person has
entered a submodule is currently not easily computed, so we use `true` below. */
TransitionMetrics.getMetric(historyKey, initial.name).enter(true);
TransitionMetrics.enter(historyKey, initial.name, true);
}
person.history = (List<State>) person.attributes.get(historyKey);
State current = person.history.get(0);
Expand All @@ -424,10 +424,10 @@ public boolean process(Person person, long time, boolean terminateOnDeath) {
Long duration = (exited - entered);
nextStateName = current.transition(person, time);
boolean firstTime = !person.hadPriorState(nextStateName);
TransitionMetrics.getMetric(historyKey, current.name).exit(nextStateName, duration);
TransitionMetrics.exit(historyKey, current.name, nextStateName, duration);
current = states.get(nextStateName).clone(); // clone the state so we don't dirty the original
person.history.add(0, current);
TransitionMetrics.getMetric(historyKey, nextStateName).enter(firstTime);
TransitionMetrics.enter(historyKey, nextStateName, firstTime);
if (exited != null && exited < time) {
// stop if the patient died in the meantime...
if (terminateOnDeath && !person.alive(exited)) {
Expand Down
35 changes: 32 additions & 3 deletions src/main/java/org/mitre/synthea/helpers/TransitionMetrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,42 @@ public abstract class TransitionMetrics {
private static final Table<String, String, Metric> metrics =
Tables.synchronizedTable(HashBasedTable.create());

public static boolean enabled =
Config.getAsBoolean("generate.track_detailed_transition_metrics", false);

/**
* Track entering a state within a given module.
* @param module The name of the module.
* @param state The name of the state.
* @param firstTime Whether or not this state was previously entered.
*/
public static void enter(String module, String state, boolean firstTime) {
if (enabled) {
getMetric(module, state).enter(firstTime);
}
}

/**
* Track exiting a state and the resulting destination.
* @param module The name of the module.
* @param state The name of the state.
* @param destination Target state that was transitioned to.
* @param duration The time in milliseconds spent within the state.
*/
public static void exit(String module, String state, String destination, long duration) {
if (enabled) {
getMetric(module, state).exit(destination, duration);
}
}

/**
* Get the Metric object for the given State in the given Module.
*
* @param moduleName Name of the module
* @param stateName Name of the state
* @return Metric object
*/
public static Metric getMetric(String moduleName, String stateName) {
static Metric getMetric(String moduleName, String stateName) {
Metric metric = metrics.get(moduleName, stateName);

if (metric == null) {
Expand Down Expand Up @@ -79,7 +107,7 @@ public static void exportMetrics() {
System.out.println("Saving metrics for " + metrics.rowKeySet().size() + " modules.");

String baseDir = Config.get("exporter.baseDirectory", "./output/");
String statsDir = "statistics";
String statsDir = "metrics";
Path output = Paths.get(baseDir, statsDir);
output.toFile().mkdirs();

Expand Down Expand Up @@ -145,7 +173,8 @@ public static class Metric {
/**
* Helper function to increment the count for a destination state.
*
* @param destination Target state that was transitioned to
* @param destination Target state that was transitioned to.
* @param duration The time in milliseconds spent within the state.
*/
public void exit(String destination, long duration) {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public static void setup() {

@Test
public void testExampleModule() throws Exception {
TransitionMetrics.enabled = true;
TransitionMetrics.clear();

Provider mockProvider = TestHelper.buildMockProvider();
Expand Down Expand Up @@ -96,6 +97,7 @@ public void testExampleModule() throws Exception {
m = TransitionMetrics.getMetric(example.name, "Terminal");
assertEquals(3, m.entered.get());
assertEquals(3, m.current.get());
TransitionMetrics.enabled = false;
}

private long run(Person person, Module singleModule, long start) {
Expand Down

0 comments on commit 3762fd3

Please sign in to comment.