Skip to content

Commit

Permalink
Benchmark memory (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
tayloraswift authored Dec 8, 2024
1 parent 0ef3a81 commit 6a01526
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 21 deletions.
59 changes: 59 additions & 0 deletions .github/workflows/Benchmark.yml
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions Benchmarks/Memory/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM swift:6.0.2-noble

RUN apt update
RUN apt -y install libjemalloc2 curl gzip
39 changes: 20 additions & 19 deletions Snippets/FirewallUsage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
""")
}
5 changes: 3 additions & 2 deletions Sources/FirewallPrefabricator/Main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}

0 comments on commit 6a01526

Please sign in to comment.