/
main_test.go
66 lines (56 loc) · 1.29 KB
/
main_test.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
package main
import (
"fmt"
"github.com/jorgenbele/go-atb/atb"
"os"
"testing"
)
var realtimeDeps []atb.RealtimeDeparture
var deps []atb.Departure
func TestMain(m *testing.M) {
var err error
realtimeDeps, err = atb.GetRealtimeDepartures("Solsiden (Trondheim)")
if err != nil {
fmt.Printf("Unable to get realtime departures: %v\n", err)
os.Exit(1)
}
deps, err = atb.GetDeparturesNow("Solsiden (Trondheim)", "Munkegata (Trondheim)")
if err != nil {
fmt.Printf("Unable to get departures: %v\n", err)
os.Exit(1)
}
os.Exit(m.Run())
}
type tfunc func()
func toDevNull(f tfunc) {
// XXX - Hack: redirect to /dev/null, should probably change printRealtimeList instead.
// Keep backup of the real stdout.
old := os.Stdout
devnull, _ := os.OpenFile("/dev/null", os.O_RDWR, 0)
os.Stdout = devnull
f()
// Restoring the real stdout.
devnull.Close()
os.Stdout = old
}
func BenchmarkPrintRealtime(b *testing.B) {
toDevNull(func() {
for n := 0; n < b.N; n++ {
printRealtimeList(realtimeDeps, true, NoRoute)
}
})
}
func BenchmarkPrintPlanMinimal(b *testing.B) {
toDevNull(func() {
for n := 0; n < b.N; n++ {
printPlanMinimal(deps)
}
})
}
func BenchmarkPrintPlanTabular(b *testing.B) {
toDevNull(func() {
for n := 0; n < b.N; n++ {
printPlanTabular(deps, symbols{})
}
})
}