Rename StatisticsUtil.getUpperBound to StatisticsUtil.roundToUpperBound for naming unification and add tests

(cherry picked from commit 4c237cd6392453a559e8944641c4b5daf47ad086)

IJ-CR-12365

GitOrigin-RevId: 44a5259da60f95dcc179529aa16fcda803aab89a
This commit is contained in:
Victor Matchenko
2021-07-30 12:33:36 +00:00
committed by intellij-monorepo-bot
parent 57f5f6a7cc
commit bc72ce8bab
3 changed files with 42 additions and 6 deletions
@@ -28,12 +28,12 @@ class SystemRuntimeCollector : ApplicationUsagesCollector() {
override fun getMetrics(): Set<MetricEvent> {
val result = HashSet<MetricEvent>()
result.add(CORES.metric(StatisticsUtil.getUpperBound(Runtime.getRuntime().availableProcessors(),
intArrayOf(1, 2, 4, 6, 8, 12, 16, 20, 24, 32, 64))))
result.add(CORES.metric(StatisticsUtil.roundToUpperBound(Runtime.getRuntime().availableProcessors(),
intArrayOf(1, 2, 4, 6, 8, 12, 16, 20, 24, 32, 64))))
val osMxBean = ManagementFactory.getOperatingSystemMXBean() as OperatingSystemMXBean
val totalPhysicalMemory = StatisticsUtil.getUpperBound((osMxBean.totalPhysicalMemorySize.toDouble() / (1 shl 30)).roundToInt(),
intArrayOf(1, 2, 4, 8, 12, 16, 24, 32, 48, 64, 128, 256))
val totalPhysicalMemory = StatisticsUtil.roundToUpperBound((osMxBean.totalPhysicalMemorySize.toDouble() / (1 shl 30)).roundToInt(),
intArrayOf(1, 2, 4, 8, 12, 16, 24, 32, 48, 64, 128, 256))
result.add(MEMORY_SIZE.metric(totalPhysicalMemory))
var totalSwapSize = (osMxBean.totalSwapSpaceSize.toDouble() / (1 shl 30)).roundToInt()
@@ -8,6 +8,7 @@ import com.intellij.internal.statistic.utils.StatisticsUtil
import com.intellij.internal.statistic.utils.StatisticsUtil.getCurrentHourInUTC
import com.intellij.internal.statistic.utils.StatisticsUtil.getNextPowerOfTwo
import com.intellij.internal.statistic.utils.StatisticsUtil.roundToPowerOfTwo
import com.intellij.internal.statistic.utils.StatisticsUtil.roundToUpperBound
import com.intellij.testFramework.LightPlatformTestCase
import junit.framework.TestCase
import org.junit.Test
@@ -119,6 +120,37 @@ class StatisticsUtilTest : LightPlatformTestCase() {
assertEquals(expected, roundToPowerOfTwo(value), "Incorrect key for value `$value`")
}
@Test
fun `test round to upper bound`() {
// Test empty bounds return next power of two
testRoundToUpperBound(0, intArrayOf(), 0)
testRoundToUpperBound(1, intArrayOf(), 1)
testRoundToUpperBound(2, intArrayOf(), 2)
testRoundToUpperBound(3, intArrayOf(), 4)
testRoundToUpperBound(10, intArrayOf(), 16)
testRoundToUpperBound(Int.MAX_VALUE, intArrayOf(), Int.MAX_VALUE)
// Test with bounds
// on the edge
testRoundToUpperBound(0, intArrayOf(0, 1, 2), 0)
testRoundToUpperBound(1, intArrayOf(0, 1, 2), 1)
testRoundToUpperBound(2, intArrayOf(0, 1, 2), 2)
// between bounds
testRoundToUpperBound(5, intArrayOf(1, 10, 100), 10)
testRoundToUpperBound(50, intArrayOf(1, 10, 100), 100)
// out of bounds
testRoundToUpperBound(-1, intArrayOf(0, 1, 2), 0)
testRoundToUpperBound(3, intArrayOf(0, 1, 2), 2)
// corner cases
testRoundToUpperBound(Int.MIN_VALUE, intArrayOf(0, 1, 2), 0)
testRoundToUpperBound(Int.MAX_VALUE, intArrayOf(0, 1, 2), 2)
}
private fun testRoundToUpperBound(value: Int, bounds: IntArray, expected: Int) {
assertEquals(expected, roundToUpperBound(value, bounds), "Incorrect key for value `$value`")
}
@Test
fun `test hash sensitive data`() {
@@ -73,14 +73,14 @@ object StatisticsUtil {
/**
* Anonymizes value by finding upper bound in provided bounds.
* Allows more fine tuning then `com.intellij.internal.statistic.utils.StatisticsUtil#getNextPowerOfTwo`
* Allows more fine tuning then `com.intellij.internal.statistic.utils.StatisticsUtil#roundToPowerOfTwo`
* but requires manual maintaining.
*
* @param bounds is an integer array sorted in ascending order (required, but not checked)
* @return value upper bound or next power of two if no bounds were provided (as fallback)
* */
@JvmStatic
fun getUpperBound(value: Int, bounds: IntArray): Int {
fun roundToUpperBound(value: Int, bounds: IntArray): Int {
if (bounds.isEmpty()) return roundToPowerOfTwo(value)
for (bound in bounds)
@@ -88,6 +88,10 @@ object StatisticsUtil {
return bounds.last()
}
@JvmStatic
@Deprecated(message = "Use roundToUpperBound instead", replaceWith = ReplaceWith("roundToUpperBound"))
fun getUpperBound(value: Int, bounds: IntArray): Int = roundToUpperBound(value, bounds)
/**
* Anonymizes sensitive project properties by rounding it to the next value in steps list.
* See `com.intellij.internal.statistic.collectors.fus.fileTypes.FileTypeUsagesCollector`