Skip to content

Refactor Size Of Go Files #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions hazeltest.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import (
clientConfig "hazeltest/client/config"
"hazeltest/logging"
"hazeltest/maps"
_ "hazeltest/maps/load"
_ "hazeltest/maps/pokedex"
"hazeltest/queues"
_ "hazeltest/queues/tweets"
"os"
"strings"
"sync"
Expand Down
105 changes: 101 additions & 4 deletions logging/logging.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package logging

import (
"os"
"fmt"
"github.com/google/uuid"
log "github.com/sirupsen/logrus"
"io"
"os"
"runtime"
"strings"
log "github.com/sirupsen/logrus"
)

const InternalStateInfo = "internal state info"
Expand All @@ -13,7 +16,11 @@ const IoError = "io error"
const HzError = "hazelcast error"
const ConfigurationError = "incorrect or incomplete configuration"

func init () {
type LogProvider struct {
ClientID uuid.UUID
}

func init() {

log.SetFormatter(&log.JSONFormatter{})

Expand Down Expand Up @@ -47,4 +54,94 @@ func init () {
log.SetOutput(out)
log.SetReportCaller(true)

}
}

func (lp *LogProvider) LogIoEvent(msg string, level log.Level) {

fields := log.Fields{
"kind": IoError,
}

lp.doLog(msg, fields, level)

}

func (lp *LogProvider) LogTimingEvent(operation string, mapName string, tookMs int, level log.Level) {

fields := log.Fields{
"kind": TimingInfo,
"map": mapName,
"tookMs": tookMs,
}

lp.doLog(fmt.Sprintf("'%s' took %d ms", operation, tookMs), fields, level)

}

func (lp *LogProvider) LogInternalStateEvent(msg string, level log.Level) {

fields := log.Fields{
"kind": InternalStateInfo,
}

lp.doLog(msg, fields, level)

}

func (lp *LogProvider) LogHzEvent(msg string, level log.Level) {

fields := log.Fields{
"kind": HzError,
}

lp.doLog(msg, fields, level)
}

func (lp *LogProvider) LogErrUponConfigExtraction(keyPath string, err error, level log.Level) {

lp.logConfigEvent(keyPath, "config file", fmt.Sprintf("will use default for property due to error: %s", err), level)

}

func (lp *LogProvider) logConfigEvent(configValue string, source string, msg string, level log.Level) {

fields := log.Fields{
"kind": ConfigurationError,
"value": configValue,
"source": source,
}

lp.doLog(msg, fields, level)

}

func (lp *LogProvider) doLog(msg string, fields log.Fields, level log.Level) {

fields["caller"] = getCaller()
fields["client"] = lp.ClientID

if level == log.FatalLevel {
log.WithFields(fields).Fatal(msg)
} else if level == log.WarnLevel {
log.WithFields(fields).Warn(msg)
} else if level == log.InfoLevel {
log.WithFields(fields).Info(msg)
} else {
log.WithFields(fields).Trace(msg)
}

}

func getCaller() string {

// Skipping three stacks will bring us to the method or function that originally invoked the logging method
pc, _, _, ok := runtime.Caller(3)

if !ok {
return "unknown"
}

file, line := runtime.FuncForPC(pc).FileLine(pc)
return fmt.Sprintf("%s:%d", file, line)

}
110 changes: 31 additions & 79 deletions maps/load/loadrunner.go → maps/loadrunner.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package load
package maps

import (
"context"
Expand All @@ -7,8 +7,6 @@ import (
"fmt"
"hazeltest/client"
"hazeltest/client/config"
"hazeltest/logging"
"hazeltest/maps"
"math/rand"
"strconv"
"time"
Expand All @@ -17,7 +15,7 @@ import (
log "github.com/sirupsen/logrus"
)

type LoadRunner struct{}
type loadRunner struct{}

type loadElement struct {
Key string
Expand All @@ -43,16 +41,16 @@ var (
)

func init() {
maps.Register(LoadRunner{})
register(loadRunner{})
gob.Register(loadElement{})
}

func (r LoadRunner) RunMapTests(hzCluster string, hzMembers []string) {
func (r loadRunner) runMapTests(hzCluster string, hzMembers []string) {

mapRunnerConfig := populateConfig()
mapRunnerConfig := populateLoadConfig()

if !mapRunnerConfig.Enabled {
logInternalStateEvent("loadrunner not enabled -- won't run", log.InfoLevel)
if !mapRunnerConfig.enabled {
lp.LogInternalStateEvent("loadrunner not enabled -- won't run", log.InfoLevel)
return
}

Expand All @@ -62,29 +60,29 @@ func (r LoadRunner) RunMapTests(hzCluster string, hzMembers []string) {
hzClient, err := client.InitHazelcastClient(ctx, fmt.Sprintf("%s-loadrunner", clientID), hzCluster, hzMembers)

if err != nil {
logHzEvent(fmt.Sprintf("unable to initialize hazelcast client: %s", err))
lp.LogHzEvent(fmt.Sprintf("unable to initialize hazelcast client: %s", err), log.FatalLevel)
}
defer hzClient.Shutdown(ctx)

logInternalStateEvent("initialized hazelcast client", log.InfoLevel)
logInternalStateEvent("starting load test loop", log.InfoLevel)
lp.LogInternalStateEvent("initialized hazelcast client", log.InfoLevel)
lp.LogInternalStateEvent("starting load test loop", log.InfoLevel)

elements := populateLoadElements()

testLoop := maps.TestLoop[loadElement]{
ID: uuid.New(),
Source: "load",
HzClient: hzClient,
Config: mapRunnerConfig,
Elements: elements,
Ctx: ctx,
GetElementIdFunc: getElementID,
DeserializeElementFunc: deserializeElementFunc,
testLoop := testLoop[loadElement]{
id: uuid.New(),
source: "load",
hzClient: hzClient,
config: mapRunnerConfig,
elements: elements,
ctx: ctx,
getElementIdFunc: getLoadElementID,
deserializeElementFunc: deserializeLoadElement,
}

testLoop.Run()
testLoop.run()

logInternalStateEvent("finished load test loop", log.InfoLevel)
lp.LogInternalStateEvent("finished load test loop", log.InfoLevel)

}

Expand Down Expand Up @@ -131,14 +129,14 @@ func generateRandomPayload(n int) string {

}

func getElementID(element interface{}) string {
func getLoadElementID(element interface{}) string {

loadElement := element.(loadElement)
return loadElement.Key

}

func deserializeElementFunc(elementFromHz interface{}) error {
func deserializeLoadElement(elementFromHz interface{}) error {

_, ok := elementFromHz.(loadElement)

Expand All @@ -150,15 +148,15 @@ func deserializeElementFunc(elementFromHz interface{}) error {

}

func populateConfig() *maps.MapRunnerConfig {
func populateLoadConfig() *runnerConfig {

parsedConfig := config.GetParsedConfig()
runnerKeyPath := "maptests.load"

keyPath := runnerKeyPath + ".numEntriesPerMap"
valueFromConfig, err := config.ExtractConfigValue(parsedConfig, keyPath)
if err != nil {
logErrUponConfigExtraction(keyPath, err)
lp.LogErrUponConfigExtraction(keyPath, err, log.WarnLevel)
numEntriesPerMap = defaultNumEntriesPerMap
} else {
numEntriesPerMap = valueFromConfig.(int)
Expand All @@ -167,63 +165,17 @@ func populateConfig() *maps.MapRunnerConfig {
keyPath = runnerKeyPath + ".payloadSizeBytes"
valueFromConfig, err = config.ExtractConfigValue(parsedConfig, keyPath)
if err != nil {
logErrUponConfigExtraction(keyPath, err)
lp.LogErrUponConfigExtraction(keyPath, err, log.WarnLevel)
payloadSizeBytes = defaultPayloadSizeBytes
} else {
payloadSizeBytes = valueFromConfig.(int)
}

configBuilder := maps.MapRunnerConfigBuilder{
RunnerKeyPath: runnerKeyPath,
MapBaseName: "load",
ParsedConfig: parsedConfig,
configBuilder := runnerConfigBuilder{
runnerKeyPath: runnerKeyPath,
mapBaseName: "load",
parsedConfig: parsedConfig,
}
return configBuilder.PopulateConfig()

}

func logConfigEvent(configValue string, source string, msg string, logLevel log.Level) {

fields := log.Fields{
"kind": logging.ConfigurationError,
"value": configValue,
"source": source,
"client": client.ClientID(),
}
if logLevel == log.WarnLevel {
log.WithFields(fields).Warn(msg)
} else {
log.WithFields(fields).Fatal(msg)
}

}

func logInternalStateEvent(msg string, logLevel log.Level) {

fields := log.Fields{
"kind": logging.InternalStateInfo,
"client": client.ClientID(),
}

if logLevel == log.TraceLevel {
log.WithFields(fields).Trace(msg)
} else {
log.WithFields(fields).Info(msg)
}

}

func logHzEvent(msg string) {

log.WithFields(log.Fields{
"kind": logging.HzError,
"client": client.ClientID(),
}).Fatal(msg)

}

func logErrUponConfigExtraction(keyPath string, err error) {

logConfigEvent(keyPath, "config file", fmt.Sprintf("will use default for property due to error: %s", err), log.WarnLevel)
return configBuilder.populateConfig()

}
11 changes: 0 additions & 11 deletions maps/maprunner.go

This file was deleted.

Loading