-
Notifications
You must be signed in to change notification settings - Fork 3
/
fioplot-bs.go
163 lines (142 loc) · 4.99 KB
/
fioplot-bs.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
package main
import (
"fmt"
"os"
"path/filepath"
"github.com/jessevdk/go-flags"
bar "github.com/vk-en/fioplot-bs/pkg/barchart"
csv "github.com/vk-en/fioplot-bs/pkg/csvtable"
log "github.com/vk-en/fioplot-bs/pkg/loggraphs"
xlsx "github.com/vk-en/fioplot-bs/pkg/xlsxchart"
bs "github.com/vk-en/fioplot-bs/pkg/bsdata"
)
// Options - command line arguments
type Options struct {
TestName string `short:"n" long:"name" description:"Name for folder with results" required:"true"`
Catalog string `short:"c" long:"catalog" description:"Full path to catalog with *.json files and/or catalogs with *.log results (Ex. /home/user/dirWithResults)" required:"true"`
ImgFormat string `short:"f" long:"format" description:"Format of an images with charts" default:"png" choice:"png" choice:"svg"`
Description string `short:"d" long:"description" description:"Description for image results" default:"github.com/vk-en/fioplot-bs"`
LogGraphs bool `short:"l" long:"loggraphs" description:"Create log graphs" optionalArgument:"true"`
}
const (
version = "1.0.0"
)
var opts Options
var parser = flags.NewParser(&opts, flags.Default)
var pathToResults string
var csvFiles []string
// argparse - parse command line arguments
func argparse() {
if _, err := parser.Parse(); err != nil {
switch flagsErr := err.(type) {
case flags.ErrorType:
if flagsErr == flags.ErrHelp {
os.Exit(0)
}
os.Exit(1)
default:
os.Exit(1)
}
}
}
// createFolderForResults - if folder already exists create new folder with new name
func createFolderForResults(folderName string) (string, error) {
curentlyPath, _ := os.Getwd()
fullPath := filepath.Join(curentlyPath, folderName)
if _, err := os.Stat(fullPath); os.IsNotExist(err) {
if err := os.Mkdir(fullPath, 0755); err != nil {
return "", err
}
return fullPath, nil
} else {
fmt.Printf("folder with results [%s] already exists.\n", fullPath)
fmt.Println("Enter a new directory name with results:")
var newName string
fmt.Scanf("%s\n", &newName)
if fullPath, err = createFolderForResults(newName); err != nil {
return "", err
}
}
return fullPath, nil
}
// createCSVTable - create CSV table from JSON file
func createCSVTables(allTestInfo bs.AllTestInfo) error {
csvFolderPath := filepath.Join(allTestInfo.MainPathToResults, "csv-tables")
if _, err := os.Stat(csvFolderPath); os.IsNotExist(err) {
if err := os.Mkdir(csvFolderPath, 0755); err != nil {
return err
}
}
// Create CSV tables for each tests
for _, testResults := range allTestInfo.Tests {
testResults.CSVFileName = fmt.Sprintf("%s.%s", testResults.TestName, "csv")
testResults.CSVFilePath = filepath.Join(csvFolderPath, testResults.CSVFileName)
if err := csv.ConvertJSONtoCSV(testResults.JSONResults, testResults.CSVFilePath); err != nil {
fmt.Printf("could not create CSV table for file [%s]\n. Error: %v\n",
testResults.TestName, err)
continue
}
// Temporary solution
csvFiles = append(csvFiles, filepath.Join(csvFolderPath, testResults.CSVFileName))
}
return nil
}
// makeResults - create folder with results (xlsx, csv, and BarChars img)
func makeResults() error {
var err error
if pathToResults, err = createFolderForResults(opts.TestName); err != nil {
return fmt.Errorf("could not create folder for results: %w", err)
}
fmt.Println("This process will take some time, please wait...")
allResults, err := bs.ReadAllJSONFiles(opts.Catalog)
if err != nil {
cleanUpDir()
return fmt.Errorf("could not read all JSON files: %w", err)
}
allResults.MainPathToResults = pathToResults
allResults.PathWithSrcResults = opts.Catalog
allResults.ImgFormat = opts.ImgFormat
allResults.Description = opts.Description
if opts.LogGraphs {
if err := log.CreateGraphsFromLogs(allResults); err != nil {
return fmt.Errorf("could not create graphs from logs: %w", err)
}
}
// Create CSV tables for each JSON file
if err := createCSVTables(allResults); err != nil {
return fmt.Errorf("could not create CSV tables: %w", err)
}
if len(csvFiles) == 0 {
cleanUpDir()
return fmt.Errorf(fmt.Sprintf("%s\n%s\n",
"Failed to read FIO results from JSON.",
"Results and graphs were not generated =("))
}
if err := xlsx.CreateXlsxReport(csvFiles, pathToResults); err != nil {
// not a critical error, can move next, just log it
fmt.Printf("could not create xsls file.\n Error: %v\n", err)
}
//Create bar charts
if err := bar.CreateBarCharts(csvFiles, opts.Description, pathToResults, opts.ImgFormat); err != nil {
fmt.Printf("could not create barCharts.\n Error: %v\n", err)
} else {
fmt.Println("Results and graphs were generated successfully!")
}
fmt.Println("Results are in folder:", pathToResults)
return nil
}
// cleanUpDir - remove all files from directory for current test
func cleanUpDir() {
if err := os.RemoveAll(pathToResults); err != nil {
fmt.Printf("could not remove folder with results: %v", err)
}
}
func main() {
argparse()
if err := makeResults(); err != nil {
fmt.Println(err)
}
}
func init() {
fmt.Println("fioplot-bs version ", version)
}