diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java index 8e03f5acd..33fbd0dc2 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/BaseWorkerContext.java @@ -109,6 +109,7 @@ import org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome; import org.hl7.fhir.r5.terminologies.ValueSetExpanderSimple; import org.hl7.fhir.r5.utils.ToolingExtensions; import org.hl7.fhir.utilities.OIDUtils; +import org.hl7.fhir.utilities.TimeTracker; import org.hl7.fhir.utilities.ToolingClientLogger; import org.hl7.fhir.utilities.TranslationServices; import org.hl7.fhir.utilities.Utilities; @@ -200,17 +201,19 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte protected Parameters expParameters; private TranslationServices translator = new NullTranslator(); protected TerminologyCache txCache; - + protected TimeTracker clock; private boolean tlogging = true; public BaseWorkerContext() throws FileNotFoundException, IOException, FHIRException { txCache = new TerminologyCache(lock, null); setValidationMessageLanguage(getLocale()); + clock = new TimeTracker(); } public BaseWorkerContext(Locale locale) throws FileNotFoundException, IOException, FHIRException { txCache = new TerminologyCache(lock, null); setValidationMessageLanguage(locale); + clock = new TimeTracker(); } public BaseWorkerContext(CanonicalResourceManager codeSystems, CanonicalResourceManager valueSets, CanonicalResourceManager maps, CanonicalResourceManager profiles, @@ -221,6 +224,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte this.maps = maps; this.structures = profiles; this.guides = guides; + clock = new TimeTracker(); } protected void copy(BaseWorkerContext other) { @@ -1914,5 +1918,15 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte } } - + public TimeTracker clock() { + return clock; + } + + + public int countAllCaches() { + return codeSystems.size() + valueSets.size() + maps.size() + transforms.size() + structures.size() + measures.size() + libraries.size() + + guides.size() + capstmts.size() + searchParameters.size() + questionnaires.size() + operations.size() + plans.size() + systems.size(); + } + + } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java index 0d31f3d3f..8a0593606 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/IWorkerContext.java @@ -65,6 +65,7 @@ import org.hl7.fhir.r5.model.ValueSet.ConceptSetComponent; import org.hl7.fhir.r5.terminologies.ValueSetExpander.TerminologyServiceErrorClass; import org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome; import org.hl7.fhir.r5.utils.IResourceValidator; +import org.hl7.fhir.utilities.TimeTracker; import org.hl7.fhir.utilities.TranslationServices; import org.hl7.fhir.utilities.cache.BasePackageCacheManager; import org.hl7.fhir.utilities.cache.NpmPackage; @@ -769,4 +770,6 @@ public interface IWorkerContext { public int getClientRetryCount(); public IWorkerContext setClientRetryCount(int value); + + public TimeTracker clock(); } \ No newline at end of file diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java index d66cee01c..207d6ec29 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/SimpleWorkerContext.java @@ -78,6 +78,7 @@ import org.hl7.fhir.r5.terminologies.TerminologyClient; import org.hl7.fhir.r5.utils.IResourceValidator; import org.hl7.fhir.utilities.CSFileInputStream; import org.hl7.fhir.utilities.TextFile; +import org.hl7.fhir.utilities.TimeTracker; import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.VersionUtilities; import org.hl7.fhir.utilities.cache.BasePackageCacheManager; @@ -810,9 +811,8 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon return loadedPackages.contains(id+"#"+ver); } - - - - - + public void setClock(TimeTracker tt) { + clock = tt; + + } } \ No newline at end of file diff --git a/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TimeTracker.java b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TimeTracker.java new file mode 100644 index 000000000..0ea7c03b6 --- /dev/null +++ b/org.hl7.fhir.utilities/src/main/java/org/hl7/fhir/utilities/TimeTracker.java @@ -0,0 +1,101 @@ +package org.hl7.fhir.utilities; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import org.hl7.fhir.utilities.TimeTracker.Counter; + +public class TimeTracker { + + public class Counter { + private String name; + private int count; + private long length; + public Counter(String name) { + this.name = name; + } + } + + public class Session { + private long start = System.nanoTime(); + private String name; + public Session(String name) { + this.name = name; + } + public void end() { + endSession(this); + } + } + + private List sessions = new ArrayList<>(); + private List records = new ArrayList<>(); + private long globalStart; + private long milestone = 0; + + + public TimeTracker() { + super(); + globalStart = System.nanoTime(); + } + + public Session start(String name) { + Counter c = null; + for (Counter t : records) { + if (t.name.equals(name)) { + c = t; + } + } + if (c == null) { + c = new Counter(name); + records.add(c); + } + Session session = new Session(name); + sessions.add(session); + return session; + } + + private void endSession(Session session) { + sessions.remove(session); + Counter c = null; + for (Counter t : records) { + if (t.name.equals(session.name)) { + c = t; + } + } + c.count++; + c.length = c.length + System.nanoTime() - session.start; + } + + + public String report() { + CommaSeparatedStringBuilder b = new CommaSeparatedStringBuilder(); + for (Counter c : records) { + if (c.count == 1) { + b.append(c.name+": "+Utilities.presentDuration(c.length)); + } + } + for (Counter c : records) { + if (c.count > 1) { + b.append(c.name+": "+Utilities.presentDuration(c.length)+" (#"+c.count+")"); + } + } + return "Times: "+b.toString(); + } + + public String clock() { + return Utilities.presentDuration(System.nanoTime() - globalStart); + } + + public String instant() { + return Utilities.presentDuration(System.nanoTime() - globalStart); + } + + public String milestone() { + long start = milestone == 0 ? globalStart : milestone ; + milestone = System.nanoTime(); + return Utilities.presentDuration(milestone - start); + } + +}