diff --git a/.github/workflows/Benchmark.yml b/.github/workflows/Benchmark.yml new file mode 100644 index 0000000..b66a9dc --- /dev/null +++ b/.github/workflows/Benchmark.yml @@ -0,0 +1,59 @@ +name: benchmark + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + linux: + runs-on: ubuntu-24.04 + name: memory consumption + steps: + - name: Checkout repository + uses: actions/checkout@v3 + + - name: Build environment + run: | + docker build -f Benchmarks/Memory/Dockerfile -t benchmark . + + - name: Build test application + run: | + docker run --rm -v=$PWD:/swift -w=/swift \ + benchmark \ + swift build -c release --product FirewallUsage + + - name: Build firewall + run: | + docker run --rm -v=$PWD:/swift -w=/swift \ + -e IPINFO_TOKEN=${{ secrets.IPINFO_TOKEN }} \ + benchmark \ + Scripts/Package + + - name: Run test application (300MB memory limit) + run: | + docker run --rm -v=$PWD:/swift -w=/swift \ + --memory-swap 300m \ + --memory 300m \ + -e LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 \ + benchmark \ + .build/release/FirewallUsage 66.249.66.169 + + - name: Run test application (200MB memory limit) + run: | + docker run --rm -v=$PWD:/swift -w=/swift \ + --memory-swap 200m \ + --memory 200m \ + -e LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 \ + benchmark \ + .build/release/FirewallUsage 66.249.66.169 + + - name: Run test application (160MB memory limit) + run: | + docker run --rm -v=$PWD:/swift -w=/swift \ + --memory-swap 160m \ + --memory 160m \ + -e LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 \ + benchmark \ + .build/release/FirewallUsage 66.249.66.169 diff --git a/Benchmarks/Memory/Dockerfile b/Benchmarks/Memory/Dockerfile new file mode 100644 index 0000000..c762671 --- /dev/null +++ b/Benchmarks/Memory/Dockerfile @@ -0,0 +1,4 @@ +FROM swift:6.0.2-noble + +RUN apt update +RUN apt -y install libjemalloc2 curl gzip diff --git a/Snippets/FirewallUsage.swift b/Snippets/FirewallUsage.swift index 879b422..a4b9f84 100644 --- a/Snippets/FirewallUsage.swift +++ b/Snippets/FirewallUsage.swift @@ -30,33 +30,34 @@ else fatalError("Invalid IP address") } +// snippet.LOAD_FIREWALL let data:Data = try .init(contentsOf: URL.init(fileURLWithPath: "firewall.bson")) let bson:BSON.Document = .init(bytes: [UInt8].init(data)[...]) let firewall:IP.Firewall = .load(from: try IP.Firewall.Image.init(bson: bson)) -guard -let country:ISO.Country = firewall.country[v6: ip] -else +// snippet.LOOKUP_COUNTRY +let country:ISO.Country? = firewall.country[v6: ip] +// snippet.end +if let country:ISO.Country { - fatalError("No Country found for \(ip)") + print("Country: \(country)") } -print(""" - Address: \(ip) - Country: \(country) - """) - -let (system, _):(IP.AS?, IP.Claimant?) = firewall.lookup(v6: ip) - -guard -let system:IP.AS -else +// snippet.LOOKUP_ASN +let (system, claimant):(IP.AS?, IP.Claimant?) = firewall.lookup(v6: ip) +// snippet.end +if let system:IP.AS { - fatalError("No ASN found for \(ip)") + print(""" + ASN: \(system.number) + AS: \(system.domain) (\(system.name)) + """) } -print(""" - ASN: \(system.number) - AS: \(system.domain) (\(system.name)) - """) +if let claimant:IP.Claimant +{ + print(""" + Claimant: \(claimant) + """) +} diff --git a/Sources/FirewallPrefabricator/Main.swift b/Sources/FirewallPrefabricator/Main.swift index f6eb3cc..b218f4d 100644 --- a/Sources/FirewallPrefabricator/Main.swift +++ b/Sources/FirewallPrefabricator/Main.swift @@ -4,8 +4,6 @@ import FoundationEssentials import Foundation #endif -// $ docker run --rm -it -p 8443:8443 --network=unidoc-test --memory 20m -v ~/swift:/swift -e LD_PRELOAD=/usr/lib/x86_64-linux-gnu/libjemalloc.so.2 -w /swift/swiftinit tayloraswift/ubuntu:latest /bin/bash - import ArgumentParser import BSON import Firewalls @@ -82,6 +80,9 @@ extension Main try image.colorByClaimant(claims) let bson:BSON.Document = .init(encoding: image) + + print("Successfully built firewall image (\(bson.bytes.count / 1_000) KB)") + try Data.init(bson.bytes).write(to: URL.init(fileURLWithPath: self.output)) } }