# Spreading benchmarks across multiple files?

**URL:** <https://forums.swift.org/t/spreading-benchmarks-across-multiple-files/76070>\
**Category:** Benchmark\
**Tags:** benchmarks\
**Created:** [November 17, 2024, 5:12pm UTC](https://forums.swift.org/t/spreading-benchmarks-across-multiple-files/76070 "2024-11-17T17:12:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joseph\_Heck](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joseph_heck/32/22369_2.png) [@Joseph\_Heck](https://forums.swift.org/u/Joseph_Heck)\
**Post date:** [November 17, 2024, 5:12pm UTC](https://forums.swift.org/t/spreading-benchmarks-across-multiple-files/76070/1 "2024-11-17T17:12:24Z")

</div>

I've been dropping in benchmarks in a single, top-level global for the package-benchmark system to pick them up and run them, basically something akin to:

```swift
let benchmarks = {
 
   Benchmark("SeedHeightmap") { benchmark in
        for _ in benchmark.scaledIterations {
            blackHole(_ = Heightmap(width: 1025, height: 1025, seed: 437_347_632))
        }
    }

    ...
}

```

As I've been iterating, I've added new benchmarks - to the point where I've got quite a few and it's getting a little weird to have them all in a single, long file.

Is there a better pattern here for breaking these declarations up and adding them through multiple files? I didn't see an alternative example illustrated in the [Writing Benchmarks](https://swiftpackageindex.com/ordo-one/package-benchmark/documentation/benchmark/writingbenchmarks) article, and I'm feeling like I'm missing something obvious.

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [January 10, 2025, 9:34am UTC](https://forums.swift.org/t/spreading-benchmarks-across-multiple-files/76070/2 "2025-01-10T09:34:17Z")

</div>

Hi @Joseph_Heck,

Sorry for late reply, bandwidth is a bit low at the moment.

It is simple to do, the obvious approach is added to this sample:

> **[package-benchmark-samples/Benchmarks/MultiFileExample at main ·...](https://github.com/ordo-one/package-benchmark-samples/tree/main/Benchmarks/MultiFileExample)**
>
> A demo project showing sample usage of package-benchmark - ordo-one/package-benchmark-samples

Basically your benchmark file is:

```swift
let benchmarks = {
    addSomeBenchmarks()
    addSomeMoreBenchmarks()
}

```

And then two (or more) separate files with whatever structure you want:

```swift
import Benchmark
import Foundation

func addSomeBenchmarks() {
    Benchmark("SomeBenchmark") { benchmark in
        for _ in benchmark.scaledIterations {
            blackHole(Date())
        }
    }
}

```

```swift
import Benchmark
import Foundation

func addSomeMoreBenchmarks() {
    Benchmark("SomeMoreBenchmark") { benchmark in
        for _ in benchmark.scaledIterations {
            blackHole(Date())
        }
    }
}

```

```bash
> swift package benchmark --target MultiFileExample
...
Building MultiFileExample

==================
Running Benchmarks
==================

100% [------------------------------------------------------------] ETA: 00:00:00 | MultiFileExample:SomeBenchmark
100% [------------------------------------------------------------] ETA: 00:00:00 | MultiFileExample:SomeMoreBenchmark

=====================================================================================================
Baseline 'Current_run'
=====================================================================================================

Host 'ice.local' with 20 'arm64' processors with 128 GB memory, running:
Darwin Kernel Version 24.2.0: Fri Dec 6 19:01:59 PST 2024; root:xnu-11215.61.5~2/RELEASE_ARM64_T6000

================
MultiFileExample
================

SomeBenchmark
╒═══════════════════════════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╕
│ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │
╞═══════════════════════════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╡
│ Instructions * │ 1235 │ 1244 │ 1244 │ 1244 │ 1244 │ 1244 │ 6404 │ 10000 │
├───────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Malloc (total) * │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 10000 │
├───────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Memory (resident peak) (K) │ 9355 │ 9724 │ 9724 │ 9732 │ 9732 │ 9732 │ 9732 │ 10000 │
├───────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Throughput (# / s) (K) │ 1848 │ 1716 │ 1713 │ 1601 │ 1601 │ 1334 │ 79 │ 10000 │
├───────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (total CPU) (ns) * │ 2458 │ 2543 │ 2585 │ 2667 │ 2875 │ 3541 │ 12333 │ 10000 │
├───────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (wall clock) (ns) * │ 541 │ 583 │ 584 │ 625 │ 625 │ 750 │ 12667 │ 10000 │
╘═══════════════════════════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╛

SomeMoreBenchmark
╒═══════════════════════════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╤═════════╕
│ Metric │ p0 │ p25 │ p50 │ p75 │ p90 │ p99 │ p100 │ Samples │
╞═══════════════════════════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╪═════════╡
│ Instructions * │ 1204 │ 1244 │ 1244 │ 1244 │ 1244 │ 1244 │ 10058 │ 10000 │
├───────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Malloc (total) * │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 0 │ 10000 │
├───────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Memory (resident peak) (K) │ 9404 │ 9773 │ 9773 │ 9781 │ 9781 │ 9781 │ 9781 │ 10000 │
├───────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Throughput (# / s) (K) │ 1848 │ 1716 │ 1713 │ 1601 │ 1500 │ 800 │ 78 │ 10000 │
├───────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (total CPU) (ns) * │ 2374 │ 2459 │ 2543 │ 2751 │ 3291 │ 5043 │ 32417 │ 10000 │
├───────────────────────────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┼─────────┤
│ Time (wall clock) (ns) * │ 541 │ 583 │ 584 │ 625 │ 667 │ 1250 │ 12791 │ 10000 │
╘═══════════════════════════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╧═════════╛

```

---

<div class="post-metadata">

**Author:** ![Joseph\_Heck](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/joseph_heck/32/22369_2.png) [@Joseph\_Heck](https://forums.swift.org/u/Joseph_Heck)\
**Post date:** [January 10, 2025, 3:12pm UTC](https://forums.swift.org/t/spreading-benchmarks-across-multiple-files/76070/3 "2025-01-10T15:12:08Z")

</div>

Thank you, quite clear and likely should have been obvious, but I was looking right past it. Appreciate you spelling it out for me.

---

<div class="post-metadata">

**Author:** ![hassila](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hassila/32/22647_2.png) [@hassila](https://forums.swift.org/u/hassila)\
**Post date:** [January 10, 2025, 3:19pm UTC](https://forums.swift.org/t/spreading-benchmarks-across-multiple-files/76070/4 "2025-01-10T15:19:15Z")

</div>

No worries, sorry for late response!
