Debian Codegraph @

jmh-scala-tests/src/main/scala/org/eclipse/collections/impl/jmh/AnagramSetScalaTest.scala

raw
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 java.util
14 import java.util.concurrent.{ExecutorService, Executors, TimeUnit}
15
16 import org.apache.commons.lang.RandomStringUtils
17 import org.junit.Assert
18
19 import scala.collection.mutable
20 import scala.collection.parallel.immutable.ParSet
21
22 object AnagramSetScalaTest
23 {
24 private val SIZE = 1000000
25 private val CORES = Runtime.getRuntime.availableProcessors
26 private val SIZE_THRESHOLD = 10
27 private val WORDS: Set[String] = Set(Seq.fill(SIZE)(RandomStringUtils.randomAlphabetic(5).toUpperCase()): _*)
28
29 private var executorService: ExecutorService = null
30
31 def setUp(): Unit =
32 {
33 this.executorService = Executors.newFixedThreadPool(CORES)
34 }
35
36 def tearDown(): Unit =
37 {
38 this.executorService.shutdownNow
39 this.executorService.awaitTermination(1L, TimeUnit.MILLISECONDS)
40 }
41
42 def serial_eager_scala(): Unit =
43 {
44 WORDS
45 .groupBy(Alphagram.apply)
46 .values
47 .filter(_.size >= SIZE_THRESHOLD)
48 .toSeq.sortBy(_.size)
49 .reverseIterator
50 .map(iterable => iterable.size + ": " + iterable.toString())
51 .foreach(string => Assert.assertFalse(string.isEmpty))
52 }
53
54 def serial_lazy_scala(): Unit =
55 {
56 WORDS.view
57 .groupBy(Alphagram.apply)
58 .values
59 .filter(_.size >= SIZE_THRESHOLD)
60 .toSeq.sortBy(_.size)
61 .reverseIterator
62 .map(iterable => iterable.size + ": " + iterable.toString())
63 .foreach(string => Assert.assertFalse(string.isEmpty))
64 }
65
66 def parallel_lazy_scala(): Unit =
67 {
68 val toBuffer: mutable.Buffer[ParSet[String]] = WORDS.par
69 .groupBy(Alphagram.apply)
70 .values
71 .filter(_.size >= SIZE_THRESHOLD)
72 .toBuffer
73 toBuffer.sortBy(_.size)
74 .par
75 .reverseMap(iterable => iterable.size + ": " + iterable.toString())
76 .foreach(string => Assert.assertFalse(string.isEmpty))
77 }
78
79 object Alphagram
80 {
81 def apply(string: String): Alphagram =
82 {
83 val key = string.toCharArray
84 util.Arrays.sort(key)
85 new Alphagram(key)
86 }
87 }
88
89 class Alphagram(private val key: Array[Char])
90 {
91 override def equals(o: Any): Boolean =
92 {
93 if (null == o || this.getClass != o.getClass)
94 {
95 return false
96 }
97 val other = o.asInstanceOf[Alphagram]
98 if (this eq other)
99 {
100 return true
101 }
102 util.Arrays.equals(this.key, other.key)
103 }
104
105 override def hashCode = util.Arrays.hashCode(this.key)
106
107 override def toString = new String(this.key)
108 }
109 }