mirror of
https://github.com/mii443/prometheus-android-exporter.git
synced 2025-08-22 23:25:40 +00:00
fix LastTimeRingBuffer
This commit is contained in:
@ -22,33 +22,41 @@ import kotlinx.coroutines.delay
|
|||||||
import kotlinx.coroutines.launch
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
import java.lang.IndexOutOfBoundsException
|
||||||
|
import kotlin.math.abs
|
||||||
|
|
||||||
private const val TAG: String = "REMOTE_WRITE_SENDER"
|
private const val TAG: String = "REMOTE_WRITE_SENDER"
|
||||||
|
|
||||||
// This class stores information about scrapes to PROM_SERVER and PUSHPROX
|
// This class stores information about scrapes to PROM_SERVER and PUSHPROX
|
||||||
// for purposes of scraping metrics on device and back-filling them later using remote write
|
// for purposes of scraping metrics on device and back-filling them later using remote write
|
||||||
//
|
//
|
||||||
// Only timestamps of succesfull scrapes are stored
|
// Only timestamps of successful scrapes are stored
|
||||||
internal class LastTimeRingBuffer(private val scrapeIntervalMs: Int) {
|
internal class LastTimeRingBuffer(private val scrapeIntervalMs: Int) {
|
||||||
private val buffer: Array<Long> = Array(hysteresisThreshold) { 0 }
|
private val buffer: Array<Long> = Array(hysteresisThreshold) { 0 }
|
||||||
private var firstIndex: Int = 0
|
private var firstIndex: Int = -1
|
||||||
|
|
||||||
companion object {
|
companion object {
|
||||||
private const val hysteresisThreshold: Int = 3
|
private const val hysteresisThreshold: Int = 3
|
||||||
}
|
}
|
||||||
|
|
||||||
fun setLastTime(timestamp: Long) {
|
fun setLastTime(timestamp: Long) {
|
||||||
firstIndex = firstIndex++ % hysteresisThreshold
|
firstIndex = (++firstIndex) % hysteresisThreshold
|
||||||
buffer[firstIndex] = timestamp
|
buffer[firstIndex] = timestamp
|
||||||
|
System.out.println("${buffer[0]} ${buffer[1]} ${buffer[2]}")
|
||||||
|
System.out.flush()
|
||||||
}
|
}
|
||||||
|
|
||||||
fun getTimeByIndex(index: Int): Long {
|
fun getTimeByIndex(index: Int): Long {
|
||||||
if (index > hysteresisThreshold - 1) {
|
if (index > hysteresisThreshold - 1) {
|
||||||
throw IllegalArgumentException("index cannot be bigger than hysteresisThreshold")
|
throw IndexOutOfBoundsException("index cannot be bigger than hysteresisThreshold")
|
||||||
}
|
}
|
||||||
|
|
||||||
val bufferIndex: Int = firstIndex + index % hysteresisThreshold
|
val bufferIndex: Int = (firstIndex - index)
|
||||||
return buffer[bufferIndex]
|
return if (bufferIndex < 0){
|
||||||
|
buffer[hysteresisThreshold + bufferIndex]
|
||||||
|
}else{
|
||||||
|
buffer[bufferIndex]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun checkScrapeDidNotHappenInTime(): Boolean {
|
fun checkScrapeDidNotHappenInTime(): Boolean {
|
||||||
@ -64,7 +72,6 @@ internal class LastTimeRingBuffer(private val scrapeIntervalMs: Int) {
|
|||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
data class RemoteWriteConfiguration(
|
data class RemoteWriteConfiguration(
|
||||||
|
@ -1,17 +0,0 @@
|
|||||||
package com.birdthedeveloper.prometheus.android.prometheus.android.exporter
|
|
||||||
|
|
||||||
import org.junit.Test
|
|
||||||
|
|
||||||
import org.junit.Assert.*
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Example local unit test, which will execute on the development machine (host).
|
|
||||||
*
|
|
||||||
* See [testing documentation](http://d.android.com/tools/testing).
|
|
||||||
*/
|
|
||||||
class ExampleUnitTest {
|
|
||||||
@Test
|
|
||||||
fun addition_isCorrect() {
|
|
||||||
assertEquals(4, 2 + 2)
|
|
||||||
}
|
|
||||||
}
|
|
@ -9,13 +9,33 @@ class LastTimeRingBufferTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `basic test of LastTimeRingBuffer`() {
|
fun `basic test of LastTimeRingBuffer`() {
|
||||||
val lastTimeBuffer = LastTimeRingBuffer(10)
|
val lastTimeRingBuffer = LastTimeRingBuffer(10)
|
||||||
assertEquals(0, lastTimeBuffer.getTimeByIndex(0))
|
assertEquals(0, lastTimeRingBuffer.getTimeByIndex(0))
|
||||||
|
|
||||||
lastTimeBuffer.setLastTime(2L)
|
lastTimeRingBuffer.setLastTime(2L)
|
||||||
lastTimeBuffer.setLastTime(5L)
|
lastTimeRingBuffer.setLastTime(5L)
|
||||||
|
|
||||||
//assertEquals(5, lastTimeBuffer.getTimeByIndex(0))
|
assertEquals(5, lastTimeRingBuffer.getTimeByIndex(0))
|
||||||
//assertEquals(2, lastTimeBuffer.getTimeByIndex(1))
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `ring buffer test`() {
|
||||||
|
val lastTimeRingBuffer = LastTimeRingBuffer(10)
|
||||||
|
|
||||||
|
for (i in 1..4){
|
||||||
|
lastTimeRingBuffer.setLastTime(i.toLong())
|
||||||
|
}
|
||||||
|
|
||||||
|
assertEquals(4, lastTimeRingBuffer.getTimeByIndex(0))
|
||||||
|
assertEquals(3, lastTimeRingBuffer.getTimeByIndex(1))
|
||||||
|
assertEquals(2, lastTimeRingBuffer.getTimeByIndex(2))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `getTimeByIndex throws exception on out-of-bounds index`(){
|
||||||
|
val lastTimeRingBuffer = LastTimeRingBuffer(10)
|
||||||
|
assertThrows(IndexOutOfBoundsException::class.java){
|
||||||
|
lastTimeRingBuffer.getTimeByIndex(Int.MAX_VALUE / 3)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user