Skip to content

Commit

Permalink
Add Execution Time
Browse files Browse the repository at this point in the history
  • Loading branch information
Eroui committed Apr 4, 2020
1 parent eba8552 commit a9d6e7e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
26 changes: 21 additions & 5 deletions commands/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ import (
"strings"
"time"

"github.com/chermehdi/egor/config"
"github.com/fatih/color"
"github.com/jedib0t/go-pretty/table"

"github.com/urfave/cli/v2"
)

Expand All @@ -40,6 +44,7 @@ var (
)

const WorkDir = "work"
const TimeOutDelta float64 = 25.0

// Checks the output of a given testcase against it's expected output
type Checker interface {
Expand Down Expand Up @@ -94,6 +99,7 @@ type CaseStatus struct {
Status int8
CheckerError error
Stderr string
Duration time.Duration
}

// Implementation must be able to prepare the working environment to compile and execute testscases,
Expand Down Expand Up @@ -169,7 +175,7 @@ func getStderrDisplay(stderr string) string {
func (c *ConsoleJudgeReport) Display() {
t := table.NewWriter()
t.SetOutputMirror(os.Stdout)
t.AppendHeader(table.Row{"#", "Test Name", "Status", "Custom", "Additional infos", "Stderr"})
t.AppendHeader(table.Row{"#", "Test Name", "Status", "Custom", "Additional infos", "Stderr", "Execution Time"})
for i, stat := range c.Stats {
output := "None"
if stat.CheckerError != nil {
Expand All @@ -182,6 +188,7 @@ func (c *ConsoleJudgeReport) Display() {
c.Descs[i].CustomCase,
output,
getStderrDisplay(stat.Stderr),
stat.Duration
})
}
t.SetStyle(table.StyleLight)
Expand All @@ -192,7 +199,8 @@ func newJudgeReport() JudgeReport {
return &ConsoleJudgeReport{Stats: []CaseStatus{}}
}

func timedExecution(cmd *exec.Cmd, timeOut float64) (int8, error) {
func timedExecution(cmd *exec.Cmd, timeOut float64) (int8, time.Duration, error) {
start := time.Now()
cmd.Start()
done := make(chan error)
go func() { done <- cmd.Wait() }()
Expand All @@ -201,9 +209,11 @@ func timedExecution(cmd *exec.Cmd, timeOut float64) (int8, error) {
select {
case <-timeout:
cmd.Process.Kill()
return TO, nil
elapsed := time.Since(start)
return TO, elapsed, nil
case err := <-done:
return OK, err
elapsed := time.Since(start)
return OK, elapsed, err
}
}

Expand Down Expand Up @@ -235,12 +245,13 @@ func execute(judge Judge, desc CaseDescription, command string, args ...string)
cmd.Stdout = outputFile
cmd.Stderr = &stderrBuffer

status, err := timedExecution(cmd, desc.TimeLimit)
status, duration, err := timedExecution(cmd, desc.TimeLimit+TimeOutDelta)
if status == TO {
return CaseStatus{
Status: TL,
CheckerError: nil,
Stderr: stderrBuffer.String(),
Duration: duration,
}, nil
}

Expand All @@ -249,6 +260,7 @@ func execute(judge Judge, desc CaseDescription, command string, args ...string)
Status: RE,
CheckerError: nil,
Stderr: stderrBuffer.String(),
Duration: duration,
}, err
}

Expand All @@ -257,6 +269,7 @@ func execute(judge Judge, desc CaseDescription, command string, args ...string)
return CaseStatus{
Status: RE,
CheckerError: nil,
Duration: duration,
}, err
}
output, err := ioutil.ReadFile(desc.WorkFile)
Expand All @@ -265,6 +278,7 @@ func execute(judge Judge, desc CaseDescription, command string, args ...string)
return CaseStatus{
Status: RE,
CheckerError: nil,
Duration: duration,
}, err
}
err = judge.Checker().Check(string(output), string(expectedOutput))
Expand All @@ -273,12 +287,14 @@ func execute(judge Judge, desc CaseDescription, command string, args ...string)
Status: WA,
CheckerError: err,
Stderr: stderrBuffer.String(),
Duration: duration,
}, err
}
return CaseStatus{
Status: AC,
CheckerError: nil,
Stderr: stderrBuffer.String(),
Duration: duration,
}, err
}

Expand Down
9 changes: 5 additions & 4 deletions commands/run_test.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package commands

import (
"github.com/stretchr/testify/assert"
"os/exec"
"testing"

"github.com/stretchr/testify/assert"
)

func TestTimedOutExecution(t *testing.T) {
cmd := exec.Command("sleep", "1")
status, err := timedExecution(cmd, 999.99)
cmd := exec.Command("sleep", "1.1")
status, _, err := timedExecution(cmd, 1000+TimeOutDelta)
assert.Equal(t, err, nil)
assert.Equal(t, status, TO)
}

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

0 comments on commit a9d6e7e

Please sign in to comment.