diff --git a/.github/workflows/pull-request.yaml b/.github/workflows/pull-request.yaml index 35fa9a6..f94c075 100644 --- a/.github/workflows/pull-request.yaml +++ b/.github/workflows/pull-request.yaml @@ -20,6 +20,8 @@ jobs: - run: sudo xcode-select -s /Applications/Xcode_26.0.app - run: swift --version - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 + - name: Install jemalloc + run: brew install jemalloc - run: make all - name: make fmt lint test (ComplianceSuite) run: make fmt lint test diff --git a/Benchmarks/Package.swift b/Benchmarks/Package.swift new file mode 100644 index 0000000..4733931 --- /dev/null +++ b/Benchmarks/Package.swift @@ -0,0 +1,30 @@ +// swift-tools-version: 6.0 + +import PackageDescription + +let package = Package( + name: "swift-opa-benchmarks", + platforms: [ + .macOS(.v13), + ], + dependencies: [ + .package(name: "swift-opa", path: ".."), + .package(url: "https://github.com/ordo-one/package-benchmark", from: "1.4.0"), + ], + targets: [ + .executableTarget( + name: "RegoBenchmarks", + dependencies: [ + .product(name: "SwiftOPA", package: "swift-opa"), + .product(name: "Benchmark", package: "package-benchmark"), + ], + path: "Sources", + swiftSettings: [ + .enableExperimentalFeature("AccessLevelOnImport") + ], + plugins: [ + .plugin(name: "BenchmarkPlugin", package: "package-benchmark") + ] + ), + ] +) \ No newline at end of file diff --git a/Benchmarks/Sources/RegoBenchmarks.swift b/Benchmarks/Sources/RegoBenchmarks.swift new file mode 100644 index 0000000..fcdbfed --- /dev/null +++ b/Benchmarks/Sources/RegoBenchmarks.swift @@ -0,0 +1,28 @@ +import AST +import Benchmark +import Foundation +internal import Rego + +let benchmarks: @Sendable () -> Void = { + Benchmark( + "Simple Policy Evaluation", + configuration: .init(metrics: [.wallClock, .mallocCountTotal]) + ) { benchmark in + // Setup OPA engine + var engine = OPA.Engine( + bundlePaths: [OPA.Engine.BundlePath(name: "simple", url: URL(fileURLWithPath: "../Tests/RegoTests/TestData/Bundles/simple-directory-bundle"))]) + let preparedQuery = try! await engine.prepareForEvaluation(query: "data.app.rbac.allow") + + let input: AST.RegoValue = [ + "user": "alice", + "action": "read", + "resource": "document123", + ] + + benchmark.startMeasurement() + for _ in benchmark.scaledIterations { + let result = try! await preparedQuery.evaluate(input: input) + blackHole(result) + } + } +} \ No newline at end of file diff --git a/Makefile b/Makefile index ed1a32f..bc02cad 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,10 @@ test: test-compliance: $(MAKE) -C ComplianceSuite test-compliance +.PHONY: perf +perf: + cd Benchmarks && swift package benchmark + .PHONY: build build: swift build