fix LastTimeRingBuffer

This commit is contained in:
Martin Ptáček
2023-06-15 14:31:53 +02:00
parent dee7401cbc
commit 143c244af1
3 changed files with 41 additions and 31 deletions

View File

@ -22,33 +22,41 @@ import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.lang.IndexOutOfBoundsException
import kotlin.math.abs
private const val TAG: String = "REMOTE_WRITE_SENDER"
// 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
//
// Only timestamps of succesfull scrapes are stored
// Only timestamps of successful scrapes are stored
internal class LastTimeRingBuffer(private val scrapeIntervalMs: Int) {
private val buffer: Array<Long> = Array(hysteresisThreshold) { 0 }
private var firstIndex: Int = 0
private var firstIndex: Int = -1
companion object {
private const val hysteresisThreshold: Int = 3
}
fun setLastTime(timestamp: Long) {
firstIndex = firstIndex++ % hysteresisThreshold
firstIndex = (++firstIndex) % hysteresisThreshold
buffer[firstIndex] = timestamp
System.out.println("${buffer[0]} ${buffer[1]} ${buffer[2]}")
System.out.flush()
}
fun getTimeByIndex(index: Int): Long {
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
return buffer[bufferIndex]
val bufferIndex: Int = (firstIndex - index)
return if (bufferIndex < 0){
buffer[hysteresisThreshold + bufferIndex]
}else{
buffer[bufferIndex]
}
}
fun checkScrapeDidNotHappenInTime(): Boolean {
@ -64,7 +72,6 @@ internal class LastTimeRingBuffer(private val scrapeIntervalMs: Int) {
}
return false
}
}
data class RemoteWriteConfiguration(

View File

@ -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)
}
}

View File

@ -9,13 +9,33 @@ class LastTimeRingBufferTest {
@Test
fun `basic test of LastTimeRingBuffer`() {
val lastTimeBuffer = LastTimeRingBuffer(10)
assertEquals(0, lastTimeBuffer.getTimeByIndex(0))
val lastTimeRingBuffer = LastTimeRingBuffer(10)
assertEquals(0, lastTimeRingBuffer.getTimeByIndex(0))
lastTimeBuffer.setLastTime(2L)
lastTimeBuffer.setLastTime(5L)
lastTimeRingBuffer.setLastTime(2L)
lastTimeRingBuffer.setLastTime(5L)
//assertEquals(5, lastTimeBuffer.getTimeByIndex(0))
//assertEquals(2, lastTimeBuffer.getTimeByIndex(1))
assertEquals(5, lastTimeRingBuffer.getTimeByIndex(0))
}
@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)
}
}
}