Skip to content

Commit

Permalink
Improve perf reusing PowerShell session and avoiding multi-file creation
Browse files Browse the repository at this point in the history
  • Loading branch information
profesorfalken committed Oct 8, 2018
1 parent 0592049 commit 6f0e6f4
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 46 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.profesorfalken</groupId>
<artifactId>jSensors</artifactId>
<version>2.1.1</version>
<version>2.2-SNAPSHOT</version>
<packaging>jar</packaging>

<name>jSensors</name>
Expand Down Expand Up @@ -168,7 +168,7 @@
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>jPowerShell</artifactId>
<version>2.0</version>
<version>3.0</version>
</dependency>
</dependencies>
<properties>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ public class WindowsSensorsManager extends SensorsManager {

@Override
public String getSensorsData() {
String rawSensorsData = PowerShellOperations.getRawSensorsData();

String rawSensorsData = PowerShellOperations.GET.getRawSensorsData();

if (debugMode) {
LOGGER.info("RawSensorData: " + rawSensorsData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,22 @@
*
* @author Javier Garcia Alonso
*/
public class PowerShellOperations {
public enum PowerShellOperations {
GET;

private static final Logger LOGGER = LoggerFactory.getLogger(PowerShellOperations.class);

private PowerShell powerShell = null;
private boolean initialized;

// Hides constructor
private PowerShellOperations() {
PowerShellOperations() {
String rawData = null;
this.powerShell = PowerShell.openSession();
}

public boolean isInitialized() {
return this.initialized;
}

public static boolean isAdministrator() {
Expand All @@ -38,8 +48,8 @@ public static boolean isAdministrator() {
return "true".equalsIgnoreCase(PowerShell.executeSingleCommand(command).getCommandOutput());
}

public static String getRawSensorsData() {
PowerShell powershell = null;
public String getRawSensorsData() {
/*PowerShell powershell = null;
String rawData = null;
try {
powershell = PowerShell.openSession();
Expand All @@ -50,7 +60,7 @@ public static String getRawSensorsData() {
if (powershell != null) {
powershell.close();
}
}
return rawData;
}*/
return this.powerShell.executeScript(PowerShellScriptHelper.generateScript()).getCommandOutput();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class PowerShellScriptHelper {

private static final String LINE_BREAK = "\r\n";

private static File tmpFile = null;

// Hides constructor
private PowerShellScriptHelper() {
}
Expand Down Expand Up @@ -93,34 +95,32 @@ private static String sensorsQueryLoop() {
}

static String generateScript() {
File tmpFile = null;
FileWriter writer = null;
String scriptPath = null;

try {
tmpFile = File.createTempFile("jsensors_" + new Date().getTime(), ".ps1");
tmpFile.deleteOnExit();
writer = new FileWriter(tmpFile);
writer.write(getPowerShellScript());
writer.flush();
writer.close();
} catch (Exception ex) {
LOGGER.error("Cannot create PowerShell script file", ex);
return "Error";
} finally {
if (tmpFile == null) {
try {
if (writer != null) {
writer.close();
tmpFile = File.createTempFile("jsensors_" + new Date().getTime(), ".ps1");
tmpFile.deleteOnExit();
writer = new FileWriter(tmpFile);
writer.write(getPowerShellScript());
writer.flush();
writer.close();
} catch (Exception ex) {
LOGGER.error("Cannot create PowerShell script file", ex);
return "Error";
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException ioe) {
LOGGER.warn("Error when finish writing Powershell script file", ioe);
}
} catch (IOException ioe) {
LOGGER.warn("Error when finish writing Powershell script file", ioe);
}
}
if (tmpFile != null) {
scriptPath = tmpFile.getAbsolutePath();
}

return scriptPath;
return tmpFile.getAbsolutePath();
}

private static String getPowerShellScript() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public GuiUpdater(JSensorsGUI jSensorsGUI) {
protected Void doInBackground() throws Exception {
while (true) {
table.setModel(calculateModel());
Thread.sleep(2000);
//Thread.sleep(2000);
}
}

Expand Down
36 changes: 20 additions & 16 deletions src/main/java/com/profesorfalken/jsensors/util/SensorsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
public class SensorsUtils {
private static final Logger LOGGER = LoggerFactory.getLogger(PowerShellOperations.class);

private static File tempFile = null;

// Hides constructor
private SensorsUtils() {

Expand All @@ -40,22 +42,24 @@ public static String generateLibTmpPath(String libName) {
}

public static String generateLibTmpPath(String path, String libName) {
InputStream in = SensorsUtils.class.getResourceAsStream(path + libName);
File tempFile;
try {
tempFile = File.createTempFile(libName, "");
byte[] buffer = new byte[1024];
int read;
FileOutputStream fos = new FileOutputStream(tempFile);
while ((read = in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
in.close();
} catch (IOException ex) {
LOGGER.error("Cannot generate temporary file", ex);
return "";
}
if (tempFile == null) {
InputStream in = SensorsUtils.class.getResourceAsStream(path + libName);
try {
tempFile = File.createTempFile(libName, "");
tempFile.deleteOnExit();
byte[] buffer = new byte[1024];
int read;
FileOutputStream fos = new FileOutputStream(tempFile);
while ((read = in.read(buffer)) != -1) {
fos.write(buffer, 0, read);
}
fos.close();
in.close();
} catch (IOException ex) {
LOGGER.error("Cannot generate temporary file", ex);
return "";
}
}

return tempFile.getAbsolutePath();
}
Expand Down

0 comments on commit 6f0e6f4

Please sign in to comment.