| 1 |
/* |
| 2 |
* Copyright (c) 2021 Goldman Sachs. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* and Eclipse Distribution License v. 1.0 which accompany this distribution. |
| 6 |
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* and the Eclipse Distribution License is available at |
| 8 |
* http://www.eclipse.org/org/documents/edl-v10.php. |
| 9 |
*/ |
| 10 |
|
| 11 |
package org.eclipse.collections.impl.jmh |
| 12 |
|
| 13 |
import org.junit.Assert |
| 14 |
|
| 15 |
import scala.collection.IterableView |
| 16 |
import scala.collection.mutable.HashSet |
| 17 |
import scala.collection.parallel.immutable.ParMap |
| 18 |
import scala.collection.parallel.mutable.ParHashSet |
| 19 |
|
| 20 |
object GroupBySetScalaTest |
| 21 |
{ |
| 22 |
private val SIZE = 1000000 |
| 23 |
private val integers = new HashSet[Int]() ++ (0 to SIZE - 1) |
| 24 |
|
| 25 |
def groupBy_2_keys_serial_eager_scala(): Map[Boolean, HashSet[Int]] = |
| 26 |
{ |
| 27 |
val multimap: Map[Boolean, HashSet[Int]] = this.integers.groupBy((each: Int) => each % 2 == 0) |
| 28 |
Assert.assertEquals(2, multimap.size) |
| 29 |
multimap |
| 30 |
} |
| 31 |
|
| 32 |
def test_groupBy_2_keys_serial_eager_scala(): Unit = |
| 33 |
{ |
| 34 |
val multimap: Map[Boolean, HashSet[Int]] = groupBy_2_keys_serial_eager_scala() |
| 35 |
val odds: HashSet[Int] = multimap(false) |
| 36 |
val evens: HashSet[Int] = multimap(true) |
| 37 |
Assert.assertEquals(0.to(999999, 2).toSet, evens) |
| 38 |
Assert.assertEquals(1.to(999999, 2).toSet, odds) |
| 39 |
} |
| 40 |
|
| 41 |
def groupBy_100_keys_serial_eager_scala(): Map[Int, HashSet[Int]] = |
| 42 |
{ |
| 43 |
val multimap: Map[Int, HashSet[Int]] = this.integers.groupBy((each: Int) => each % 100) |
| 44 |
Assert.assertEquals(100, multimap.size) |
| 45 |
multimap |
| 46 |
} |
| 47 |
|
| 48 |
def test_groupBy_100_keys_serial_eager_scala(): Unit = |
| 49 |
{ |
| 50 |
val multimap: Map[Int, HashSet[Int]] = groupBy_100_keys_serial_eager_scala() |
| 51 |
for (i <- 0 to 99) |
| 52 |
{ |
| 53 |
val integers: HashSet[Int] = multimap(i) |
| 54 |
Assert.assertEquals(i.to(999999, 100).toSet, integers) |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
def groupBy_10000_keys_serial_eager_scala(): Map[Int, HashSet[Int]] = |
| 59 |
{ |
| 60 |
val multimap: Map[Int, HashSet[Int]] = this.integers.groupBy((each: Int) => each % 10000) |
| 61 |
Assert.assertEquals(10000, multimap.size) |
| 62 |
multimap |
| 63 |
} |
| 64 |
|
| 65 |
def test_groupBy_10000_keys_serial_eager_scala(): Unit = |
| 66 |
{ |
| 67 |
val multimap: Map[Int, HashSet[Int]] = groupBy_10000_keys_serial_eager_scala() |
| 68 |
for (i <- 0 to 9999) |
| 69 |
{ |
| 70 |
val integers: HashSet[Int] = multimap(i) |
| 71 |
Assert.assertEquals(i.to(999999, 10000).toSet, integers) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
def groupBy_unordered_lists_2_keys_serial_lazy_scala(): Map[Boolean, IterableView[Int, HashSet[Int]]] = |
| 76 |
{ |
| 77 |
val multimap: Map[Boolean, IterableView[Int, HashSet[Int]]] = this.integers.view.groupBy((each: Int) => each % 2 == 0) |
| 78 |
Assert.assertEquals(2, multimap.size) |
| 79 |
multimap |
| 80 |
} |
| 81 |
|
| 82 |
def test_groupBy_unordered_lists_2_keys_serial_lazy_scala(): Unit = |
| 83 |
{ |
| 84 |
val multimap: Map[Boolean, IterableView[Int, HashSet[Int]]] = groupBy_unordered_lists_2_keys_serial_lazy_scala() |
| 85 |
val evens: IterableView[Int, HashSet[Int]] = multimap(true) |
| 86 |
val odds: IterableView[Int, HashSet[Int]] = multimap(false) |
| 87 |
Assert.assertEquals(0.to(999999, 2).toSet, evens.toSet) |
| 88 |
Assert.assertEquals(1.to(999999, 2).toSet, odds.toSet) |
| 89 |
} |
| 90 |
|
| 91 |
def groupBy_unordered_lists_100_keys_serial_lazy_scala(): Map[Int, IterableView[Int, HashSet[Int]]] = |
| 92 |
{ |
| 93 |
val multimap: Map[Int, IterableView[Int, HashSet[Int]]] = this.integers.view.groupBy((each: Int) => each % 100) |
| 94 |
Assert.assertEquals(100, multimap.size) |
| 95 |
multimap |
| 96 |
} |
| 97 |
|
| 98 |
def test_groupBy_unordered_lists_100_keys_serial_lazy_scala(): Unit = |
| 99 |
{ |
| 100 |
val multimap: Map[Int, IterableView[Int, HashSet[Int]]] = groupBy_unordered_lists_100_keys_serial_lazy_scala() |
| 101 |
for (i <- 0 to 99) |
| 102 |
{ |
| 103 |
val integers: IterableView[Int, HashSet[Int]] = multimap(i) |
| 104 |
Assert.assertEquals(i.to(999999, 100).toSet, integers.toSet) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
def groupBy_unordered_lists_10000_keys_serial_lazy_scala(): Map[Int, IterableView[Int, HashSet[Int]]] = |
| 109 |
{ |
| 110 |
val multimap: Map[Int, IterableView[Int, HashSet[Int]]] = this.integers.view.groupBy((each: Int) => each % 10000) |
| 111 |
Assert.assertEquals(10000, multimap.size) |
| 112 |
multimap |
| 113 |
} |
| 114 |
|
| 115 |
def test_groupBy_unordered_lists_10000_keys_serial_lazy_scala(): Unit = |
| 116 |
{ |
| 117 |
val multimap: Map[Int, IterableView[Int, HashSet[Int]]] = groupBy_unordered_lists_10000_keys_serial_lazy_scala() |
| 118 |
for (i <- 0 to 9999) |
| 119 |
{ |
| 120 |
val integers: IterableView[Int, HashSet[Int]] = multimap(i) |
| 121 |
Assert.assertEquals(i.to(999999, 10000).toSet, integers.toSet) |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
def groupBy_2_keys_parallel_lazy_scala(): ParMap[Boolean, ParHashSet[Int]] = |
| 126 |
{ |
| 127 |
val multimap: ParMap[Boolean, ParHashSet[Int]] = this.integers.par.groupBy((each: Int) => each % 2 == 0) |
| 128 |
Assert.assertEquals(2, multimap.size) |
| 129 |
multimap |
| 130 |
} |
| 131 |
|
| 132 |
def test_groupBy_2_keys_parallel_lazy_scala(): Unit = |
| 133 |
{ |
| 134 |
val multimap: ParMap[Boolean, ParHashSet[Int]] = groupBy_2_keys_parallel_lazy_scala() |
| 135 |
val evens: ParHashSet[Int] = multimap(true) |
| 136 |
val odds: ParHashSet[Int] = multimap(false) |
| 137 |
Assert.assertEquals(0.to(999999, 2).toSet, evens) |
| 138 |
Assert.assertEquals(1.to(999999, 2).toSet, odds) |
| 139 |
} |
| 140 |
|
| 141 |
def groupBy_100_keys_parallel_lazy_scala(): ParMap[Int, ParHashSet[Int]] = |
| 142 |
{ |
| 143 |
val multimap: ParMap[Int, ParHashSet[Int]] = this.integers.par.groupBy((each: Int) => each % 100) |
| 144 |
Assert.assertEquals(100, multimap.size) |
| 145 |
multimap |
| 146 |
} |
| 147 |
|
| 148 |
def test_groupBy_100_keys_parallel_lazy_scala(): Unit = |
| 149 |
{ |
| 150 |
val multimap: ParMap[Int, ParHashSet[Int]] = groupBy_100_keys_parallel_lazy_scala() |
| 151 |
for (i <- 0 to 99) |
| 152 |
{ |
| 153 |
val integers: ParHashSet[Int] = multimap(i) |
| 154 |
Assert.assertEquals(i.to(999999, 100).toSet, integers) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
def groupBy_10000_keys_parallel_lazy_scala(): ParMap[Int, ParHashSet[Int]] = |
| 159 |
{ |
| 160 |
val multimap: ParMap[Int, ParHashSet[Int]] = this.integers.par.groupBy((each: Int) => each % 10000) |
| 161 |
Assert.assertEquals(10000, multimap.size) |
| 162 |
multimap |
| 163 |
} |
| 164 |
|
| 165 |
def test_groupBy_10000_keys_parallel_lazy_scala(): Unit = |
| 166 |
{ |
| 167 |
val multimap: ParMap[Int, ParHashSet[Int]] = groupBy_10000_keys_parallel_lazy_scala() |
| 168 |
for (i <- 0 to 9999) |
| 169 |
{ |
| 170 |
val integers: ParHashSet[Int] = multimap(i) |
| 171 |
Assert.assertEquals(i.to(999999, 10000).toSet, integers) |
| 172 |
} |
| 173 |
} |
| 174 |
} |