From f036b1d0734056516645666c582dd4275cab2718 Mon Sep 17 00:00:00 2001 From: Solomon Himelbloom <7608183+TechSolomon@users.noreply.github.com> Date: Fri, 19 Apr 2024 17:11:55 -0800 Subject: [PATCH] Finalize code structure for experiment replication (#43) * refactor: entry point for main application, build information, & testing * feat: split input & output metadata via project settings * fix: repackaged JSON object contents + diode metadata * docs: update main program directory name * Saturate components for testing diode system behavior (#42) --- Dockerfile | 2 +- Makefile | 2 +- config/settings.yaml | 3 +- docker-compose.yaml | 8 ++---- go.mod | 7 ++--- go.sum | 16 +++-------- insights/benchmark.go | 11 +++---- insights/experiment.go | 38 ------------------------- insights/observability.sh | 2 +- main.go | 2 +- utility/application.go | 57 ++++++++++++++----------------------- utility/application_test.go | 35 ++++++++++++++--------- utility/republisher.go | 4 +-- 13 files changed, 61 insertions(+), 126 deletions(-) delete mode 100644 insights/experiment.go diff --git a/Dockerfile b/Dockerfile index 94253f0..9940826 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,4 +6,4 @@ FROM golang:alpine WORKDIR /cli COPY . /cli RUN CGO_ENABLED=0 GOOS=linux go build -o diode -CMD [ "./diode", "mqtt" ] +CMD [ "./diode", "diagnostics" ] diff --git a/Makefile b/Makefile index ac5b806..6477a9e 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,5 @@ BIN_NAME=diode -BIN_VERSION=0.1.1 +BIN_VERSION=0.1.2 BIN_DATE=$(shell date +%FT%T%z) all: build diff --git a/config/settings.yaml b/config/settings.yaml index 4360841..84d2044 100644 --- a/config/settings.yaml +++ b/config/settings.yaml @@ -16,5 +16,4 @@ mqtt: outside: server: "localhost" port: 1883 - prefix: "diode" - # Avoid namespace collisions + prefix: "diode" # Avoid namespace collisions diff --git a/docker-compose.yaml b/docker-compose.yaml index e0529ef..1f1baf4 100644 --- a/docker-compose.yaml +++ b/docker-compose.yaml @@ -9,8 +9,8 @@ services: source: ./config/mosquitto.conf target: /mosquitto/config/mosquitto.conf ports: - - target: 1883 - published: 1883 + - target: 9001 + published: 9001 protocol: tcp mode: host mqtt-client: @@ -20,7 +20,3 @@ services: container_name: sample-client-connection depends_on: - mqtt-broker - -volumes: - data: - name: "mqtt-benchmark-data" diff --git a/go.mod b/go.mod index e532a84..a7ab099 100644 --- a/go.mod +++ b/go.mod @@ -4,7 +4,6 @@ go 1.22 require ( github.com/eclipse/paho.mqtt.golang v1.4.3 - github.com/olekukonko/tablewriter v0.0.5 github.com/urfave/cli/v2 v2.27.1 gopkg.in/yaml.v2 v2.4.0 ) @@ -12,10 +11,8 @@ require ( require ( github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect github.com/gorilla/websocket v1.5.1 // indirect - github.com/mattn/go-runewidth v0.0.15 // indirect - github.com/rivo/uniseg v0.4.7 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 // indirect - golang.org/x/net v0.23.0 // indirect - golang.org/x/sync v0.6.0 // indirect + golang.org/x/net v0.24.0 // indirect + golang.org/x/sync v0.7.0 // indirect ) diff --git a/go.sum b/go.sum index 6b066e3..22d2600 100644 --- a/go.sum +++ b/go.sum @@ -4,24 +4,16 @@ github.com/eclipse/paho.mqtt.golang v1.4.3 h1:2kwcUGn8seMUfWndX0hGbvH8r7crgcJguQ github.com/eclipse/paho.mqtt.golang v1.4.3/go.mod h1:CSYvoAlsMkhYOXh/oKyxa8EcBci6dVkLCbo5tTC1RIE= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= -github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= -github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U= -github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= -github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec= -github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY= -github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= -github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/urfave/cli/v2 v2.27.1 h1:8xSQ6szndafKVRmfyeUMxkNUJQMjL1F2zmsZ+qHpfho= github.com/urfave/cli/v2 v2.27.1/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ= github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913 h1:+qGGcbkzsfDQNPPe9UDgpxAWQrhbbBXOYJFQDq/dtJw= github.com/xrash/smetrics v0.0.0-20240312152122-5f08fbb34913/go.mod h1:4aEEwZQutDLsQv2Deui4iYQ6DWTxR14g6m8Wv88+Xqk= -golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= -golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= -golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= -golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/net v0.24.0 h1:1PcaxkF854Fu3+lvBIx5SYn9wRlBzzcnHZSiaFFAb0w= +golang.org/x/net v0.24.0/go.mod h1:2Q7sJY5mzlzWjKtYUEXSlBWCdyaioyXzRB2RtU8KVE8= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= diff --git a/insights/benchmark.go b/insights/benchmark.go index 439b2a1..0d4fd6c 100644 --- a/insights/benchmark.go +++ b/insights/benchmark.go @@ -3,9 +3,7 @@ package analysis import ( "crypto/sha256" "io" - "math/rand" "net/http" - "time" ) func Pong() bool { @@ -23,6 +21,10 @@ func Pong() bool { return true } +func Saturate() { + println(">> Data Diode Components") +} + func Checksum() [32]byte { location := "https://www.gutenberg.org/cache/epub/84/pg84.txt" @@ -44,8 +46,3 @@ func Checksum() [32]byte { return hash } - -func Value() int { - randGenerator := rand.New(rand.NewSource(time.Now().UnixNano())) - return randGenerator.Intn(100) -} diff --git a/insights/experiment.go b/insights/experiment.go deleted file mode 100644 index 191b195..0000000 --- a/insights/experiment.go +++ /dev/null @@ -1,38 +0,0 @@ -package analysis - -import ( - "fmt" - "os" - - "github.com/olekukonko/tablewriter" -) - -func Isolation() { - data := [][]string{ - {"Objective", "Vary the packet size in a one packet per second test to observe the impact on bandwidth and latency."}, - {"Expectation", "Low data rate (small packets) will deliver poor bandwidth but good latency, high data rate (large packets) will deliver good bandwidth but poor latency."}, - {"Scenario", "Investigate what happens if more data per second is sent than the diode can forward."}, - {"Question", "Is the excess data queued or dropped?"}, - } - - table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"Experiment", "Description"}) - - for _, v := range data { - table.Append(v) - } - - table.Render() -} - -func Validation() { - fmt.Println(">> Variable Packet Size") - - example := []string{ - "Latency", "Throughput", "Bandwidth", "Packet Queue", - } - - for _, v := range example { - fmt.Println("* ", v) - } -} diff --git a/insights/observability.sh b/insights/observability.sh index 45fe4d4..f0434d0 100755 --- a/insights/observability.sh +++ b/insights/observability.sh @@ -2,7 +2,7 @@ START=`date +%Y-%m-%d-%k:%m:%S` COLLECTION=`date +%Y-%m-%d` -LOGFILE="insights/ACEP_CAMIO_SEDOID_RESULTS-${COLLECTION}.txt" +LOGFILE="insights/DIODE_RESULTS-${COLLECTION}.txt" function log() { echo "diode: $@" diff --git a/main.go b/main.go index 2278c9c..6597301 100644 --- a/main.go +++ b/main.go @@ -22,7 +22,6 @@ var ( SemVer string BuildInfo string ConfigSettings = "config/settings.yaml" - InputTextFile = "docs/example.txt" ) type Configuration struct { @@ -141,6 +140,7 @@ func main() { Usage: "System benchmark analysis + report performance metrics", Action: func(bCtx *cli.Context) error { fmt.Println("----- BENCHMARKS -----") + analysis.Saturate() return nil }, }, diff --git a/utility/application.go b/utility/application.go index 598afde..6c4682e 100644 --- a/utility/application.go +++ b/utility/application.go @@ -4,15 +4,13 @@ import ( "fmt" "log" "net" - "time" ) const ( ACKNOWLEDGEMENT = "OK\r\n" CONN_TYPE = "tcp" - MAX_ATTEMPTS = 2 - CHUNK_SIZE = 1460 // ? Characters - SAMPLE = 10240 // 10 Kbytes + CHUNK_SIZE = 1460 + SAMPLE = 10240 ) func SendMessage(input string, client string) { @@ -45,11 +43,6 @@ func SendMessage(input string, client string) { } func StartPlaceholderClient(host string, port int) { - - for i := 1; i <= MAX_ATTEMPTS; i++ { - fmt.Printf(">> [%d of %d] Dialing host %s on port %d via %s...\n", i, MAX_ATTEMPTS, host, port, CONN_TYPE) - } - conn, err := net.Dial(CONN_TYPE, fmt.Sprintf("%s:%d", host, port)) if err != nil { @@ -73,41 +66,33 @@ func StartPlaceholderClient(host string, port int) { fmt.Printf(">> Server response: %s\n", string(buffer[:bytesRead])) } -func ProcessMessage(message string, conn net.Conn) bool { - for try := 1; try <= MAX_ATTEMPTS; try++ { - if len(message) > CHUNK_SIZE { - index := 0 +func ProcessMessage(message string, conn net.Conn) { + if len(message) > CHUNK_SIZE { + index := 0 - for index < len(message) { - chunk := message[index : index+CHUNK_SIZE] + for index < len(message) { + chunk := message[index : index+CHUNK_SIZE] - _, err := conn.Write([]byte(chunk)) - if err != nil { - fmt.Println(">> [!] Error sending data: ", err) - return true - } + _, err := conn.Write([]byte(chunk)) + if err != nil { + fmt.Println(">> [!] Error sending data: ", err) + } - response := make([]byte, 4) - _, err = conn.Read(response) - if err != nil { - fmt.Println(">> [!] Error receiving ACK: ", err) - return true - } + response := make([]byte, 4) + _, err = conn.Read(response) + if err != nil { + fmt.Println(">> [!] Error receiving ACK: ", err) + } - if string(response) != ACKNOWLEDGEMENT { - fmt.Println(">> [?] Invalid ACK received.") - return true - } + if string(response) != ACKNOWLEDGEMENT { + fmt.Println(">> [?] Invalid ACK received.") + } - fmt.Printf(">> Successfully sent message to diode: %s\n", chunk) + fmt.Printf(">> Successfully sent message to diode: %s\n", chunk) - index += CHUNK_SIZE - } + index += CHUNK_SIZE } - - time.Sleep(1 * time.Second) } - return false } func StartPlaceholderServer(host string, port int) { diff --git a/utility/application_test.go b/utility/application_test.go index 66c3c46..dced201 100644 --- a/utility/application_test.go +++ b/utility/application_test.go @@ -1,21 +1,30 @@ package utility -import "testing" +import ( + "net" + "testing" +) -func TestClient(t *testing.T) { - got := "server" - want := "client" - - if got != want { - t.Errorf("got %q, want %q", got, want) +func TestRecieveMessage(t *testing.T) { + serverMock, err := net.Listen("tcp", "localhost:0") + if err != nil { + t.Fatalf("[!] Failed to start the mock server: %v", err) } -} + defer serverMock.Close() + + serverMockAddress := serverMock.Addr().String() -func TestServer(t *testing.T) { - got := "client" - want := "server" + contents := make(chan string) + go func() { + err := RecieveMessage(serverMockAddress, contents) + if err != nil { + t.Errorf("[?] Returned an error: %v", err) + } + }() - if got != want { - t.Errorf("got %q, want %q", got, want) + conn, err := net.Dial("tcp", serverMockAddress) + if err != nil { + t.Fatalf("[!] Failed to connect to the mock server: %v", err) } + defer conn.Close() } diff --git a/utility/republisher.go b/utility/republisher.go index c2189c7..ee54b94 100644 --- a/utility/republisher.go +++ b/utility/republisher.go @@ -117,8 +117,6 @@ func EncapsulatePayload(message string) string { } func UnencapsulatePayload(message string) string { - // TODO: Test case(s) for various message lengths and content. - decoded, err := base64.StdEncoding.DecodeString(message) if err != nil { fmt.Println(">> [!] Error decoding the message: ", err) @@ -141,7 +139,7 @@ func PublishPayload(server string, port int, topic string, message string) { } func MakeTimestamp() int64 { - return time.Now().UnixMilli() + return time.Now().UnixMicro() } func Verification(data string) string {