Skip to content

Commit

Permalink
Merge pull request #61 from geolffreym/feat/streaming/compression
Browse files Browse the repository at this point in the history
Feat/streaming/compression
  • Loading branch information
geolffreym authored Apr 14, 2024
2 parents 4e275a1 + 481e2e2 commit 9408d13
Show file tree
Hide file tree
Showing 13 changed files with 336 additions and 255 deletions.
41 changes: 20 additions & 21 deletions .vscode/targets.log
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ make all --print-data-base --no-builtin-variables --no-builtin-rules --question
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.

# Make data base, printed on Sat Feb 4 16:13:37 2023
# Make data base, printed on Sat Aug 12 08:13:38 2023

# Variables

Expand All @@ -31,6 +31,8 @@ LC_NAME = es_NI.UTF-8
# environment
LC_NUMERIC = es_NI.UTF-8
# environment
VSCODE_CRASH_REPORTER_PROCESS_TYPE = extensionHost
# environment
VSCODE_CWD = /home/gmena
# environment
WINDOWPATH = 2
Expand All @@ -47,7 +49,6 @@ GOPATH = /home/gmena/go
# environment
VSCODE_HANDLES_UNCAUGHT_ERRORS = true
# makefile (from 'Makefile', line 13)

BINARY = main
# default
.VARIABLES :=
Expand All @@ -62,7 +63,7 @@ XDG_DATA_DIRS = /usr/share/ubuntu:/usr/share/gnome:/home/gmena/.local/share/flat
# automatic
%F = $(notdir $%)
# environment
VSCODE_CODE_CACHE_PATH = /home/gmena/.config/Code/CachedData/97dec172d3256f8ca4bfb2143f3f76b503ca0534
VSCODE_CODE_CACHE_PATH = /home/gmena/.config/Code/CachedData/6c3e3dba23e8fadc360aed75ce363ba185c49794
# environment
LANG = C
# environment
Expand Down Expand Up @@ -99,19 +100,18 @@ XDG_CONFIG_DIRS = /etc/xdg/xdg-ubuntu:/etc/xdg
XDG_SESSION_DESKTOP = ubuntu
# makefile (from 'Makefile', line 1)
MAKEFILE_LIST := Makefile

# automatic
@F = $(notdir $@)
# environment
VSCODE_PID = 5099
VSCODE_PID = 9684
# environment
XDG_SESSION_TYPE = x11
# automatic
?D = $(patsubst %/,%,$(dir $?))
# makefile (from 'Makefile', line 8)
PACKAGE = p2p-noise
# environment
SESSION_MANAGER = local/gmena-ThinkPad-P70:@/tmp/.ICE-unix/2791,unix/gmena-ThinkPad-P70:/tmp/.ICE-unix/2791
SESSION_MANAGER = local/gmena-ThinkPad-P70:@/tmp/.ICE-unix/3633,unix/gmena-ThinkPad-P70:/tmp/.ICE-unix/3633
# automatic
*F = $(notdir $*)
# environment
Expand All @@ -123,7 +123,7 @@ DBUS_SESSION_BUS_ADDRESS = unix:path=/run/user/1000/bus
# automatic
<D = $(patsubst %/,%,$(dir $<))
# environment
VSCODE_NLS_CONFIG = {"locale":"en-us","availableLanguages":{},"_languagePackSupport":true}
VSCODE_NLS_CONFIG = {"locale":"en-us","osLocale":"en-us","availableLanguages":{},"_languagePackSupport":true}
# default
MAKE_HOST := x86_64-pc-linux-gnu
# environment
Expand All @@ -145,7 +145,7 @@ MAKELEVEL := 0
# default
MAKE = $(MAKE_COMMAND)
# environment
PATH = /home/gmena/sdk/go1.19/bin:/home/gmena/go/bin:/bin:/home/gmena/.cargo/bin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/gmena/.local/bin:/home/gmena/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/usr/local/go/bin:/bin:/home/gmena/.local/share/JetBrains/Toolbox/scripts:/home/gmena/.local/share/JetBrains/Toolbox/scripts
PATH = /home/gmena/sdk/go1.19/bin:/home/gmena/go/bin:/bin:/home/gmena/.cargo/bin:/home/linuxbrew/.linuxbrew/bin:/home/linuxbrew/.linuxbrew/sbin:/home/gmena/.local/bin:/home/gmena/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin:/snap/bin:/usr/local/go/bin:/bin
# default
MAKEFILES :=
# makefile (from 'Makefile', line 23)
Expand All @@ -161,7 +161,7 @@ HOMEBREW_SHELLENV_PREFIX = /home/linuxbrew/.linuxbrew
# makefile (from 'Makefile', line 15)
BINARY_OSX = ${BINARY}-darwin
# environment
INVOCATION_ID = e7b686228df84a218763228e23d8dbd2
INVOCATION_ID = 30284db4cb9a405b84b51eabd2a43608
# makefile (from 'Makefile', line 16)
BINARY_LINUX = ${BINARY}-linux
# environment
Expand All @@ -171,6 +171,8 @@ LC_TELEPHONE = es_NI.UTF-8
# automatic
?F = $(notdir $?)
# environment
GTK_IM_MODULE = ibus
# environment
XDG_CURRENT_DESKTOP = Unity
# makefile (from 'Makefile', line 18)
ARCH_64 = amd64
Expand All @@ -189,7 +191,7 @@ GIO_LAUNCHED_DESKTOP_FILE = /usr/share/applications/code.desktop
# makefile
.DEFAULT_GOAL := test
# environment
SYSTEMD_EXEC_PID = 2813
SYSTEMD_EXEC_PID = 3667
# environment
DISPLAY = :0
# makefile (from 'Makefile', line 7)
Expand All @@ -203,17 +205,16 @@ LINUX_32 = ${BINARY_LINUX}-${ARCH_32}
# makefile (from 'Makefile', line 24)
WIN_32 = ${BINARY_WIN}-${ARCH_32}
# environment
MANAGERPID = 2062
MANAGERPID = 2510
# environment
HOMEBREW_CELLAR = /home/linuxbrew/.linuxbrew/Cellar
# makefile (from 'Makefile', line 14)
BINARY_WIN = ${BINARY}-win
# environment
LC_MEASUREMENT = es_NI.UTF-8
# environment
GIO_LAUNCHED_DESKTOP_FILE_PID = 5099
GIO_LAUNCHED_DESKTOP_FILE_PID = 9684
# makefile (from 'Makefile', line 25)

OSX_64 = ${BINARY_LINUX}-${ARCH_64}
# environment
_ = /usr/share/code/code
Expand All @@ -224,7 +225,7 @@ XDG_RUNTIME_DIR = /run/user/1000
# environment
GPG_AGENT_INFO = /run/user/1000/gnupg/S.gpg-agent:0:1
# environment
JOURNAL_STREAM = 8:38942
JOURNAL_STREAM = 8:34795
# environment
GOMODCACHE = /home/gmena/go/pkg/mod
# environment
Expand All @@ -238,7 +239,7 @@ QT_IM_MODULE = ibus
# environment
ELECTRON_RUN_AS_NODE = 1
# environment
VSCODE_IPC_HOOK = /run/user/1000/vscode-114bb07b-1.74.3-main.sock
VSCODE_IPC_HOOK = /run/user/1000/vscode-114bb07b-1.81-main.sock
# environment
IM_CONFIG_PHASE = 1
# makefile (from 'Makefile', line 117)
Expand All @@ -258,7 +259,7 @@ GDMSESSION = ubuntu
# environment
LC_IDENTIFICATION = es_NI.UTF-8
# variable set hash-table stats:
# Load=122/1024=12%, Rehash=0, Collisions=11/157=7%
# Load=124/1024=12%, Rehash=0, Collisions=11/159=7%

# Pattern-specific Variable Values

Expand All @@ -277,10 +278,10 @@ LC_IDENTIFICATION = es_NI.UTF-8
# Files

update-pkg-cache:

# Implicit rule search has not been done.
# Modification time never checked.
# File has not been updated.


compile-linux:
# Implicit rule search has not been done.
Expand Down Expand Up @@ -323,7 +324,7 @@ benchmark:
# Not a target:
Makefile:
# Implicit rule search has been done.
# Last modified 2022-12-27 12:47:29.840830435
# Last modified 2023-05-05 12:51:06.247074397
# File has been updated.
# Successfully updated.

Expand All @@ -340,7 +341,6 @@ compile-win:
# Modification time never checked.
# File has not been updated.
# recipe to execute (from 'Makefile', line 94):

@GOOS=windows GOARCH=amd64 go build -o bin/${WIN_64} ${INPUT}
@GOOS=windows GOARCH=386 go build -o bin/${WIN_32} ${INPUT}

Expand Down Expand Up @@ -413,7 +413,6 @@ build:
# File has been updated.
# Needs to be updated (-q is set).
# automatic

# @ := build
# automatic
# * :=
Expand Down Expand Up @@ -492,6 +491,6 @@ vendorize:
# strcache performance: lookups = 97 / hit rate = 36%
# hash-table stats:
# Load=62/8192=1%, Rehash=0, Collisions=0/97=0%
# Finished Make data base on Sat Feb 4 16:13:37 2023
# Finished Make data base on Sat Aug 12 08:13:38 2023


42 changes: 32 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ OSX_64=${BINARY_LINUX}-${ARCH_64}
# -count 1 idiomatic no cached testing
# -race test race condition for routines
# @ = dont echo the output
.PHONY: test ## run tests
test:
@go test -v ./... -count 1 -race -covermode=atomic
@echo "[OK] test finished"
Expand All @@ -36,8 +37,9 @@ test:
# make benchmark > a.old
# make benchmark > b.new
# benchcmp a.old b.new
.PHONY: benchmark ## run benchmark tests
benchmark:
@go test ./... -bench=. -benchtime 100000x -count 5
@perflock -governor=80% go test -run=^Benchmarck$ -benchtime 1s -bench=. -count=1
@echo "[OK] benchmark finished"


Expand All @@ -54,71 +56,91 @@ benchmark:
# For fancy visualization:
# Could use Graphviz (https://graphviz.org/download/)
# eg. go tool pprof -web bin/main-linux-amd64 cpu.prof

.PHONY: profiling ## run profiling tests
profiling:
@go test -bench=. -benchtime 100000x -run=^$ -cpuprofile=cpu.prof -memprofile=prof.mem
@perflock -governor=80% go test -run=^Benchmarck$ -benchmem -benchtime 1s -bench=. -cpu 1,2,4,8 -count=1 -memprofile mem.prof -cpuprofile cpu.prof
@echo "[OK] profiling finished"

.PHONY: coverage ## run tests coverage
coverage:
@go test -v ./... -race -covermode=atomic -coverprofile coverage ./...
@echo "[OK] coverage finished"


.PHONY: coverage-export ## run tests coverage export
coverage-export: coverage
@go tool cover -html=coverage
@echo "[OK] code test coverage finished"

# Allow to preview documentation.
# Please verify your GOPATH before run this command
.PHONY: preview-doc ## run local documentation server
preview-doc:
@godoc -http=localhost:6060 -links=true

.PHONY: build ## compiles the command into and executable
build:
@go build -v ./...

code-fmt:
.PHONY: format ## automatically formats Go source cod
format:
@go fmt ./...
@goimports -w .
@echo "[OK] code format finished"

code-check:
.PHONY: check ## examines Go source code and reports suspicious constructs
check:
@go vet -v ./...
@echo "[OK] code check finished"

.PHONY: clean ## removes generated files and clean go cache
clean:
@go clean --cache ./...
@rm -f mem.prof
@rm -f prof.mem
@rm -rf bin
@echo "[OK] cleaned"

.PHONY: compile-win ## compiles window exec
compile-win:
@GOOS=windows GOARCH=amd64 go build -o bin/${WIN_64} ${INPUT}
@GOOS=windows GOARCH=386 go build -o bin/${WIN_32} ${INPUT}

#Go1.15 deprecates 32-bit macOS builds
# go build -x to show compilation details
#GOOS=darwin GOARCH=386 go build -o bin/main-mac-386 main.go
.PHONY: compile-mac ## compiles mac exec
compile-mac:
@GOOS=darwin GOARCH=amd64 go build -o bin/${OSX_64} ${INPUT}

.PHONY: compile-linux ## compiles linux exec
compile-linux:
@GOOS=linux GOARCH=amd64 go build -o bin/${LINUX_64} ${INPUT}
@GOOS=linux GOARCH=386 go build -o bin/${LINUX_32} ${INPUT}

.PHONY: compile ## compiles all os exec
compile: compile-linux compile-win compile-mac
@echo "[OK] Compiling for every OS and Platform"

build-gc:
@go build -gcflags='-m -m' $(filter-out $@,$(MAKECMDGOALS))

.PHONY: run ## compiles and runs the named main Go package
run:
@go run ${INPUT} $(filter-out $@,$(MAKECMDGOALS))

.PHONY: update-pkg-cache ## updated the package cache version
update-pkg-cache:
GOPROXY=https://proxy.golang.org GO111MODULE=on \
go get github.com/${USER}/${PACKAGE}@v${VERSION}

# https://go.dev/ref/mod#go-mod-vendor
.PHONY: vendorize ## lock dependencies
vendorize:
@go mod vendor
@echo "[OK]"

all: build test check-test-coverage code-check compile
all: build test check-test-coverage code-check compile

.PHONY: help ## display this message
help:
@grep -E \
'^.PHONY: .*?## .*$$' $(MAKEFILE_LIST) | \
sort | \
awk 'BEGIN {FS = ".PHONY: |## "}; {printf "\033[36m%-19s\033[0m %s\n", $$2, $$3}'
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,21 @@ func main() {
Using [perflock](https://github.com/aclements/perflock) to prevent our benchmarks from using too much CPU at once.

```text
perflock -governor=80% go test -benchmem -run=^$ -benchtime 1s -bench=BenchmarkHandshakeProfile -cpu 1,2,4,8 -count 2 -memprofile mem.prof -cpuprofile cpu.prof
perflock -governor=80% go test -benchmem -run=^$ -benchtime 1s -bench=. -cpu 1,2,4,8 -count=1
goos: linux
goarch: amd64
pkg: github.com/geolffreym/p2p-noise
cpu: Intel(R) Xeon(R) CPU E3-1505M v5 @ 2.80GHz
BenchmarkHandshakeProfile 962 1061028 ns/op 34391 B/op 267 allocs/op
BenchmarkHandshakeProfile 1322 919647 ns/op 34383 B/op 267 allocs/op
BenchmarkHandshakeProfile-2 1294 834506 ns/op 37095 B/op 285 allocs/op
BenchmarkHandshakeProfile-2 1398 858845 ns/op 36872 B/op 284 allocs/op
BenchmarkHandshakeProfile-4 1395 875618 ns/op 41912 B/op 323 allocs/op
BenchmarkHandshakeProfile-4 1341 914046 ns/op 41858 B/op 323 allocs/op
BenchmarkHandshakeProfile-8 1276 879535 ns/op 42055 B/op 324 allocs/op
BenchmarkHandshakeProfile-8 1279 929125 ns/op 41812 B/op 323 allocs/op
BenchmarkHandshakeProfile 726 1575256 ns/op 46959 B/op 363 allocs/op
BenchmarkHandshakeProfile-2 1548 1037351 ns/op 47100 B/op 364 allocs/op
BenchmarkHandshakeProfile-4 2460 908573 ns/op 49885 B/op 383 allocs/op
BenchmarkHandshakeProfile-8 2127 736442 ns/op 60454 B/op 457 allocs/op
BenchmarkNodesSecureMessageExchange 29032570 35.03 ns/op 0 B/op 0 allocs/op
BenchmarkNodesSecureMessageExchange-2 59745247 16.78 ns/op 0 B/op 0 allocs/op
BenchmarkNodesSecureMessageExchange-4 124446950 9.454 ns/op 0 B/op 0 allocs/op
BenchmarkNodesSecureMessageExchange-8 151214516 7.088 ns/op 0 B/op 0 allocs/op
PASS
ok github.com/geolffreym/p2p-noise 13.201s
ok github.com/geolffreym/p2p-noise 18.865s
```

Expand All @@ -107,12 +107,12 @@ Some available capabilities for dev support:
* **Test Coverage**: `make coverage`
* **Benchmark**: `make benchmark`
* **Profiling**: `make profiling`
* **Code check**: `make code-check`
* **Code format**: `make code-fmt`
* **Code check**: `make check`
* **Code format**: `make format`
* **Flush cache**: `make clean`
* **Build**: `make build`

Note: Please check [Makefile](https://github.com/geolffreym/p2p-noise/Makefile) for more capabilities.
Note: Run `make help` to check for more capabilities.

## More info

Expand Down
4 changes: 3 additions & 1 deletion config/config.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Package conf provide a "functional option" design pattern to handle node settings.
// See also: https://github.com/crazybber/awesome-patterns/blob/master/idiom/functional-options.md
// See more about [Functional Options].
//
// [Functional Options]: https://github.com/crazybber/awesome-patterns/blob/master/idiom/functional-options.md
package config

import "time"
Expand Down
4 changes: 2 additions & 2 deletions handshake.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,9 @@ func newHandshake(conn net.Conn, initiator bool) (*handshake, error) {
// Setup the max of size possible for tokens exchanged between peers.
edKeyLen := ed25519.PublicKeySize // 32 bytes
dhKeyLen := 2 * noise.DH25519.DHLen() // 64 bytes
cypherLen := 2 * chacha20poly1305.Overhead // 32 bytes
cipherLen := 2 * chacha20poly1305.Overhead // 32 bytes
// Sum the needed memory size for pool
size := dhKeyLen + edKeyLen + cypherLen + headerSize
size := dhKeyLen + edKeyLen + cipherLen + headerSize
pool := bpool.NewBytePool(bPools, size) // N bytes pool

// Create a new session handler
Expand Down
Loading

0 comments on commit 9408d13

Please sign in to comment.