Skip to content

Commit 89e7c1f

Browse files
committed
add time total taken
1 parent 1d3dd26 commit 89e7c1f

File tree

1 file changed

+87
-29
lines changed

1 file changed

+87
-29
lines changed

core-kotlin-modules/core-kotlin-collections-6/src/test/kotlin/com/baeldung/parallelOperationsCollections/ParallelOperationCollectionsUnitTest.kt

Lines changed: 87 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -30,149 +30,207 @@ class ParallelOperationCollectionsUnitTest {
3030

3131
private fun assertResults(filteredPeople: List<Person>) {
3232
assertThat(filteredPeople).containsExactly(
33-
Person("Bob", 16, false),
34-
Person("Alice", 30, true),
35-
Person("Charlie", 40, true),
36-
Person("Ahmad", 42, true)
33+
Person("Bob", 16, false), Person("Alice", 30, true), Person("Charlie", 40, true), Person("Ahmad", 42, true)
3734
)
3835
}
3936

4037
@Test
4138
fun `using coroutines for parallel operations`() = runBlocking {
39+
println(
40+
"%-30s %-40s %s".format(
41+
"Time", "Thread name", "Operation"
42+
)
43+
)
44+
val startTime = System.currentTimeMillis()
4245
val filteredPeople = people.map { person ->
4346
async {
4447
launch {
4548
person.isAdult = person.age >= 18
4649
println(
4750
"%-30s %-40s %s".format(
48-
dateFormat.format(System.currentTimeMillis()),
49-
Thread.currentThread().name,
50-
person
51+
dateFormat.format(System.currentTimeMillis()), Thread.currentThread().name, person
5152
)
5253
)
5354
}
5455
person
5556
}
5657
}.awaitAll().filter { it.age > 15 }.sortedBy { it.age }
5758

59+
val endTime = System.currentTimeMillis()
60+
val duration = endTime - startTime
61+
println("Total time taken: $duration ms")
62+
println()
63+
5864
assertResults(filteredPeople)
5965
}
6066

6167
@OptIn(ExperimentalCoroutinesApi::class)
6268
@Test
6369
fun `using coroutines for parallel operations with Flow`() = runBlocking {
70+
println(
71+
"%-30s %-40s %s".format(
72+
"Time", "Thread name", "Operation"
73+
)
74+
)
75+
val startTime = System.currentTimeMillis()
6476
val filteredPeople = people.asFlow().flatMapMerge { person ->
6577
flow {
6678
emit(async {
6779
person.isAdult = person.age >= 18
6880

6981
println(
7082
"%-30s %-40s %s".format(
71-
dateFormat.format(System.currentTimeMillis()),
72-
Thread.currentThread().name,
73-
person
83+
dateFormat.format(System.currentTimeMillis()), Thread.currentThread().name, person
7484
)
7585
)
7686
person
7787
}.await())
7888
}
7989
}.filter { it.age > 15 }.toList().sortedBy { it.age }
8090

91+
val endTime = System.currentTimeMillis()
92+
val duration = endTime - startTime
93+
println("Total time taken: $duration ms")
94+
println()
95+
8196
assertResults(filteredPeople)
8297
}
8398

8499
@Test
85100
fun `using RxJava for parallel operations`() { // Observable.class from io.reactivex;
101+
println(
102+
"%-30s %-40s %s".format(
103+
"Time", "Thread name", "Operation"
104+
)
105+
)
106+
val startTime = System.currentTimeMillis()
86107
val observable = Observable.fromIterable(people).flatMap({
87108
Observable.just(it).subscribeOn(Schedulers.computation()).doOnNext { person ->
88109
person.isAdult = person.age >= 18
89110
println(
90111
"%-30s %-40s %s".format(
91-
dateFormat.format(System.currentTimeMillis()),
92-
Thread.currentThread().name,
93-
person
112+
dateFormat.format(System.currentTimeMillis()), Thread.currentThread().name, person
94113
)
95114
)
96115
}
97116
}, people.size) // Uses maxConcurrency for the number of elements
98117
.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
99118

119+
val endTime = System.currentTimeMillis()
120+
val duration = endTime - startTime
121+
println("Total time taken: $duration ms")
122+
println()
123+
100124
assertResults(observable)
101125
}
102126

103127
@Test
104128
fun `using RxKotlin for parallel operations`() { // ObservableKt.kt.class from io.reactivex.rxkotlin
129+
println(
130+
"%-30s %-40s %s".format(
131+
"Time", "Thread name", "Operation"
132+
)
133+
)
134+
val startTime = System.currentTimeMillis()
105135
val observable = people.toObservable().flatMap({
106136
Observable.just(it).subscribeOn(Schedulers.computation()).doOnNext { person ->
107137
person.isAdult = person.age >= 18
108138
println(
109139
"%-30s %-40s %s".format(
110-
dateFormat.format(System.currentTimeMillis()),
111-
Thread.currentThread().name,
112-
person
140+
dateFormat.format(System.currentTimeMillis()), Thread.currentThread().name, person
113141
)
114142
)
115143
}
116144
}, people.size) // Uses maxConcurrency for the number of elements
117145
.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
118146

147+
val endTime = System.currentTimeMillis()
148+
val duration = endTime - startTime
149+
println("Total time taken: $duration ms")
150+
println()
151+
119152
assertResults(observable)
120153
}
121154

122155
@Test
123156
fun `using RxKotlin but still use 1 thread`() { // ObservableKt.kt.class from io.reactivex.rxkotlin
157+
println(
158+
"%-30s %-40s %s".format(
159+
"Time", "Thread name", "Operation"
160+
)
161+
)
162+
val startTime = System.currentTimeMillis()
124163
val observable =
125164
people.toObservable().subscribeOn(Schedulers.io()).flatMap { Observable.just(it) }.doOnNext { person ->
126165
person.isAdult = person.age >= 18
127166
println(
128167
"%-30s %-40s %s".format(
129-
dateFormat.format(System.currentTimeMillis()),
130-
Thread.currentThread().name,
131-
person
168+
dateFormat.format(System.currentTimeMillis()), Thread.currentThread().name, person
132169
)
133170
)
134171
}.filter { it.age > 15 }.toList().map { it.sortedBy { person -> person.age } }.blockingGet()
135172

173+
val endTime = System.currentTimeMillis()
174+
val duration = endTime - startTime
175+
println("Total time taken: $duration ms")
176+
println()
177+
136178
assertResults(observable)
137179
}
138180

139181
@Test
140182
fun `using parallelStream()`() {
183+
println(
184+
"%-30s %-40s %s".format(
185+
"Time", "Thread name", "Operation"
186+
)
187+
)
188+
val startTime = System.currentTimeMillis()
141189
val filteredPeople = people.parallelStream().map { person ->
142190
person.isAdult = person.age >= 18
143191
println(
144192
"%-30s %-40s %s".format(
145-
dateFormat.format(System.currentTimeMillis()),
146-
Thread.currentThread().name,
147-
person
193+
dateFormat.format(System.currentTimeMillis()), Thread.currentThread().name, person
148194
)
149195
)
150196
person
151197
}.filter { it.age > 15 }.sorted { p1, p2 -> p1.age.compareTo(p2.age) }.collect(Collectors.toList())
152198

199+
val endTime = System.currentTimeMillis()
200+
val duration = endTime - startTime
201+
println("Total time taken: $duration ms")
202+
println()
203+
153204
assertResults(filteredPeople)
154205
}
155206

156207
@Test
157208
fun `using ScheduledExecutorService for parallel operations`() {
158-
val executor = Executors.newCachedThreadPool()
209+
println(
210+
"%-30s %-40s %s".format(
211+
"Time", "Thread name", "Operation"
212+
)
213+
)
214+
val startTime = System.currentTimeMillis()
215+
val executor = Executors.newFixedThreadPool(people.size)
159216
val futures = people.map { person ->
160217
executor.submit(Callable {
161218
person.isAdult = person.age >= 18
162219
println(
163220
"%-30s %-40s %s".format(
164-
dateFormat.format(System.currentTimeMillis()),
165-
Thread.currentThread().name,
166-
person
221+
dateFormat.format(System.currentTimeMillis()), Thread.currentThread().name, person
167222
)
168223
)
169224
person
170225
})
171-
}
226+
}.map { it.get() }.filter { it.age > 15 }.sortedBy { it.age }
172227

173-
val filteredPeople = futures.map { it.get() }.filter { it.age > 15 }.sortedBy { it.age }
228+
val endTime = System.currentTimeMillis()
229+
val duration = endTime - startTime
230+
println("Total time taken: $duration ms")
231+
println()
174232

175-
assertResults(filteredPeople)
233+
assertResults(futures)
176234

177235
executor.shutdown()
178236
}

0 commit comments

Comments
 (0)