add timeout for flushing output buffer when running problem cases

This commit is contained in:
royqh1979@gmail.com 2022-01-28 08:21:56 +08:00
parent c45e0db7c7
commit a9f7cdd0e6
5 changed files with 80 additions and 9 deletions

View File

@ -38,6 +38,7 @@ ExecutableRunner::ExecutableRunner(const QString &filename, const QString &argum
mStartConsole(false), mStartConsole(false),
mQuitSemaphore(0) mQuitSemaphore(0)
{ {
setWaitForFinishTime(1000);
} }
bool ExecutableRunner::startConsole() const bool ExecutableRunner::startConsole() const
@ -170,7 +171,7 @@ void ExecutableRunner::run()
} }
while (true) { while (true) {
mProcess->waitForFinished(1000); mProcess->waitForFinished(mWaitForFinishTime);
if (mProcess->state()!=QProcess::Running) { if (mProcess->state()!=QProcess::Running) {
break; break;
} }

View File

@ -27,6 +27,9 @@ OJProblemCasesRunner::OJProblemCasesRunner(const QString& filename, const QStrin
Runner(filename,arguments,workDir,parent) Runner(filename,arguments,workDir,parent)
{ {
mProblemCases = problemCases; mProblemCases = problemCases;
mBufferSize = 8192;
mOutputRefreshTime = 1000;
setWaitForFinishTime(100);
} }
OJProblemCasesRunner::OJProblemCasesRunner(const QString& filename, const QString& arguments, const QString& workDir, 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) Runner(filename,arguments,workDir,parent)
{ {
mProblemCases.append(problemCase); mProblemCases.append(problemCase);
mBufferSize = 8192;
mOutputRefreshTime = 1000;
setWaitForFinishTime(100);
} }
void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase) void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase)
@ -80,9 +86,10 @@ void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase)
QByteArray readed; QByteArray readed;
QByteArray buffer; QByteArray buffer;
QByteArray output; QByteArray output;
int noOutputTime = 0;
while (true) { while (true) {
process.waitForFinished(100); process.waitForFinished(mWaitForFinishTime);
readed = process.read(5001); readed = process.read(mBufferSize);
buffer += readed; buffer += readed;
if (process.state()!=QProcess::Running) { if (process.state()!=QProcess::Running) {
break; break;
@ -97,11 +104,16 @@ void OJProblemCasesRunner::runCase(int index,POJProblemCase problemCase)
} }
if (errorOccurred) if (errorOccurred)
break; break;
if (buffer.length()>5000) { if (buffer.length()>=mBufferSize || noOutputTime > mOutputRefreshTime) {
if (!buffer.isEmpty()) {
emit newOutputGetted(problemCase->getId(),QString::fromLocal8Bit(buffer)); emit newOutputGetted(problemCase->getId(),QString::fromLocal8Bit(buffer));
output.append(buffer); output.append(buffer);
buffer.clear(); buffer.clear();
} }
noOutputTime = 0;
} else {
noOutputTime += mWaitForFinishTime;
}
} }
if (process.state() == QProcess::ProcessState::NotRunning) if (process.state() == QProcess::ProcessState::NotRunning)
buffer += process.readAll(); buffer += process.readAll();
@ -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;
}

View File

@ -29,6 +29,17 @@ public:
const QVector<POJProblemCase>& problemCases, QObject *parent = nullptr); const QVector<POJProblemCase>& problemCases, QObject *parent = nullptr);
explicit OJProblemCasesRunner(const QString& filename, const QString& arguments, const QString& workDir, explicit OJProblemCasesRunner(const QString& filename, const QString& arguments, const QString& workDir,
POJProblemCase problemCase, QObject *parent = nullptr); 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: signals:
void caseStarted(const QString& id, int current, int total); void caseStarted(const QString& id, int current, int total);
void caseFinished(const QString& id, int current, int total); void caseFinished(const QString& id, int current, int total);
@ -41,7 +52,9 @@ private:
// QThread interface // QThread interface
protected: protected:
void run() override; void run() override;
private:
int mBufferSize;
int mOutputRefreshTime;
}; };
#endif // OJPROBLEMCASESRUNNER_H #endif // OJPROBLEMCASESRUNNER_H

View File

@ -23,7 +23,8 @@ Runner::Runner(const QString &filename, const QString &arguments, const QString
mStop(false), mStop(false),
mFilename(filename), mFilename(filename),
mArguments(arguments), mArguments(arguments),
mWorkDir(workDir) mWorkDir(workDir),
mWaitForFinishTime(1000)
{ {
} }
@ -49,3 +50,13 @@ void Runner::setPausing(bool newCanFinish)
mPausing = newCanFinish; mPausing = newCanFinish;
} }
int Runner::waitForFinishTime() const
{
return mWaitForFinishTime;
}
void Runner::setWaitForFinishTime(int newWaitForFinishTime)
{
mWaitForFinishTime = newWaitForFinishTime;
}

View File

@ -27,6 +27,9 @@ public:
bool pausing() const; bool pausing() const;
// time (in milliseconds) waiting for process finished in each processing loop
int waitForFinishTime() const;
void setWaitForFinishTime(int newWaitForFinishTime);
signals: signals:
void started(); void started();
@ -45,6 +48,7 @@ protected:
QString mFilename; QString mFilename;
QString mArguments; QString mArguments;
QString mWorkDir; QString mWorkDir;
int mWaitForFinishTime;
}; };
#endif // RUNNER_H #endif // RUNNER_H