use of java.util.concurrent.atomic.AtomicIntegerArray in project reactor-core by reactor.
the class BoundedElasticSchedulerTest method whenCapReachedPicksLeastBusyExecutorWithContention.
@ParameterizedTestWithName
@CsvSource(value = { "4, 1", "4, 100", "4, 1000", "100, 1", "100, 100", "100, 1000", "1000, 1", "1000, 100", "1000, 1000", "10000, 1", "10000, 100", "10000, 1000" })
void whenCapReachedPicksLeastBusyExecutorWithContention(int maxThreads, int contention) throws InterruptedException {
BoundedElasticScheduler s = scheduler(maxThreads);
HashSet<BoundedElasticScheduler.BoundedState> boundedStates = new HashSet<>();
// reach the cap of workers and keep track of boundedStates
for (int i = 0; i < maxThreads; i++) {
boundedStates.add(afterTest.autoDispose(s.boundedServices.pick()));
}
assertThat(boundedStates).as("all distinct").hasSize(maxThreads);
CountDownLatch latch = new CountDownLatch(1);
AtomicInteger countDown = new AtomicInteger(contention);
AtomicInteger errors = new AtomicInteger();
ExecutorService executorService = Executors.newFixedThreadPool(contention);
AtomicIntegerArray busyPattern = new AtomicIntegerArray(Math.max(5, contention));
for (int i = 0; i < contention; i++) {
executorService.submit(() -> {
if (countDown.get() <= 0) {
return;
}
try {
BoundedElasticScheduler.BoundedState bs = s.boundedServices.pick();
assertThat(boundedStates).as("picked a busy one").contains(bs);
assertThat(bs.markPicked()).isTrue();
int business = bs.markCount;
busyPattern.incrementAndGet(business);
} catch (Throwable t) {
errors.incrementAndGet();
t.printStackTrace();
countDown.set(0);
latch.countDown();
} finally {
if (countDown.decrementAndGet() <= 0) {
latch.countDown();
}
}
});
}
assertThat(latch.await(10, TimeUnit.SECONDS)).as("latch").isTrue();
executorService.shutdownNow();
assertThat(errors).as("errors").hasValue(0);
int maxPicks = 0;
for (int businessFactor = 0; businessFactor < busyPattern.length(); businessFactor++) {
int pickCount = busyPattern.get(businessFactor);
if (pickCount == 0)
continue;
if (pickCount > maxPicks)
maxPicks = pickCount;
LOGGER.trace("Picked {} times at business level {}", pickCount, businessFactor);
}
final int expectedMaxPick = Math.min(contention, maxThreads);
Offset<Integer> offset;
if (expectedMaxPick < 10) {
offset = Offset.offset(1);
} else if (expectedMaxPick < 1000) {
offset = Offset.offset(10);
} else {
offset = Offset.offset(50);
}
assertThat(maxPicks).as("maxPicks").isCloseTo(expectedMaxPick, offset);
LOGGER.info("Max picks {} ", maxPicks);
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project MyPerf4J by ThinkpadNC5.
the class AtomicIntArrayBench method setup.
@Setup
public void setup() {
jdkArray = new AtomicIntegerArray(1024);
myArray = new MyAtomicIntArray(1024);
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project android_packages_apps_Settings by omnirom.
the class SelectableSubscriptionsTest method atomicToList_zeroLengthInput_getEmptyList.
@Test
public void atomicToList_zeroLengthInput_getEmptyList() {
List<Integer> result = SelectableSubscriptions.atomicToList(new AtomicIntegerArray(0));
assertThat(result.size()).isEqualTo(0);
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project android_packages_apps_Settings by omnirom.
the class SelectableSubscriptionsTest method atomicToList_subIdInArray_getList.
@Test
public void atomicToList_subIdInArray_getList() {
AtomicIntegerArray array = new AtomicIntegerArray(3);
array.set(0, 3);
array.set(1, 7);
array.set(2, 4);
List<Integer> result = SelectableSubscriptions.atomicToList(array);
assertThat(result.size()).isEqualTo(3);
assertThat(result.get(0)).isEqualTo(3);
assertThat(result.get(1)).isEqualTo(7);
assertThat(result.get(2)).isEqualTo(4);
}
use of java.util.concurrent.atomic.AtomicIntegerArray in project assertj-core by joel-costigliola.
the class RecursiveComparisonDifferenceCalculator method compareAtomicIntegerArray.
private static void compareAtomicIntegerArray(DualValue dualValue, ComparisonState comparisonState) {
if (!dualValue.isActualFieldAnAtomicIntegerArray()) {
comparisonState.addDifference(dualValue, differentTypeErrorMessage(dualValue, "an AtomicIntegerArray"));
return;
}
AtomicIntegerArray actual = (AtomicIntegerArray) dualValue.actual;
AtomicIntegerArray expected = (AtomicIntegerArray) dualValue.expected;
// both values in dualValue are arrays
int actualArrayLength = actual.length();
int expectedArrayLength = expected.length();
if (actualArrayLength != expectedArrayLength) {
comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "AtomicIntegerArrays", actualArrayLength, expectedArrayLength));
// no need to inspect elements, arrays are not equal as they don't have the same size
return;
}
// register each pair of actual/expected elements for recursive comparison
FieldLocation arrayFieldLocation = dualValue.fieldLocation;
for (int i = 0; i < actualArrayLength; i++) {
Object actualElement = actual.get(i);
Object expectedElement = expected.get(i);
FieldLocation elementFieldLocation = arrayFieldLocation.field(format(ARRAY_FIELD_NAME + "[%d]", i));
comparisonState.registerForComparison(new DualValue(elementFieldLocation, actualElement, expectedElement));
}
}
Aggregations