From a9f7cdd0e632d094dae4aa1b2efac39dd40e9a59 Mon Sep 17 00:00:00 2001 From: "royqh1979@gmail.com" Date: Fri, 28 Jan 2022 08:21:56 +0800 Subject: [PATCH] add timeout for flushing output buffer when running problem cases --- RedPandaIDE/compiler/executablerunner.cpp | 3 +- RedPandaIDE/compiler/ojproblemcasesrunner.cpp | 54 ++++++++++++++++--- RedPandaIDE/compiler/ojproblemcasesrunner.h | 15 +++++- RedPandaIDE/compiler/runner.cpp | 13 ++++- RedPandaIDE/compiler/runner.h | 4 ++ 5 files changed, 80 insertions(+), 9 deletions(-) diff --git a/RedPandaIDE/compiler/executablerunner.cpp b/RedPandaIDE/compiler/executablerunner.cpp index 0cd87a03..02d9d07c 100644 --- a/RedPandaIDE/compiler/executablerunner.cpp +++ b/RedPandaIDE/compiler/executablerunner.cpp @@ -38,6 +38,7 @@ ExecutableRunner::ExecutableRunner(const QString &filename, const QString &argum mStartConsole(false), mQuitSemaphore(0) { + setWaitForFinishTime(1000); } bool ExecutableRunner::startConsole() const @@ -170,7 +171,7 @@ void ExecutableRunner::run() } while (true) { - mProcess->waitForFinished(1000); + mProcess->waitForFinished(mWaitForFinishTime); if (mProcess->state()!=QProcess::Running) { break; } diff --git a/RedPandaIDE/compiler/ojproblemcasesrunner.cpp b/RedPandaIDE/compiler/ojproblemcasesrunner.cpp index e2ccf6e2..a74266aa 100644 --- a/RedPandaIDE/compiler/ojproblemcasesrunner.cpp +++ b/RedPandaIDE/compiler/ojproblemcasesrunner.cpp @@ -27,6 +27,9 @@ OJProblemCasesRunner::OJProblemCasesRunner(const QString& filename, const QStrin Runner(filename,arguments,workDir,parent) { mProblemCases = problemCases; + mBufferSize = 8192; + mOutputRefreshTime = 1000; + setWaitForFinishTime(100); } OJProblemCasesRunner::OJProblemCasesRunner(const QString& filename, const QString& arguments, const QString& workDir, @@ -34,6 +37,9 @@ OJProblemCasesRunner::OJProblemCasesRunner(const QString& filename, const QStrin Runner(filename,arguments,workDir,parent) { mProblemCases.append(problemCase); + mBufferSize = 8192; + mOutputRefreshTime = 1000; + setWaitForFinishTime(100); } void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase) @@ -80,9 +86,10 @@ void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase) QByteArray readed; QByteArray buffer; QByteArray output; + int noOutputTime = 0; while (true) { - process.waitForFinished(100); - readed = process.read(5001); + process.waitForFinished(mWaitForFinishTime); + readed = process.read(mBufferSize); buffer += readed; if (process.state()!=QProcess::Running) { break; @@ -97,10 +104,15 @@ void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase) } if (errorOccurred) break; - if (buffer.length()>5000) { - emit newOutputGetted(problemCase->getId(),QString::fromLocal8Bit(buffer)); - output.append(buffer); - buffer.clear(); + if (buffer.length()>=mBufferSize || noOutputTime > mOutputRefreshTime) { + if (!buffer.isEmpty()) { + emit newOutputGetted(problemCase->getId(),QString::fromLocal8Bit(buffer)); + output.append(buffer); + buffer.clear(); + } + noOutputTime = 0; + } else { + noOutputTime += mWaitForFinishTime; } } if (process.state() == QProcess::ProcessState::NotRunning) @@ -146,4 +158,34 @@ void OJProblemCasesRunner::run() } } +int OJProblemCasesRunner::waitForFinishTime() const +{ + return mWaitForFinishTime; +} + +void OJProblemCasesRunner::setWaitForFinishTime(int newWaitForFinishTime) +{ + mWaitForFinishTime = newWaitForFinishTime; +} + +int OJProblemCasesRunner::outputRefreshTime() const +{ + return mOutputRefreshTime; +} + +void OJProblemCasesRunner::setOutputRefreshTime(int newOutputRefreshTime) +{ + mOutputRefreshTime = newOutputRefreshTime; +} + +int OJProblemCasesRunner::bufferSize() const +{ + return mBufferSize; +} + +void OJProblemCasesRunner::setBufferSize(int newBufferSize) +{ + mBufferSize = newBufferSize; +} + diff --git a/RedPandaIDE/compiler/ojproblemcasesrunner.h b/RedPandaIDE/compiler/ojproblemcasesrunner.h index c4d127b3..0a91cd20 100644 --- a/RedPandaIDE/compiler/ojproblemcasesrunner.h +++ b/RedPandaIDE/compiler/ojproblemcasesrunner.h @@ -29,6 +29,17 @@ public: const QVector& problemCases, QObject *parent = nullptr); explicit OJProblemCasesRunner(const QString& filename, const QString& arguments, const QString& workDir, POJProblemCase problemCase, QObject *parent = nullptr); + //max size of output buffer + int bufferSize() const; + void setBufferSize(int newBufferSize); + + //max time (in milliseconds) waiting to flush output buffer + int outputRefreshTime() const; + void setOutputRefreshTime(int newOutputRefreshTime); + + int waitForFinishTime() const; + void setWaitForFinishTime(int newWaitForFinishTime); + signals: void caseStarted(const QString& id, int current, int total); void caseFinished(const QString& id, int current, int total); @@ -41,7 +52,9 @@ private: // QThread interface protected: void run() override; - +private: + int mBufferSize; + int mOutputRefreshTime; }; #endif // OJPROBLEMCASESRUNNER_H diff --git a/RedPandaIDE/compiler/runner.cpp b/RedPandaIDE/compiler/runner.cpp index bbb60cdf..3771a923 100644 --- a/RedPandaIDE/compiler/runner.cpp +++ b/RedPandaIDE/compiler/runner.cpp @@ -23,7 +23,8 @@ Runner::Runner(const QString &filename, const QString &arguments, const QString mStop(false), mFilename(filename), mArguments(arguments), - mWorkDir(workDir) + mWorkDir(workDir), + mWaitForFinishTime(1000) { } @@ -49,3 +50,13 @@ void Runner::setPausing(bool newCanFinish) mPausing = newCanFinish; } +int Runner::waitForFinishTime() const +{ + return mWaitForFinishTime; +} + +void Runner::setWaitForFinishTime(int newWaitForFinishTime) +{ + mWaitForFinishTime = newWaitForFinishTime; +} + diff --git a/RedPandaIDE/compiler/runner.h b/RedPandaIDE/compiler/runner.h index d6c7a8a1..9ef0b026 100644 --- a/RedPandaIDE/compiler/runner.h +++ b/RedPandaIDE/compiler/runner.h @@ -27,6 +27,9 @@ public: bool pausing() const; + // time (in milliseconds) waiting for process finished in each processing loop + int waitForFinishTime() const; + void setWaitForFinishTime(int newWaitForFinishTime); signals: void started(); @@ -45,6 +48,7 @@ protected: QString mFilename; QString mArguments; QString mWorkDir; + int mWaitForFinishTime; }; #endif // RUNNER_H