@@ -26,22 +26,28 @@ let benchmarks: @Sendable () -> Void = {
2626
2727 thresholdTolerances = [ . wallClock: . relaxed]
2828
29- Benchmark . defaultConfiguration = . init( warmupIterations: 0 ,
30- maxDuration: . seconds( 1 ) ,
31- maxIterations: Int . max,
32- thresholds: thresholdTolerances)
29+ Benchmark . defaultConfiguration = . init(
30+ warmupIterations: 0 ,
31+ maxDuration: . seconds( 1 ) ,
32+ maxIterations: Int . max,
33+ thresholds: thresholdTolerances
34+ )
3335
3436 testSetUpTearDown ( )
3537
3638 // A way to define custom metrics fairly compact
3739 enum CustomMetrics {
3840 static var one : BenchmarkMetric { . custom( " CustomMetricOne " ) }
3941 static var two : BenchmarkMetric { . custom( " CustomMetricTwo " , polarity: . prefersLarger, useScalingFactor: true ) }
40- static var three : BenchmarkMetric { . custom( " CustomMetricThree " , polarity: . prefersLarger, useScalingFactor: false ) }
42+ static var three : BenchmarkMetric {
43+ . custom( " CustomMetricThree " , polarity: . prefersLarger, useScalingFactor: false )
44+ }
4145 }
4246
43- Benchmark ( " Basic " ,
44- configuration: . init( metrics: [ . wallClock, . throughput, . instructions] ) ) { _ in
47+ Benchmark (
48+ " Basic " ,
49+ configuration: . init( metrics: [ . wallClock, . throughput, . instructions] )
50+ ) { _ in
4551 }
4652
4753 Benchmark ( " Noop " , configuration: . init( metrics: [ . wallClock, . mallocCountTotal, . instructions] ) ) { _ in
@@ -50,51 +56,70 @@ let benchmarks: @Sendable () -> Void = {
5056 Benchmark ( " Noop2 " , configuration: . init( metrics: [ . wallClock, . instructions] + . arc) ) { _ in
5157 }
5258
53- Benchmark ( " Scaled metrics One " ,
54- configuration: . init( metrics: . all + [ CustomMetrics . two, CustomMetrics . one] ,
55- scalingFactor: . one) ) { benchmark in
59+ Benchmark (
60+ " Scaled metrics One " ,
61+ configuration: . init(
62+ metrics: . all + [ CustomMetrics . two, CustomMetrics . one] ,
63+ scalingFactor: . one
64+ )
65+ ) { benchmark in
5666 for _ in benchmark. scaledIterations {
57- blackHole ( Int . random ( in: 1 ... 1_000 ) )
67+ blackHole ( Int . random ( in: 1 ... 1_000 ) )
5868 }
59- benchmark. measurement ( CustomMetrics . two, Int . random ( in: 1 ... 1_000_000 ) )
60- benchmark. measurement ( CustomMetrics . one, Int . random ( in: 1 ... 1_000 ) )
69+ benchmark. measurement ( CustomMetrics . two, Int . random ( in: 1 ... 1_000_000 ) )
70+ benchmark. measurement ( CustomMetrics . one, Int . random ( in: 1 ... 1_000 ) )
6171 }
6272
63- Benchmark ( " Scaled metrics K " ,
64- configuration: . init( metrics: . all + [ CustomMetrics . two, CustomMetrics . one] ,
65- scalingFactor: . kilo) ) { benchmark in
73+ Benchmark (
74+ " Scaled metrics K " ,
75+ configuration: . init(
76+ metrics: . all + [ CustomMetrics . two, CustomMetrics . one] ,
77+ scalingFactor: . kilo
78+ )
79+ ) { benchmark in
6680 for _ in benchmark. scaledIterations {
67- blackHole ( Int . random ( in: 1 ... 1_000 ) )
81+ blackHole ( Int . random ( in: 1 ... 1_000 ) )
6882 }
69- benchmark. measurement ( CustomMetrics . two, Int . random ( in: 1 ... 1_000_000 ) )
70- benchmark. measurement ( CustomMetrics . one, Int . random ( in: 1 ... 1_000 ) )
83+ benchmark. measurement ( CustomMetrics . two, Int . random ( in: 1 ... 1_000_000 ) )
84+ benchmark. measurement ( CustomMetrics . one, Int . random ( in: 1 ... 1_000 ) )
7185 }
7286
73- Benchmark ( " Scaled metrics M " ,
74- configuration: . init( metrics: . all + [ CustomMetrics . two, CustomMetrics . one, CustomMetrics . three] ,
75- scalingFactor: . mega) ) { benchmark in
87+ Benchmark (
88+ " Scaled metrics M " ,
89+ configuration: . init(
90+ metrics: . all + [ CustomMetrics . two, CustomMetrics . one, CustomMetrics . three] ,
91+ scalingFactor: . mega
92+ )
93+ ) { benchmark in
7694 for _ in benchmark. scaledIterations {
7795 blackHole ( Int . random ( in: benchmark. scaledIterations) )
7896 }
79- benchmark. measurement ( CustomMetrics . three, Int . random ( in: 1 ... 1_000_000_000 ) )
80- benchmark. measurement ( CustomMetrics . two, Int . random ( in: 1 ... 1_000_000 ) )
81- benchmark. measurement ( CustomMetrics . one, Int . random ( in: 1 ... 1_000 ) )
97+ benchmark. measurement ( CustomMetrics . three, Int . random ( in: 1 ... 1_000_000_000 ) )
98+ benchmark. measurement ( CustomMetrics . two, Int . random ( in: 1 ... 1_000_000 ) )
99+ benchmark. measurement ( CustomMetrics . one, Int . random ( in: 1 ... 1_000 ) )
82100 }
83101
84- Benchmark ( " All metrics " ,
85- configuration: . init( metrics: . all, skip: true ) ) { _ in
102+ Benchmark (
103+ " All metrics " ,
104+ configuration: . init( metrics: . all, skip: true )
105+ ) { _ in
86106 }
87107
88108 let stats = Statistics ( numberOfSignificantDigits: . four)
89109 let measurementCount = 8_340
90110
91- for measurement in ( 0 ..< measurementCount) . reversed ( ) {
111+ for measurement in ( 0 ..< measurementCount) . reversed ( ) {
92112 stats. add ( measurement)
93113 }
94114
95- Benchmark ( " Statistics " ,
96- configuration: . init( metrics: . arc + [ . wallClock] ,
97- scalingFactor: . kilo, maxDuration: . seconds( 1 ) ) ) { benchmark in
115+ Benchmark (
116+ " Statistics " ,
117+ configuration: . init(
118+ metrics: . arc + [ . wallClock] ,
119+ scalingFactor: . kilo,
120+ maxDuration: . seconds( 1 )
121+ )
122+ ) { benchmark in
98123 for _ in benchmark. scaledIterations {
99124 blackHole ( stats. percentiles ( ) )
100125 }
@@ -104,7 +129,7 @@ let benchmarks: @Sendable () -> Void = {
104129
105130 parameterization. forEach { count in
106131 Benchmark ( " Parameterized " , configuration: . init( tags: [ " count " : count. description] ) ) { benchmark in
107- for _ in 0 ..< count {
132+ for _ in 0 ..< count {
108133 blackHole ( Int . random ( in: benchmark. scaledIterations) )
109134 }
110135 }
@@ -115,37 +140,46 @@ let benchmarks: @Sendable () -> Void = {
115140
116141 @Sendable
117142 func dummyCounter( _ count: Int ) {
118- for index in 0 ..< count {
143+ for index in 0 ..< count {
119144 blackHole ( index)
120145 }
121146 }
122147
123148 func concurrentWork( tasks: Int = 4 , mallocs: Int = 0 ) async {
124- _ = await withTaskGroup ( of: Void . self, returning: Void . self, body: { taskGroup in
125- for _ in 0 ..< tasks {
126- taskGroup. addTask {
127- dummyCounter ( defaultCounter ( ) * 1_000 )
128- for _ in 0 ..< mallocs {
129- let something = malloc ( 1_024 * 1_024 )
130- blackHole ( something)
131- free ( something)
132- }
133- if let fileHandle = FileHandle ( forWritingAtPath: " /dev/null " ) {
134- let data = Data ( " Data to discard " . utf8)
135- fileHandle. write ( data)
136- fileHandle. closeFile ( )
149+ _ = await withTaskGroup (
150+ of: Void . self,
151+ returning: Void . self,
152+ body: { taskGroup in
153+ for _ in 0 ..< tasks {
154+ taskGroup. addTask {
155+ dummyCounter ( defaultCounter ( ) * 1_000 )
156+ for _ in 0 ..< mallocs {
157+ let something = malloc ( 1_024 * 1_024 )
158+ blackHole ( something)
159+ free ( something)
160+ }
161+ if let fileHandle = FileHandle ( forWritingAtPath: " /dev/null " ) {
162+ let data = Data ( " Data to discard " . utf8)
163+ fileHandle. write ( data)
164+ fileHandle. closeFile ( )
165+ }
137166 }
138167 }
139- }
140168
141- for await _ in taskGroup { }
142- } )
169+ for await _ in taskGroup { }
170+ }
171+ )
143172 }
144173
145- Benchmark ( " InstructionCount " , configuration: . init( metrics: [ . instructions] ,
146- warmupIterations: 0 ,
147- scalingFactor: . kilo,
148- thresholds: [ . instructions: . relaxed] ) ) { _ in
174+ Benchmark (
175+ " InstructionCount " ,
176+ configuration: . init(
177+ metrics: [ . instructions] ,
178+ warmupIterations: 0 ,
179+ scalingFactor: . kilo,
180+ thresholds: [ . instructions: . relaxed]
181+ )
182+ ) { _ in
149183 await concurrentWork ( tasks: 15 , mallocs: 1_000 )
150184 }
151185}
0 commit comments