Skip to content

Commit

Permalink
feat: Integrate with hdr histogram (#54)
Browse files Browse the repository at this point in the history
## Description

This fixes #40

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. 

## Minimal checklist:

- [ ] I have performed a self-review of my own code 
- [ ] I have added `DocC` code-level documentation for any public
interfaces exported by the package
- [ ] I have added unit and/or integration tests that prove my fix is
effective or that my feature works
  • Loading branch information
dimlio committed Feb 13, 2023
1 parent 9670dce commit 2995b2c
Show file tree
Hide file tree
Showing 9 changed files with 258 additions and 440 deletions.
93 changes: 93 additions & 0 deletions Benchmarks/Histogram/HistogramBenchmark.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
//
// Copyright (c) 2022 Ordo One AB.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
//
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
import BenchmarkSupport
import Histogram

@main
extension BenchmarkRunner {}

// swiftlint disable: attributes
@_dynamicReplacement(for: registerBenchmarks)
func benchmarks() {
Benchmark.defaultDesiredDuration = .seconds(1)
Benchmark.defaultDesiredIterations = .giga(1)

Benchmark("Record",
metrics: [.wallClock, .throughput] + BenchmarkMetric.memory,
skip: false) { benchmark in
let maxValue: UInt64 = 1_000_000

var histogram = Histogram<UInt64>(highestTrackableValue: maxValue, numberOfSignificantValueDigits: .three)

let numValues = 1024 // so compiler can optimize modulo below
let values = [UInt64]((0..<numValues).map { _ in UInt64.random(in: 100...1000) })

benchmark.startMeasurement()

for i in benchmark.throughputIterations {
histogram.record(values[i % numValues])
}
}

Benchmark("Record to autoresizing",
metrics: [.wallClock, .throughput] + BenchmarkMetric.memory,
skip: false) { benchmark in
var histogram = Histogram<UInt64>(numberOfSignificantValueDigits: .three)

let numValues = 1024 // so compiler can optimize modulo below
let values = [UInt64]((0..<numValues).map { _ in UInt64.random(in: 100...1000) })

benchmark.startMeasurement()

for i in benchmark.throughputIterations {
histogram.record(values[i % numValues])
}
}

Benchmark("ValueAtPercentile",
metrics: [.wallClock, .throughput],
skip: false) { benchmark in
let maxValue: UInt64 = 1_000_000

var histogram = Histogram<UInt64>(highestTrackableValue: maxValue, numberOfSignificantValueDigits: .three)

// fill histogram with some data
for _ in 0 ..< 10_000 {
histogram.record(UInt64.random(in: 10...1000))
}

let percentiles = [ 0.0, 25.0, 50.0, 75.0, 80.0, 90.0, 99.0, 100.0 ]

benchmark.startMeasurement()

for i in benchmark.throughputIterations {
blackHole(histogram.valueAtPercentile(percentiles[i % percentiles.count]))
}
}

Benchmark("Mean",
metrics: [.wallClock, .throughput],
skip: false) { benchmark in
let maxValue: UInt64 = 1_000_000

var histogram = Histogram<UInt64>(highestTrackableValue: maxValue, numberOfSignificantValueDigits: .three)

// fill histogram with some data
for _ in 0 ..< 10_000 {
histogram.record(UInt64.random(in: 10...1000))
}

benchmark.startMeasurement()

for _ in benchmark.throughputIterations {
blackHole(histogram.mean)
}
}
}
9 changes: 9 additions & 0 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
"version" : "0.0.4"
}
},
{
"identity" : "package-histogram",
"kind" : "remoteSourceControl",
"location" : "https://github.com/ordo-one/package-histogram",
"state" : {
"revision" : "657b68ad2a56de72216845263d0cb05135061ea0",
"version" : "0.0.1"
}
},
{
"identity" : "package-jemalloc",
"kind" : "remoteSourceControl",
Expand Down
14 changes: 13 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ let package = Package(
.package(url: "https://github.com/ordo-one/TextTable", .upToNextMajor(from: "0.0.1")),
.package(url: "https://github.com/ordo-one/package-jemalloc", .upToNextMajor(from: "1.0.0")),
.package(url: "https://github.com/ordo-one/package-datetime", .upToNextMajor(from: "0.0.0")),
.package(url: "https://github.com/ordo-one/package-histogram", .upToNextMajor(from: "0.0.1")),
],
targets: [
// Plugin used by users of the package
Expand Down Expand Up @@ -56,7 +57,8 @@ let package = Package(
name: "Statistics",
dependencies: [
.product(name: "Numerics", package: "swift-numerics"),
.product(name: "DateTime", package: "package-datetime")
.product(name: "DateTime", package: "package-datetime"),
.product(name: "Histogram", package: "package-histogram"),
]
),

Expand Down Expand Up @@ -102,6 +104,16 @@ let package = Package(
path: "Benchmarks/DateTime"
),

// Benchmark of the Histogram package
.executableTarget(
name: "HistogramBenchmark",
dependencies: [
"BenchmarkSupport",
.product(name: "Histogram", package: "package-histogram"),
],
path: "Benchmarks/Histogram"
),

// Scaffolding to support benchmarks under the hood
.target(
name: "BenchmarkSupport",
Expand Down

0 comments on commit 2995b2c

Please sign in to comment.