Skip to content

Commit e57fe21

Browse files
committed
Move RunMode code, Use DSL overloads
1 parent 7fe68a2 commit e57fe21

File tree

3 files changed

+104
-97
lines changed

3 files changed

+104
-97
lines changed

tests/benchmarks/service-benchmarks/jvm/src/aws/sdk/kotlin/benchmarks/service/definitions/CloudwatchProtocolBenchmark.kt

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@ package aws.sdk.kotlin.benchmarks.service.definitions
66

77
import aws.sdk.kotlin.benchmarks.service.Common
88
import aws.sdk.kotlin.services.cloudwatch.CloudWatchClient
9-
import aws.sdk.kotlin.services.cloudwatch.model.*
9+
import aws.sdk.kotlin.services.cloudwatch.getMetricData
10+
import aws.sdk.kotlin.services.cloudwatch.listMetrics
11+
import aws.sdk.kotlin.services.cloudwatch.model.Dimension
12+
import aws.sdk.kotlin.services.cloudwatch.model.Metric
13+
import aws.sdk.kotlin.services.cloudwatch.model.MetricDataQuery
14+
import aws.sdk.kotlin.services.cloudwatch.model.MetricDatum
15+
import aws.sdk.kotlin.services.cloudwatch.putMetricData
1016
import aws.smithy.kotlin.runtime.ExperimentalApi
1117
import aws.smithy.kotlin.runtime.time.Instant
1218
import java.util.*
@@ -18,6 +24,7 @@ class CloudwatchProtocolBenchmark : ServiceProtocolBenchmark<CloudWatchClient> {
1824
companion object {
1925
val suiteId = UUID.randomUUID()
2026
val baseTime = Instant.now() - 2.hours
27+
const val TEST_NAME_SPACE = "SDK Benchmark Test Data"
2128
}
2229

2330
@OptIn(ExperimentalApi::class)
@@ -42,71 +49,65 @@ class CloudwatchProtocolBenchmark : ServiceProtocolBenchmark<CloudWatchClient> {
4249
override val requireScaling = true
4350

4451
override suspend fun transact(client: CloudWatchClient, scale: Int, iteration: Int) {
45-
client.putMetricData(
46-
PutMetricDataRequest {
47-
namespace = "SDK Benchmark Test Data"
48-
metricData = (0 until scale).map { metricDatumIndex ->
49-
MetricDatum {
50-
metricName = "TestMetric"
51-
dimensions = listOf(
52-
Dimension {
53-
name = "TestDimension"
54-
value = "$suiteId-$scale"
55-
},
56-
)
57-
value = Random.nextDouble()
58-
unit = null
59-
timestamp = baseTime + ((metricDatumIndex + 1) * 2).seconds
60-
}
61-
}.toList()
62-
},
63-
)
52+
client.putMetricData {
53+
namespace = TEST_NAME_SPACE
54+
metricData = (0 until scale).map { metricDatumIndex ->
55+
MetricDatum {
56+
metricName = "TestMetric"
57+
dimensions = listOf(
58+
Dimension {
59+
name = "TestDimension"
60+
value = "$suiteId-$scale"
61+
},
62+
)
63+
value = Random.nextDouble()
64+
unit = null
65+
timestamp = baseTime + ((metricDatumIndex + 1) * 2).seconds
66+
}
67+
}.toList()
68+
}
6469
}
6570
}
6671

6772
private val getMetricDataBenchmark = object : AbstractOperationProtocolBenchmark<CloudWatchClient>("Get metric data") {
6873
override val requireScaling = true
6974

7075
override suspend fun transact(client: CloudWatchClient, scale: Int, iteration: Int) {
71-
client.getMetricData(
72-
GetMetricDataRequest {
73-
startTime = baseTime
74-
endTime = baseTime + 2.hours
75-
metricDataQueries = listOf(
76-
MetricDataQuery {
77-
id = "m0"
78-
returnData = true
79-
metricStat {
80-
unit = null
81-
stat = "Sum"
82-
metric = Metric {
83-
namespace = "TestNamespace"
84-
metricName = "TestMetric"
85-
dimensions = listOf(
86-
Dimension {
87-
name = "TestDimension"
88-
value = "$suiteId-$scale"
89-
},
90-
)
91-
}
92-
period = 60
76+
client.getMetricData {
77+
startTime = baseTime
78+
endTime = baseTime + 2.hours
79+
metricDataQueries = listOf(
80+
MetricDataQuery {
81+
id = "m0"
82+
returnData = true
83+
metricStat {
84+
unit = null
85+
stat = "Sum"
86+
metric = Metric {
87+
namespace = TEST_NAME_SPACE
88+
metricName = "TestMetric"
89+
dimensions = listOf(
90+
Dimension {
91+
name = "TestDimension"
92+
value = "$suiteId-$scale"
93+
},
94+
)
9395
}
94-
},
95-
)
96-
},
97-
)
96+
period = 60
97+
}
98+
},
99+
)
100+
}
98101
}
99102
}
100103

101104
private val listMetricsBenchmark = object : AbstractOperationProtocolBenchmark<CloudWatchClient>("List metrics") {
102105
override val requireScaling = true
103106

104107
override suspend fun transact(client: CloudWatchClient, scale: Int, iteration: Int) {
105-
client.listMetrics(
106-
ListMetricsRequest {
107-
namespace = "TestNamespace"
108-
},
109-
)
108+
client.listMetrics {
109+
namespace = TEST_NAME_SPACE
110+
}
110111
}
111112
}
112113
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
package aws.sdk.kotlin.benchmarks.service.definitions
6+
7+
import kotlin.time.Duration
8+
import kotlin.time.TimeSource
9+
10+
/**
11+
* Identifies the mode in which to run a phase of benchmark execution.
12+
*/
13+
sealed interface RunMode {
14+
/**
15+
* Run for a specific number of iterations.
16+
* @param iterations The number of iterations to run.
17+
*/
18+
data class Iterations(val iterations: Int) : RunMode
19+
20+
/**
21+
* Run for a specific amount of time.
22+
* @param time The amount of time to run.
23+
*/
24+
data class Time(val time: Duration) : RunMode
25+
}
26+
27+
val RunMode.explanation get() = when (this) {
28+
is RunMode.Iterations -> "$iterations iterations"
29+
is RunMode.Time -> time.toString()
30+
}
31+
32+
internal inline fun RunMode.forAtLeast(block: (Int?) -> Unit) {
33+
val start = TimeSource.Monotonic.markNow()
34+
35+
when (this) {
36+
is RunMode.Time -> {
37+
var cnt = 0
38+
while (start.elapsedNow() < time) {
39+
block(cnt)
40+
cnt++
41+
}
42+
println(" (completed $cnt iterations)")
43+
}
44+
45+
is RunMode.Iterations -> {
46+
repeat(iterations) { cnt ->
47+
block(cnt + 1)
48+
}
49+
println(" (took ${start.elapsedNow()})")
50+
}
51+
}
52+
}

tests/benchmarks/service-benchmarks/jvm/src/aws/sdk/kotlin/benchmarks/service/definitions/ServiceBenchmark.kt

Lines changed: 0 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ package aws.sdk.kotlin.benchmarks.service.definitions
77
import aws.sdk.kotlin.benchmarks.service.DEFAULT_ITERATION_TIME
88
import aws.sdk.kotlin.benchmarks.service.DEFAULT_WARMUP_TIME
99
import aws.smithy.kotlin.runtime.client.SdkClient
10-
import kotlin.time.Duration
11-
import kotlin.time.TimeSource
1210

1311
/**
1412
* Defines the harness for conducting a benchmark of a service client.
@@ -92,50 +90,6 @@ interface OperationBenchmark<C : SdkClient> {
9290
suspend fun tearDown(client: C) { }
9391
}
9492

95-
/**
96-
* Identifies the mode in which to run a phase of benchmark execution.
97-
*/
98-
sealed interface RunMode {
99-
/**
100-
* Run for a specific number of iterations.
101-
* @param iterations The number of iterations to run.
102-
*/
103-
data class Iterations(val iterations: Int) : RunMode
104-
105-
/**
106-
* Run for a specific amount of time.
107-
* @param time The amount of time to run.
108-
*/
109-
data class Time(val time: Duration) : RunMode
110-
}
111-
112-
val RunMode.explanation get() = when (this) {
113-
is RunMode.Iterations -> "$iterations iterations"
114-
is RunMode.Time -> time.toString()
115-
}
116-
117-
internal inline fun RunMode.forAtLeast(block: (Int?) -> Unit) {
118-
val start = TimeSource.Monotonic.markNow()
119-
120-
when (this) {
121-
is RunMode.Time -> {
122-
var cnt = 0
123-
while (start.elapsedNow() < time) {
124-
block(cnt)
125-
cnt++
126-
}
127-
println(" (completed $cnt iterations)")
128-
}
129-
130-
is RunMode.Iterations -> {
131-
repeat(iterations) { cnt ->
132-
block(cnt + 1)
133-
}
134-
println(" (took ${start.elapsedNow()})")
135-
}
136-
}
137-
}
138-
13993
/**
14094
* An abstract base class for operation benchmarks.
14195
* @param The name of the operation (e.g., `HeadBucket`).

0 commit comments

Comments
 (0)