Skip to content

Commit

Permalink
feat: add metric for block compression ratio and blob utilization ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
jonesho committed Dec 5, 2024
1 parent 4028a4a commit 7ee6316
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ interface BlobCompressor {
}

class GoBackedBlobCompressor private constructor(
internal val goNativeBlobCompressor: GoNativeBlobCompressor,
private val goNativeBlobCompressor: GoNativeBlobCompressor,
private val dataLimit: UInt,
private val metricsFacade: MetricsFacade
) : BlobCompressor {

Expand All @@ -62,7 +63,7 @@ class GoBackedBlobCompressor private constructor(
if (!initialized) {
throw InstantiationException(goNativeBlobCompressor.Error())
}
instance = GoBackedBlobCompressor(goNativeBlobCompressor, metricsFacade)
instance = GoBackedBlobCompressor(goNativeBlobCompressor, dataLimit, metricsFacade)
} else {
throw IllegalStateException("Compressor singleton instance already created")
}
Expand All @@ -84,6 +85,16 @@ class GoBackedBlobCompressor private constructor(
name = "compressor.appendblock",
description = "Time taken to compress block into current blob"
)
private val compressionRatioHistogram = metricsFacade.createHistogram(
category = LineaMetricsCategory.BLOB,
name = "block.compression.ratio",
description = "Block compression ratio measured in [0.0,1.0]"
)
private val utilizationRatioHistogram = metricsFacade.createHistogram(
category = LineaMetricsCategory.BLOB,
name = "data.utilization.ratio",
description = "Data utilization ratio of a blob measured in [0.0,1.0]"
)

private val log = LogManager.getLogger(GoBackedBlobCompressor::class.java)

Expand All @@ -99,7 +110,9 @@ class GoBackedBlobCompressor private constructor(
goNativeBlobCompressor.Write(blockRLPEncoded, blockRLPEncoded.size)
}
val compressedSizeAfter = goNativeBlobCompressor.Len()
val compressionRatio = 1.0 - ((compressedSizeAfter - compressionSizeBefore).toDouble() / blockRLPEncoded.size)
val compressionRatio = (1.0 - (compressedSizeAfter - compressionSizeBefore).toDouble() / blockRLPEncoded.size)
.also { compressionRatioHistogram.record(it) }

log.trace(
"block compressed: blockRlpSize={} compressionDataBefore={} compressionDataAfter={} compressionRatio={}",
blockRLPEncoded.size,
Expand All @@ -122,6 +135,7 @@ class GoBackedBlobCompressor private constructor(
override fun getCompressedData(): ByteArray {
val compressedData = ByteArray(goNativeBlobCompressor.Len())
goNativeBlobCompressor.Bytes(compressedData)
utilizationRatioHistogram.record(goNativeBlobCompressor.Len().toDouble() / dataLimit.toInt())
return compressedData
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import org.junit.jupiter.api.assertThrows
import org.junit.jupiter.api.extension.ExtendWith
import tech.pegasys.teku.infrastructure.async.SafeFuture
import java.util.concurrent.ExecutionException
import kotlin.random.Random
import kotlin.time.Duration.Companion.seconds
import kotlin.time.toJavaDuration

Expand All @@ -45,7 +44,6 @@ class BlobsPostgresDaoTest : CleanDbTestSuiteParallel() {
private val expectedEndBlock = 100UL
private val expectedStartBlockTime = fakeClock.now().trimToSecondPrecision()
private val expectedEndBlockTime = fakeClock.now().plus(1200.seconds).trimToMillisecondPrecision()
private val expectedShnarf = Random.nextBytes(32)

@BeforeEach
fun beforeEach() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ interface MetricsFacade {
name: String,
description: String,
tags: List<Tag> = emptyList(),
baseUnit: String
baseUnit: String? = null
): Histogram

fun <T> createSimpleTimer(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ class MicrometerMetricsFacade(
name: String,
description: String,
tags: List<Tag>,
baseUnit: String
baseUnit: String?
): Histogram {
if (category != null) requireValidMicrometerName(category.toString())
requireValidMicrometerName(name)
requireValidBaseUnit(baseUnit)
if (baseUnit != null) requireValidBaseUnit(baseUnit)
val distributionSummaryBuilder = DistributionSummary.builder(metricHandle(category, name))
if (tags.isNotEmpty()) {
val flatTags = tags.flatMap {
Expand Down

0 comments on commit 7ee6316

Please sign in to comment.