
de.mcs.jmeasurement.MeasureTask Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of JMeasurement Show documentation
Show all versions of JMeasurement Show documentation
JMeasurement profiling programs in production enviroment
The newest version!
package de.mcs.jmeasurement;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.TimerTask;
import java.util.stream.Collectors;
import org.xml.sax.SAXException;
/**
* This class is the backgorund processing class for the Measure factory.
*
* @author w.klaas
* @since 0.68
*/
public class MeasureTask extends TimerTask {
/** make automatic snapshots. */
private boolean autoSnapshot;
/** save memory, store measurepoints and snapshots into the filesystem. */
private boolean memorySavings;
/** where to save the data. */
private File workingPath;
/** idle time of a point, before it will be saved. */
private long idleTime;
/** needed for date processing. */
private SimpleDateFormat dformat = new SimpleDateFormat("yyyyMMddHHmmss");
/**
* Constructor of this background task. See options {@link MeasureFactory#setOption(String, String)}
*
* @param aOptions
* options to set.
*/
public MeasureTask(final JMConfig aOptions) {
autoSnapshot = aOptions.getBoolean(JMConfig.OPTION_ENABLE_AUTOSNAPSHOT);
memorySavings = aOptions.getBoolean(JMConfig.OPTION_ENABLE_MEMORY_SAVINGS);
File sTempPath = new File(System.getProperty("java.io.tmpdir"));
String workingPathString = aOptions.getProperty(JMConfig.OPTION_WORKINGPATH, sTempPath.getAbsolutePath());
if (workingPathString.indexOf("%TEMP%") >= 0) {
workingPathString = workingPathString.substring(0, workingPathString.indexOf("%TEMP%")) + sTempPath
+ workingPathString.substring(workingPathString.indexOf("%TEMP%") + "%TEMP%".length());
}
workingPath = new File(workingPathString);
idleTime = aOptions.getLong(JMConfig.OPTION_POINT_IDLETIME);
}
/**
* processing.
*/
@Override
public void run() {
if (autoSnapshot) {
makeSnapShot();
}
if (memorySavings) {
saveMemory();
}
}
/**
* try to save memory by saving all availble snapshots and all no longer needed Measurepoints to filesystem.
*/
private void saveMemory() {
saveAllSnapShots();
saveUnusedMeasurePoints();
}
/**
*
*/
private void saveUnusedMeasurePoints() {
Collection measurePoints = MeasureFactory.getMeasurePoints();
List pointsToSave = null;
synchronized (measurePoints) {
pointsToSave = measurePoints.stream().filter(point -> {
try {
Date lastActivation = point.getData(DefaultMeasurePoint.DATA_KEY_LAST_ACTIVATION).getAsDate();
return (!point.hasActiveMonitors() && (lastActivation.before(new Date(new Date().getTime() - idleTime))));
} catch (Exception e) {
e.printStackTrace();
}
return false;
}).collect(Collectors.toList());
}
if (pointsToSave != null) {
pointsToSave.forEach(point -> MeasureFactory.saveMeasurePointandRemove(point, workingPath));
}
}
/**
*
*/
private void saveAllSnapShots() {
String[] snapShotNames = MeasureFactory.getSnapShotNames();
if (snapShotNames != null) {
for (String snapshotName : snapShotNames) {
SnapShot snapshot = MeasureFactory.getSnapShot(snapshotName);
try {
MeasureFactory.saveSnapShot(snapshot, new File(workingPath, snapshot.getName() + ".xml"));
MeasureFactory.removeSnapShot(snapshotName);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* take a snapshot.
*/
private void makeSnapShot() {
String snapShotName = "snapshot_" + dformat.format(new Date());
MeasureFactory.takeSnapshot(snapShotName);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy