-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime.go
78 lines (61 loc) · 1.66 KB
/
time.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package main
import (
"bufio"
"errors"
"fmt"
"github.com/JosephNaberhaus/cli-tti/util"
"log"
"os/exec"
"syscall"
"time"
)
func timeCommandAverage(command *command, expectedOutput []byte, numTime int64) (duration time.Duration, err error) {
total := time.Duration(0)
for i := int64(0); i < numTime; i++ {
duration, err := timeCommand(command, expectedOutput)
if err != nil {
return 0, err
}
total += duration
fmt.Printf("Time %d: %d milliseconds\n", i, duration.Milliseconds())
}
return time.Duration(total.Nanoseconds() / numTime) * time.Nanosecond, nil
}
func timeCommand(command *command, expectedOutput []byte) (duration time.Duration, err error) {
cmd := exec.Command(command.name, command.args...)
cmd.Stderr = nil
cmd.Stdin = nil
stdOut, err := cmd.StdoutPipe()
if err != nil {
return 0, fmt.Errorf("error opening stdout pipe: %w", err)
}
scanner := bufio.NewScanner(stdOut)
scanner.Split(bufio.ScanBytes)
go killAfterDuration(cmd, time.Duration(*timeToWait)*time.Millisecond)
matcher := util.ByteMatcher(expectedOutput)
err = cmd.Start()
if err != nil {
return 0, fmt.Errorf("error starting command: %w", err)
}
startTime := time.Now()
for scanner.Scan() {
err := matcher.Match(scanner.Bytes())
if err != nil {
return 0, fmt.Errorf("error matching bytes: %w", err)
}
if matcher.Complete() {
break
}
}
if !matcher.Complete() {
return 0, errors.New("command exited before all bytes were matched")
}
return time.Now().Sub(startTime), nil
}
func killAfterDuration(cmd *exec.Cmd, duration time.Duration) {
time.Sleep(duration)
err := cmd.Process.Signal(syscall.SIGINT)
if err != nil {
log.Fatal(err)
}
}