Skip to content

Commit

Permalink
Merge pull request #2180 from wangyu096/issue_1697_3.7.x
Browse files Browse the repository at this point in the history
perf: 作业包含大量主机,执行作业请求响应时间过长 #1697
  • Loading branch information
jsonwan committed Jun 30, 2023
2 parents 16a8286 + 584ffcb commit c3e47c0
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
Expand Up @@ -80,11 +80,9 @@ public static <E> List<List<E>> partitionCollection(Collection<E> collection, in
int limit = (collection.size() + size - 1) / size;
return Stream.iterate(0, n -> n + 1)
.limit(limit)
.parallel()
.map(a -> collection.stream()
.skip(a * size)
.limit(size)
.parallel()
.collect(Collectors.toList()))
.collect(Collectors.toList());
}
Expand Down
Expand Up @@ -28,9 +28,11 @@
import org.junit.jupiter.api.Test;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

Expand Down Expand Up @@ -87,6 +89,25 @@ void testPartitionList() {
assertThat(partitionLists).hasSize(2);
assertThat(partitionLists.get(0)).hasSize(2);
assertThat(partitionLists.get(1)).hasSize(2);

// 测试分区较多的场景
list = new ArrayList<>();
for (int i = 1; i <= 2009; i++) {
list.add("test" + i);
}
partitionLists = CollectionUtil.partitionCollection(list, 2);
assertThat(partitionLists).hasSize(1005);
assertThat(partitionLists.get(0)).hasSize(2);
assertThat(partitionLists.get(0).get(0)).isEqualTo("test1");
assertThat(partitionLists.get(0).get(1)).isEqualTo("test2");
assertThat(partitionLists.get(1)).hasSize(2);
assertThat(partitionLists.get(1004)).hasSize(1);
assertThat(partitionLists.get(1004).get(0)).isEqualTo("test2009");

List<String> mergedElements =
partitionLists.stream().flatMap(Collection::stream).distinct().collect(Collectors.toList());
// 测试分区之后与原始的在数量上一致
assertThat(mergedElements).hasSize(2009);
}

@Test
Expand Down Expand Up @@ -135,6 +156,22 @@ void testPartitionHashSet() {
assertThat(partitionLists.get(0)).hasSize(3);
assertThat(partitionLists.get(1)).hasSize(3);
assertThat(partitionLists.get(2)).hasSize(1);

// 测试分区较多的场景
Set<String> set6 = new HashSet<>();
for (int i = 1; i <= 2009; i++) {
set6.add("test" + i);
}
partitionLists = CollectionUtil.partitionCollection(set6, 2);
assertThat(partitionLists).hasSize(1005);
assertThat(partitionLists.get(0)).hasSize(2);
assertThat(partitionLists.get(1)).hasSize(2);
assertThat(partitionLists.get(1004)).hasSize(1);

List<String> mergedElements =
partitionLists.stream().flatMap(Collection::stream).distinct().collect(Collectors.toList());
// 测试分区之后与原始的在数量上一致
assertThat(mergedElements).hasSize(2009);
}

}
Expand Down

0 comments on commit c3e47c0

Please sign in to comment.