Skip to content

Commit e0b1456

Browse files
committed
Add Execution Time
1 parent 4221393 commit e0b1456

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

commands/run.go

+21-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"github.com/chermehdi/egor/config"
8-
"github.com/fatih/color"
9-
"github.com/jedib0t/go-pretty/table"
107
"io/ioutil"
118
"os"
129
"os/exec"
@@ -15,6 +12,10 @@ import (
1512
"strings"
1613
"time"
1714

15+
"github.com/chermehdi/egor/config"
16+
"github.com/fatih/color"
17+
"github.com/jedib0t/go-pretty/table"
18+
1819
"github.com/urfave/cli/v2"
1920
)
2021

@@ -39,6 +40,7 @@ var (
3940
)
4041

4142
const WorkDir = "work"
43+
const TimeOutDelta float64 = 25.0
4244

4345
// Checks the output of a given testcase against it's expected output
4446
type Checker interface {
@@ -93,6 +95,7 @@ type CaseStatus struct {
9395
Status int8
9496
CheckerError error
9597
Stderr string
98+
Duration time.Duration
9699
}
97100

98101
// Implementation must be able to prepare the working environment to compile and execute testscases,
@@ -168,7 +171,7 @@ func getStderrDisplay(stderr string) string {
168171
func (c *ConsoleJudgeReport) Display() {
169172
t := table.NewWriter()
170173
t.SetOutputMirror(os.Stdout)
171-
t.AppendHeader(table.Row{"#", "Test Name", "Status", "Custom", "Additional infos", "Stderr"})
174+
t.AppendHeader(table.Row{"#", "Test Name", "Status", "Custom", "Additional infos", "Stderr", "Execution Time"})
172175
for i, stat := range c.Stats {
173176
output := "None"
174177
if stat.CheckerError != nil {
@@ -181,6 +184,7 @@ func (c *ConsoleJudgeReport) Display() {
181184
c.Descs[i].CustomCase,
182185
output,
183186
getStderrDisplay(stat.Stderr),
187+
stat.Duration
184188
})
185189
}
186190
t.SetStyle(table.StyleLight)
@@ -191,7 +195,8 @@ func newJudgeReport() JudgeReport {
191195
return &ConsoleJudgeReport{Stats: []CaseStatus{}}
192196
}
193197

194-
func timedExecution(cmd *exec.Cmd, timeOut float64) (int8, error) {
198+
func timedExecution(cmd *exec.Cmd, timeOut float64) (int8, time.Duration, error) {
199+
start := time.Now()
195200
cmd.Start()
196201
done := make(chan error)
197202
go func() { done <- cmd.Wait() }()
@@ -200,9 +205,11 @@ func timedExecution(cmd *exec.Cmd, timeOut float64) (int8, error) {
200205
select {
201206
case <-timeout:
202207
cmd.Process.Kill()
203-
return TO, nil
208+
elapsed := time.Since(start)
209+
return TO, elapsed, nil
204210
case err := <-done:
205-
return OK, err
211+
elapsed := time.Since(start)
212+
return OK, elapsed, err
206213
}
207214
}
208215

@@ -234,12 +241,13 @@ func execute(judge Judge, desc CaseDescription, command string, args ...string)
234241
cmd.Stdout = outputFile
235242
cmd.Stderr = &stderrBuffer
236243

237-
status, err := timedExecution(cmd, desc.TimeLimit)
244+
status, duration, err := timedExecution(cmd, desc.TimeLimit+TimeOutDelta)
238245
if status == TO {
239246
return CaseStatus{
240247
Status: TL,
241248
CheckerError: nil,
242249
Stderr: stderrBuffer.String(),
250+
Duration: duration,
243251
}, nil
244252
}
245253

@@ -248,6 +256,7 @@ func execute(judge Judge, desc CaseDescription, command string, args ...string)
248256
Status: RE,
249257
CheckerError: nil,
250258
Stderr: stderrBuffer.String(),
259+
Duration: duration,
251260
}, err
252261
}
253262

@@ -256,6 +265,7 @@ func execute(judge Judge, desc CaseDescription, command string, args ...string)
256265
return CaseStatus{
257266
Status: RE,
258267
CheckerError: nil,
268+
Duration: duration,
259269
}, err
260270
}
261271
output, err := ioutil.ReadFile(desc.WorkFile)
@@ -264,6 +274,7 @@ func execute(judge Judge, desc CaseDescription, command string, args ...string)
264274
return CaseStatus{
265275
Status: RE,
266276
CheckerError: nil,
277+
Duration: duration,
267278
}, err
268279
}
269280
err = judge.Checker().Check(string(output), string(expectedOutput))
@@ -272,12 +283,14 @@ func execute(judge Judge, desc CaseDescription, command string, args ...string)
272283
Status: WA,
273284
CheckerError: err,
274285
Stderr: stderrBuffer.String(),
286+
Duration: duration,
275287
}, err
276288
}
277289
return CaseStatus{
278290
Status: AC,
279291
CheckerError: nil,
280292
Stderr: stderrBuffer.String(),
293+
Duration: duration,
281294
}, err
282295
}
283296

commands/run_test.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
package commands
22

33
import (
4-
"github.com/stretchr/testify/assert"
54
"os/exec"
65
"testing"
6+
7+
"github.com/stretchr/testify/assert"
78
)
89

910
func TestTimedOutExecution(t *testing.T) {
10-
cmd := exec.Command("sleep", "1")
11-
status, err := timedExecution(cmd, 999.99)
11+
cmd := exec.Command("sleep", "1.1")
12+
status, _, err := timedExecution(cmd, 1000+TimeOutDelta)
1213
assert.Equal(t, err, nil)
1314
assert.Equal(t, status, TO)
1415
}
1516

1617
func TestTimedExecution(t *testing.T) {
1718
cmd := exec.Command("sleep", "1")
18-
status, err := timedExecution(cmd, 1250)
19+
status, _, err := timedExecution(cmd, 1000+TimeOutDelta)
1920
assert.Equal(t, err, nil)
2021
assert.Equal(t, status, OK)
2122
}

0 commit comments

Comments
 (0)