2 * Copyright 2000-2017 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com.intellij.performance
19 data class IntervalData(val intervalStart: Double, val intervalEnd: Double, val count: Int)
21 class IntervalCounter(
22 private val minPower: Int,
23 private val maxPower: Int,
24 private val exponent: Double,
25 val data: Array<Int> = Array(maxPower - minPower, { 0 })
28 fun register(value: Long) {
29 val log = roundedLog(value)
30 val bucket = Math.min(maxPower - 1, Math.max(minPower, log.toInt())) - minPower
34 fun intervals(): List<IntervalData> {
35 return data.indices.map { interval(it) }
38 private fun interval(index: Int): IntervalData {
39 val start = Math.pow(exponent, minPower + index.toDouble())
40 val end = Math.pow(exponent, minPower + (index + 1).toDouble())
42 val count = data[index]
44 return IntervalData(start, end, count)
47 private fun roundedLog(time: Long): Double {
48 val dTime = time.toDouble()
49 return (Math.log(dTime) / Math.log(exponent))