-
Notifications
You must be signed in to change notification settings - Fork 244
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Extract test deps * Rate limiter test * Timer test * Decimal format test * List partition test * Licenses
- Loading branch information
Showing
9 changed files
with
212 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
benchmark-framework/src/test/java/io/openmessaging/benchmark/utils/ListPartitionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* 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 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.openmessaging.benchmark.utils; | ||
|
||
import static java.util.Arrays.asList; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import java.util.List; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ListPartitionTest { | ||
|
||
@Test | ||
void partitionList() { | ||
List<Integer> list = asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); | ||
List<List<Integer>> lists = ListPartition.partitionList(list, 3); | ||
assertThat(lists).satisfies(s -> { | ||
assertThat(s).hasSize(3); | ||
assertThat(s.get(0)).isEqualTo(asList(1, 4, 7, 10)); | ||
assertThat(s.get(1)).isEqualTo(asList(2, 5, 8)); | ||
assertThat(s.get(2)).isEqualTo(asList(3, 6, 9)); | ||
}); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...rk-framework/src/test/java/io/openmessaging/benchmark/utils/PaddingDecimalFormatTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* 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 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.openmessaging.benchmark.utils; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class PaddingDecimalFormatTest { | ||
|
||
@Test | ||
void format() { | ||
PaddingDecimalFormat format = new PaddingDecimalFormat("0.0", 7); | ||
assertThat(format.format(1L)).isEqualTo(" 1.0"); | ||
assertThat(format.format(1000L)).isEqualTo(" 1000.0"); | ||
assertThat(format.format(10000000L)).isEqualTo("10000000.0"); | ||
} | ||
|
||
} |
41 changes: 41 additions & 0 deletions
41
benchmark-framework/src/test/java/io/openmessaging/benchmark/utils/TimerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* 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 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.openmessaging.benchmark.utils; | ||
|
||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import java.util.function.Supplier; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class TimerTest { | ||
|
||
@Test | ||
void elapsedMillis() { | ||
Supplier<Long> mockClock = mock(Supplier.class); | ||
when(mockClock.get()).thenReturn(MILLISECONDS.toNanos(1), MILLISECONDS.toNanos(3)); | ||
Timer timer = new Timer(mockClock); | ||
assertThat(timer.elapsedMillis()).isEqualTo(2.0d); | ||
} | ||
|
||
@Test | ||
void elapsedSeconds() { | ||
Supplier<Long> mockClock = mock(Supplier.class); | ||
when(mockClock.get()).thenReturn(SECONDS.toNanos(1), SECONDS.toNanos(3)); | ||
Timer timer = new Timer(mockClock); | ||
assertThat(timer.elapsedSeconds()).isEqualTo(2.0d); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
...mark-framework/src/test/java/io/openmessaging/benchmark/utils/UniformRateLimiterTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* 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 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package io.openmessaging.benchmark.utils; | ||
|
||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
import static java.util.concurrent.TimeUnit.SECONDS; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatCode; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
import java.util.function.Supplier; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class UniformRateLimiterTest { | ||
|
||
@Test | ||
void getOpsPerSec() { | ||
assertThat(new UniformRateLimiter(1000).getOpsPerSec()).isEqualTo(1000.0d); | ||
} | ||
|
||
@Test | ||
void getIntervalNs() { | ||
assertThat(new UniformRateLimiter(1000).getIntervalNs()).isEqualTo(SECONDS.toNanos(1) / 1000); | ||
} | ||
|
||
@Test | ||
void acquireSlowSingleThread() { | ||
Supplier<Long> mockClock = mock(Supplier.class); | ||
when(mockClock.get()).thenReturn(SECONDS.toNanos(2)); | ||
UniformRateLimiter rateLimiter = new UniformRateLimiter(1000, mockClock); | ||
assertThat(rateLimiter.acquire()).isEqualTo(2000000000L); | ||
assertThat(rateLimiter.acquire()).isEqualTo(2001000000L); | ||
assertThat(rateLimiter.acquire()).isEqualTo(2002000000L); | ||
} | ||
|
||
@Test | ||
void uninterruptibleSleepNs() { | ||
long start = System.nanoTime(); | ||
long expectedEnd = start + MILLISECONDS.toNanos(100); | ||
UniformRateLimiter.uninterruptibleSleepNs(expectedEnd); | ||
long end = System.nanoTime(); | ||
assertThat(end).isGreaterThan(expectedEnd); | ||
} | ||
|
||
@Test | ||
void cinitExceptions() { | ||
assertThatCode(() -> new UniformRateLimiter(Double.NaN)).isInstanceOf(IllegalArgumentException.class); | ||
assertThatCode(() -> new UniformRateLimiter(1.0d / 0.0d)).isInstanceOf(IllegalArgumentException.class); | ||
assertThatCode(() -> new UniformRateLimiter(-0.1)).isInstanceOf(IllegalArgumentException.class); | ||
assertThatCode(() -> new UniformRateLimiter(0.0)).isInstanceOf(IllegalArgumentException.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters