Add time tracking for progress reporting

This commit is contained in:
Grahame Grieve 2020-08-31 13:21:28 +10:00
parent 5f730916d4
commit 42a7dad39a
4 changed files with 125 additions and 7 deletions

View File

@ -109,6 +109,7 @@ import org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
import org.hl7.fhir.r5.terminologies.ValueSetExpanderSimple; import org.hl7.fhir.r5.terminologies.ValueSetExpanderSimple;
import org.hl7.fhir.r5.utils.ToolingExtensions; import org.hl7.fhir.r5.utils.ToolingExtensions;
import org.hl7.fhir.utilities.OIDUtils; import org.hl7.fhir.utilities.OIDUtils;
import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.utilities.ToolingClientLogger; import org.hl7.fhir.utilities.ToolingClientLogger;
import org.hl7.fhir.utilities.TranslationServices; import org.hl7.fhir.utilities.TranslationServices;
import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities;
@ -200,17 +201,19 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
protected Parameters expParameters; protected Parameters expParameters;
private TranslationServices translator = new NullTranslator(); private TranslationServices translator = new NullTranslator();
protected TerminologyCache txCache; protected TerminologyCache txCache;
protected TimeTracker clock;
private boolean tlogging = true; private boolean tlogging = true;
public BaseWorkerContext() throws FileNotFoundException, IOException, FHIRException { public BaseWorkerContext() throws FileNotFoundException, IOException, FHIRException {
txCache = new TerminologyCache(lock, null); txCache = new TerminologyCache(lock, null);
setValidationMessageLanguage(getLocale()); setValidationMessageLanguage(getLocale());
clock = new TimeTracker();
} }
public BaseWorkerContext(Locale locale) throws FileNotFoundException, IOException, FHIRException { public BaseWorkerContext(Locale locale) throws FileNotFoundException, IOException, FHIRException {
txCache = new TerminologyCache(lock, null); txCache = new TerminologyCache(lock, null);
setValidationMessageLanguage(locale); setValidationMessageLanguage(locale);
clock = new TimeTracker();
} }
public BaseWorkerContext(CanonicalResourceManager<CodeSystem> codeSystems, CanonicalResourceManager<ValueSet> valueSets, CanonicalResourceManager<ConceptMap> maps, CanonicalResourceManager<StructureDefinition> profiles, public BaseWorkerContext(CanonicalResourceManager<CodeSystem> codeSystems, CanonicalResourceManager<ValueSet> valueSets, CanonicalResourceManager<ConceptMap> maps, CanonicalResourceManager<StructureDefinition> profiles,
@ -221,6 +224,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
this.maps = maps; this.maps = maps;
this.structures = profiles; this.structures = profiles;
this.guides = guides; this.guides = guides;
clock = new TimeTracker();
} }
protected void copy(BaseWorkerContext other) { 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();
}
} }

View File

@ -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.TerminologyServiceErrorClass;
import org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome; import org.hl7.fhir.r5.terminologies.ValueSetExpander.ValueSetExpansionOutcome;
import org.hl7.fhir.r5.utils.IResourceValidator; import org.hl7.fhir.r5.utils.IResourceValidator;
import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.utilities.TranslationServices; import org.hl7.fhir.utilities.TranslationServices;
import org.hl7.fhir.utilities.cache.BasePackageCacheManager; import org.hl7.fhir.utilities.cache.BasePackageCacheManager;
import org.hl7.fhir.utilities.cache.NpmPackage; import org.hl7.fhir.utilities.cache.NpmPackage;
@ -769,4 +770,6 @@ public interface IWorkerContext {
public int getClientRetryCount(); public int getClientRetryCount();
public IWorkerContext setClientRetryCount(int value); public IWorkerContext setClientRetryCount(int value);
public TimeTracker clock();
} }

View File

@ -78,6 +78,7 @@ import org.hl7.fhir.r5.terminologies.TerminologyClient;
import org.hl7.fhir.r5.utils.IResourceValidator; import org.hl7.fhir.r5.utils.IResourceValidator;
import org.hl7.fhir.utilities.CSFileInputStream; import org.hl7.fhir.utilities.CSFileInputStream;
import org.hl7.fhir.utilities.TextFile; import org.hl7.fhir.utilities.TextFile;
import org.hl7.fhir.utilities.TimeTracker;
import org.hl7.fhir.utilities.Utilities; import org.hl7.fhir.utilities.Utilities;
import org.hl7.fhir.utilities.VersionUtilities; import org.hl7.fhir.utilities.VersionUtilities;
import org.hl7.fhir.utilities.cache.BasePackageCacheManager; import org.hl7.fhir.utilities.cache.BasePackageCacheManager;
@ -810,9 +811,8 @@ public class SimpleWorkerContext extends BaseWorkerContext implements IWorkerCon
return loadedPackages.contains(id+"#"+ver); return loadedPackages.contains(id+"#"+ver);
} }
public void setClock(TimeTracker tt) {
clock = tt;
}
} }

View File

@ -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<Session> sessions = new ArrayList<>();
private List<Counter> 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);
}
}