From 1c74de73214379a1a263cb1b319efa36c7ef1e69 Mon Sep 17 00:00:00 2001 From: Lars Langhans Date: Mon, 13 Sep 2010 09:33:05 +0200 Subject: perftest12:#163613# Performancetest cleanups --- qadevOOo/runner/helper/ProcessHandler.java | 210 +++++++++++++++++++++++++++-- 1 file changed, 198 insertions(+), 12 deletions(-) (limited to 'qadevOOo/runner/helper/ProcessHandler.java') diff --git a/qadevOOo/runner/helper/ProcessHandler.java b/qadevOOo/runner/helper/ProcessHandler.java index c414accd44ac..8e37a58a370e 100644 --- a/qadevOOo/runner/helper/ProcessHandler.java +++ b/qadevOOo/runner/helper/ProcessHandler.java @@ -26,6 +26,7 @@ ************************************************************************/ package helper; +import java.io.BufferedReader; import java.io.InputStream; import java.io.File; import java.io.PrintWriter; @@ -33,10 +34,12 @@ import java.io.PrintStream; import java.io.LineNumberReader; import java.io.InputStreamReader; import java.io.OutputStreamWriter; +import java.io.Writer; import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; import lib.TestParameters; +import share.LogWriter; import util.PropertyName; import util.utils; @@ -58,6 +61,7 @@ class Pump extends Thread private String pref; private StringBuffer buf = new StringBuffer(256); private PrintWriter log; + private boolean bOutput; /** * Creates Pump for specified InputStream. @@ -70,11 +74,12 @@ class Pump extends Thread * @param outPrefix A prefix which is printed at the * beginning of each output line. */ - public Pump(InputStream is, PrintWriter log, String outPrefix) + public Pump(InputStream is, PrintWriter log, String outPrefix, boolean _bOutput) { this.pref = (outPrefix == null) ? "" : outPrefix; reader = new LineNumberReader(new InputStreamReader(is)); this.log = log; + this.bOutput = _bOutput; start(); } @@ -85,8 +90,11 @@ class Pump extends Thread String line = reader.readLine(); while (line != null) { - log.println(pref + line); - log.flush(); + if (bOutput) + { + log.println(pref + line); + log.flush(); + } buf.append(line).append('\n'); line = reader.readLine(); } @@ -133,6 +141,11 @@ public class ProcessHandler private Process m_aProcess = null; private TestParameters param = null; private boolean debug = false; + private boolean bUseOutput = true; + + private int m_nProcessTimeout = 0; + private String m_sProcessKiller; + private ProcessWatcher m_aWatcher; /** * Creates instance with specified external command. @@ -347,6 +360,24 @@ public class ProcessHandler } + /** + * If not equal 0, the time to maximal wait. + * @param _n + */ + public void setProcessTimeout(int _n) + { + m_nProcessTimeout = _n; + } + + /** + * This command will call after ProcessTimeout is arrived. + * @param _s + */ + public void setProcessKiller(String _s) + { + m_sProcessKiller = _s; + } + /** * This method do an asynchronous execution of the commands. To avoid a interruption on long running processes * caused by OfficeWatcher, the OfficeWatcher get frequently a ping. @@ -395,7 +426,7 @@ public class ProcessHandler if (sOutputText.length() == memText.length()) { changedText = false; - // dbg("runCommand Could not detect changes in output stream!!!"); + // dbg("runCommand Could not detect changes in output stream!!!"); } hangcheck = 10; memText = this.getOutputText(); @@ -515,6 +546,21 @@ public class ProcessHandler return m_nExactStartTimeInMillisec; } + private void showEnvVars() + { + if (envVars != null) + { + for (int i = 0; i < envVars.length; i++) + { + log.println("env: " + envVars[i]); + } + } + else + { + log.println("env: null"); + } + } + protected void execute() { if (isStarted()) @@ -527,27 +573,32 @@ public class ProcessHandler { if (cmdLine == null) { - log.print(utils.getDateTime() + "execute: Starting command from array: "); + log.println(utils.getDateTime() + "execute: Starting command from array: "); for (int i = 0; i < cmdLineArray.length; i++) { - log.print(cmdLineArray[i]); - log.print(" "); + log.println(cmdLineArray[i]); + // log.print(" "); } + showEnvVars(); log.println(""); initialExactStartTime(); + initializeProcessKiller(); m_aProcess = runtime.exec(cmdLineArray, envVars); } else { if (workDir != null) { - log.println(utils.getDateTime() + "execute: Starting command: " + cmdLine + " " + - workDir.getAbsolutePath()); + log.println(utils.getDateTime() + "execute: Starting command: "); + log.println(cmdLine + " path=" + workDir.getAbsolutePath()); + showEnvVars(); m_aProcess = runtime.exec(cmdLine, envVars, workDir); } else { - log.println(utils.getDateTime() + "execute: Starting command: " + cmdLine); + log.println(utils.getDateTime() + "execute: Starting command: "); + log.println(cmdLine); + showEnvVars(); m_aProcess = runtime.exec(cmdLine, envVars); } } @@ -566,8 +617,8 @@ public class ProcessHandler return; } dbg("execute: pump io-streams"); - stdout = new Pump(m_aProcess.getInputStream(), log, "out > "); - stderr = new Pump(m_aProcess.getErrorStream(), log, "err > "); + stdout = new Pump(m_aProcess.getInputStream(), log, "out > ", bUseOutput); + stderr = new Pump(m_aProcess.getErrorStream(), log, "err > ", bUseOutput); stdIn = new PrintStream(m_aProcess.getOutputStream()); // int nExitValue = m_aProcess.exitValue(); @@ -821,4 +872,139 @@ public class ProcessHandler log.println(utils.getDateTime() + "PH." + message); } } + + public void noOutput() + { + bUseOutput = false; + } + // ------------------------------------------------------------------------- + class ProcessWatcher extends Thread + { + + private int m_nTimeoutInSec; + private String m_sProcessToStart; + private boolean m_bInterrupt; + + public ProcessWatcher(int _nTimeOut, String _sProcess) + { + m_nTimeoutInSec = _nTimeOut; + m_sProcessToStart = _sProcess; + m_bInterrupt = false; + } + + /** + * returns true, if the thread should hold on + * @return + */ + public synchronized boolean isInHoldOn() + { + return m_bInterrupt; + } + /** + * Marks the thread to hold on, next time + * STUPID: The thread must poll this flag itself. + * + * Reason: interrupt() seems not to work as expected. + */ + public synchronized void holdOn() + { + m_bInterrupt = true; + interrupt(); + } + + public void run() + { + while (m_nTimeoutInSec > 0) + { + m_nTimeoutInSec--; + try + { + sleep(1000); + } + catch(java.lang.InterruptedException e) + { + // interrupt flag is set back to 'not interrupted' :-( + } + if (isInHoldOn()) + { + break; + } + } + if (m_nTimeoutInSec <= 0 && !isInHoldOn()) // not zero, so we are interrupted. + { + system(m_sProcessToStart); + } + } + + /** + * Start an external Process + * @param _sProcess + */ + private void system(String _sProcess) + { + if (_sProcess == null) + { + return; + } + + try + { + + // run a _sProcess command + // using the Runtime exec method: + Process p = Runtime.getRuntime().exec(_sProcess); + + BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); + + BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); + + // read the output from the command + String s; + while ((s = stdInput.readLine()) != null) + { + System.out.println("out:" + s); + } + + // read any errors from the attempted command + while ((s = stdError.readLine()) != null) + { + System.out.println("err:" + s); + } + + } + catch (java.io.IOException e) + { + System.out.println("exception caught: "); + e.printStackTrace(); + } + + } + } + + /** + * If the timeout only given by setProcessTimeout(int seconds) function is != 0, + * a extra thread is created and after time has run out, the ProcessKiller string + * given by function setProcessKiller(string) will execute. + * So it is possible to kill a running office after a given time of seconds. + */ + private void initializeProcessKiller() + { + if (m_nProcessTimeout != 0) + { + m_aWatcher = new ProcessWatcher(m_nProcessTimeout, m_sProcessKiller); + m_aWatcher.start(); + } + } + + /** + * to stop the extra thread, before he will kill a running office. This will stop the thread. + */ + public void stopWatcher() + { + if (m_aWatcher != null) + { + m_aWatcher.holdOn(); + shortWait(5000); + } + } } -- cgit