-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
124 lines (95 loc) · 2.7 KB
/
main.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"
"time"
"github.com/rs/zerolog/log"
"github.com/spf13/afero"
"github.com/alecthomas/kong"
"github.com/martinlindhe/feng/mapper"
"github.com/martinlindhe/feng/smoketest"
)
var args struct {
Filename string `kong:"arg" name:"filename" type:"existingfile" help:"Input yaml with file listing."`
}
type measuredExecution struct {
duration time.Duration
filename string
}
func main() {
var fs = afero.NewOsFs()
_ = kong.Parse(&args,
kong.Name("feng"),
kong.Description("A binary template reader and data presenter."))
data, err := ioutil.ReadFile(args.Filename)
if err != nil {
log.Fatal().Err(err).Msgf("failed")
}
referenceRoot := "./smoketest/reference"
err = os.RemoveAll(referenceRoot)
if err != nil {
log.Fatal().Err(err).Msgf("failed")
}
smoketests, err := smoketest.UnmarshalData(data)
if err != nil {
log.Fatal().Err(err).Msgf("failed")
}
filenames := smoketests.GenerateFilenames(filepath.Dir(args.Filename))
measures := []measuredExecution{}
for _, entry := range filenames {
log.Info().Msgf("Start entry %s\n", entry.In)
started := time.Now()
f, _ := fs.Open(entry.In)
fl, err := mapper.MapFileToMatchingTemplate(&mapper.MapperConfig{
F: f,
})
if err != nil {
// template don't match, try another
if _, ok := err.(mapper.EvaluateError); ok {
log.Print(" failed to evaluate:", err)
}
log.Print("MapReader returned err:", err)
continue
}
if len(fl.Structs) == 0 {
fmt.Println("MapReader failure, skipping")
continue
}
log.Info().Msgf("Parsed %s as %s\n\n", entry.In, fl.BaseName)
fl.Present(&mapper.PresentFileLayoutConfig{
ShowRaw: true,
ReportOverlapping: true,
InUTC: true})
timeSpent := time.Since(started)
measures = append(measures, measuredExecution{
duration: timeSpent,
filename: entry.Out,
})
filename, _ := filepath.Abs(filepath.Join(referenceRoot, entry.Out))
path := filepath.Dir(filename)
err = os.MkdirAll(path, os.ModePerm)
if err != nil {
log.Fatal().Err(err).Msgf("failed")
}
log.Info().Msgf("WRITE ROOT %s, out %s, full %s\n", referenceRoot, entry.Out, filename)
err = ioutil.WriteFile(filename, []byte(data), 0644)
if err != nil {
log.Fatal().Err(err).Msgf("failed")
}
}
sort.Slice(measures, func(i, j int) bool {
return measures[i].duration > measures[j].duration
})
// XXX sort by exec speed and show the slowest first
topMeasures := 25
if len(measures) > topMeasures {
measures = measures[:topMeasures]
}
fmt.Println("--- the", topMeasures, "slowest mappings were ---")
for _, measure := range measures {
fmt.Printf("%v:\t%s\n", measure.duration, measure.filename)
}
}