/
io.go
196 lines (163 loc) · 4.16 KB
/
io.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package io
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"runtime/pprof"
"strconv"
"strings"
"time"
"github.com/gonum/plot"
"github.com/gonum/plot/plotutil"
"github.com/gonum/plot/vg"
)
type TestCaseFunc func(*Input, *Output)
type Parser struct {
f TestCaseFunc
input *Input
output *Output
compareOutput *CompareOutput
baseFn string
inputFn string
outputFn string
correctFn string
profileFn string
}
func TestCases(f TestCaseFunc) {
log.SetFlags(0)
parser := Parser{
f: f,
}
if len(os.Args) < 2 {
log.Fatalln("You need to specify at least one input file")
}
for _, inputFn := range os.Args[1:] {
parser.SetFn(inputFn)
parser.ParseFile()
}
}
func (parser *Parser) SetFn(inputFn string) {
parser.inputFn = inputFn
parser.baseFn = strings.TrimSuffix(inputFn, ".in")
parser.outputFn = parser.baseFn + ".out"
parser.correctFn = parser.baseFn + ".correct"
parser.profileFn = parser.baseFn + ".prof"
}
func (parser *Parser) formatDuration(d int64) string {
var i int
df := float64(d)
units := []string{"ns", "us", "ms", "s"}
for i = 0; df >= 1000; i++ {
df /= 1000
if i >= 3 {
break
}
}
res := fmt.Sprintf("%.2f%s", df, units[i])
return res
}
func (parser *Parser) ParseFile() {
inputF, err := os.Open(parser.inputFn)
if err != nil {
log.Fatalln("Error opening input file:", err)
}
defer inputF.Close()
outputF, err := os.Create(parser.outputFn)
if err != nil {
log.Fatalln("Error creating output file:", err)
}
defer outputF.Close()
scanner := bufio.NewScanner(inputF)
scanner.Split(bufio.ScanWords)
parser.output = newOutput(outputF)
parser.input = newInput(scanner)
parser.compareOutput = nil
if _, err := os.Stat(parser.correctFn); err == nil {
correctF, err := os.Open(parser.correctFn)
if err != nil {
log.Fatalln("Error opening correct file:", err)
}
defer correctF.Close()
parser.compareOutput = NewCompareOutput(correctF)
}
T := parser.input.Int()
startTime := time.Now().UnixNano()
for i := 1; i <= T; i++ {
parser.runTestCase(i)
}
log.Println("Total time:", parser.formatDuration(time.Now().UnixNano()-startTime))
}
func (parser *Parser) runTestCase(i int) {
warningTimer := time.NewTimer(500 * time.Millisecond)
startProfileTimer := time.NewTimer(1 * time.Second)
stopProfileTimer := time.NewTimer(10 * time.Second)
periodicPrintTicker := time.NewTicker(1 * time.Second)
doneChan := make(chan bool)
go func() {
parser.output.init(parser.input, i)
parser.input.init()
parser.f(parser.input, parser.output)
if parser.compareOutput != nil && parser.compareOutput.HasOutput(i) {
parser.output.AssertEqual(string(parser.compareOutput.GetOutput(i)))
}
parser.output.flush()
parser.writeChart(i)
doneChan <- true
}()
var f *os.File
var err error
loop:
for {
select {
case <-warningTimer.C:
parser.output.Debug("Long calculation")
case <-startProfileTimer.C:
f, err = os.Create(parser.profileFn)
if err != nil {
log.Fatalln("Error opening profile file:", err)
}
pprof.StartCPUProfile(f)
case <-stopProfileTimer.C:
pprof.StopCPUProfile()
f = nil
out, err := exec.Command("go", "tool", "pprof", "-top", os.Args[0], parser.profileFn).CombinedOutput()
if err != nil {
log.Fatalln("Error running profile tool:", err)
}
parser.output.Debug("CPUProfile:", string(out))
err = exec.Command("go", "tool", "pprof", "-web", os.Args[0], parser.profileFn).Start()
if err != nil {
log.Fatalln("Error running profile tool:", err)
}
case <-periodicPrintTicker.C:
parser.output.triggerPeriodic()
case <-doneChan:
break loop
}
}
periodicPrintTicker.Stop()
parser.output.resetPeriodic()
if f != nil {
pprof.StopCPUProfile()
}
}
func (parser *Parser) writeChart(i int) {
if len(parser.output.points) == 0 {
return
}
p, err := plot.New()
if err != nil {
log.Fatalln("Error creating plot:", err)
}
err = plotutil.AddLinePoints(p, "", parser.output.points)
if err != nil {
log.Fatalln("Error adding linepoints:", err)
}
err = p.Save(4*vg.Inch, 4*vg.Inch, parser.baseFn+strconv.Itoa(i)+".png")
if err != nil {
log.Fatalln("Error saving img:", err)
}
parser.output.points = parser.output.points[:0]
}