This repository has been archived by the owner on Jan 12, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
diablo.go
executable file
·231 lines (193 loc) · 7.9 KB
/
diablo.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// DIABLO provides a unique blockchain benchmark framework focusing on analysis
// of real-world workloads with distributed clients from the core.
// The diablo framework provides modular blockchain implementation as well as
// simple workload definition and design, aiming to maximise the applicability
// and integration of as many systems as possible.
//
// About the architecture
//
// The main aspects of the Diablo benchmark is the "primary" and "secondary"
// benchmark nodes. "Primary" is the main node that orchestrates the benchmark
// and sends commands to the distributed clients to start the benchmark. It is
// the main generator of the workload and contains all the information required.
// The "Secondary" is used to accept commands from the master, connect to the
// blockchain node and then send the transactions executing the benchmark while
// measuring information. There should always be ONE primary and ONE OR MORE
// secondaries. The number of secondaries are defined in the benchmark
// configuration.
package main
import (
"diablo-benchmark/blockchains/workloadgenerators"
"diablo-benchmark/core"
"diablo-benchmark/core/configs"
"diablo-benchmark/core/configs/parsers"
"fmt"
"os"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// Prints the welcome message that is seen at the start
func printWelcome(isPrimary bool) {
fmt.Println("=====================")
fmt.Println(
"\n" +
"████████▄ ▄█ ▄████████ ▀█████████▄ ▄█ ▄██████▄ \n" +
"██ ▀███ ███ ███ ███ ███ ███ ███ ███ ███ \n" +
"██ ███ ███▌ ███ ███ ███ ███ ███ ███ ███ \n" +
"██ ███ ███▌ ███ ███ ▄███▄▄▄██▀ ███ ███ ███ \n" +
"██ ███ ███▌ ▀███████████ ▀▀███▀▀▀██▄ ███ ███ ███ \n" +
"██ ███ ███ ███ ███ ███ ██▄ ███ ███ ███ \n" +
"██ ▄███ ███ ███ ███ ███ ███ ███▌ ▄ ███ ███ \n" +
"███████▀ █▀ ███ █▀ ▄█████████▀ █████▄▄██ ▀██████▀ \n" +
" ▀",
)
if isPrimary {
fmt.Println(
"\n" +
"______ _ \n" +
"| ___ \\ (_) \n" +
"| |_/ /_ __ _ _ __ ___ __ _ _ __ _ _ \n" +
"| __/| '__|| || '_ ` _ \\ / _` || '__|| | | |\n" +
"| | | | | || | | | | || (_| || | | |_| |\n" +
"\\_| |_| |_||_| |_| |_| \\__,_||_| \\__, |\n" +
" __/ |\n" +
" |___/ ",
)
} else {
fmt.Println("\n" +
" _____ _ \n" +
"/ ___| | | \n" +
"\\ `--. ___ ___ ___ _ __ __| | __ _ _ __ _ _ \n" +
" `--. \\ / _ \\ / __|/ _ \\ | '_ \\ / _` | / _` || '__|| | | |\n" +
"/\\__/ /| __/| (__| (_) || | | || (_| || (_| || | | |_| |\n" +
"\\____/ \\___| \\___|\\___/ |_| |_| \\__,_| \\__,_||_| \\__, |\n" +
" __/ |\n" +
" |___/",
)
}
fmt.Println("=====================")
}
// Prepares the logger
// TODO make a flag to change level of the logger (DEBUG, INFO, ..)
func prepareLogger(logType string, level zapcore.Level) {
// config := zap.NewDevelopmentConfig()
// config.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
// logger, err := config.Build()
// Set up the console log - so we can see the colours
consoleConfig := zap.NewDevelopmentEncoderConfig()
consoleConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
consoleEncoder := zapcore.NewConsoleEncoder(consoleConfig)
atomicLevel := zap.NewAtomicLevel()
consoleCore := zapcore.NewCore(consoleEncoder, zapcore.Lock(os.Stdout), atomicLevel)
atomicLevel.SetLevel(level)
// Set up the file-logger
fileConfig := zap.NewProductionEncoderConfig()
fileConfig.EncodeTime = zapcore.ISO8601TimeEncoder
fileConfig.EncodeLevel = zapcore.CapitalLevelEncoder
// Create the file, add the sync
file, err := os.Create(fmt.Sprintf("%s_diablo.log", logType))
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to create log file")
os.Exit(1)
}
fileSync := zapcore.AddSync(file)
fileEncoder := zapcore.NewJSONEncoder(fileConfig)
fileLevel := zap.NewAtomicLevel()
fileCore := zapcore.NewCore(fileEncoder, fileSync, fileLevel)
// Set up both the loggers
cores := zapcore.NewTee(
consoleCore,
fileCore,
)
logger := zap.New(cores)
zap.ReplaceGlobals(logger)
}
// Run the primary functions
func runPrimary(primaryArgs *core.PrimaryArgs) {
// Check the arguments
primaryArgs.CheckArgs()
zap.L().Info("loading configs",
zap.String("bench config", primaryArgs.BenchConfigPath),
zap.String("chain config", primaryArgs.ChainConfigPath),
)
// Parse the configurations.
bConfig, err := parsers.ParseBenchConfig(primaryArgs.BenchConfigPath)
if err != nil {
zap.L().Error(err.Error())
os.Exit(1)
}
cConfig, err := parsers.ParseChainConfig(primaryArgs.ChainConfigPath)
if err != nil {
zap.L().Error(err.Error())
os.Exit(1)
}
generatorClass, err := workloadgenerators.GetWorkloadGenerator(cConfig)
if err != nil {
zap.L().Error("failed to get workload generators",
zap.String("error", err.Error()))
os.Exit(1)
}
wg := generatorClass.NewGenerator(cConfig, bConfig)
// Initialise the TCP server
m := core.InitPrimary(primaryArgs.ListenAddr, bConfig.Secondaries, wg, bConfig, cConfig)
// Run the benchmark flow
zap.L().Info("Primary ready, running benchmark flow")
m.Run()
}
// Run the secondary
func runSecondary(secondaryArgs *core.SecondaryArgs) {
secondaryArgs.SecondaryArgs()
chainConfiguration, err := parsers.ParseChainConfig(secondaryArgs.ChainConfigPath)
if err != nil {
zap.L().Error("failed to parse config",
zap.Error(err))
os.Exit(1)
}
benchConfiguration, err := parsers.ParseBenchConfig(secondaryArgs.BenchConfigPath)
// Check the timeout with args
if secondaryArgs.Timeout == 0 && benchConfiguration.Timeout <= 0 {
zap.L().Warn(fmt.Sprintf("Invalid or no timeout provided, defaulting to %d", configs.DefaultTimeout))
benchConfiguration.Timeout = configs.DefaultTimeout
} else if secondaryArgs.Timeout > 0 {
zap.L().Warn(fmt.Sprintf("Overwriting config timeout (%d) with flag %d", benchConfiguration.Timeout, secondaryArgs.Timeout))
benchConfiguration.Timeout = secondaryArgs.Timeout
}
secondary, err := core.NewSecondary(chainConfiguration, benchConfiguration, secondaryArgs.PrimaryAddr)
if err != nil {
zap.L().Error("Failed to start new secondary",
zap.Error(err))
os.Exit(1)
}
secondary.Run()
}
// Main running function
func main() {
args := core.DefineArguments()
if len(os.Args) < 2 {
// This is going to be a primary
fmt.Fprintf(os.Stderr, "No subcommand given (primary/secondary), exiting!")
os.Exit(1)
} else {
switch os.Args[1] {
case "primary":
// Print the welcome message
printWelcome(true)
// Parse the arguments
args.PrimaryCommand.Parse(os.Args[2:])
prepareLogger("primary", args.PrimaryArgs.LogLevel)
runPrimary(args.PrimaryArgs)
case "secondary":
// Print the welcome message
printWelcome(false)
// Parse the arguments
err := args.SecondaryCommand.Parse(os.Args[2:])
prepareLogger("secondary", args.SecondaryArgs.LogLevel)
if err != nil {
zap.L().Error("error parsing",
zap.Error(err))
os.Exit(1)
}
runSecondary(args.SecondaryArgs)
}
}
}