Skip to content

Add benchmarks to prometheus#346

Merged
JonoPrest merged 3 commits into
mainfrom
jp/benchmark-to-prometheus
Nov 19, 2024
Merged

Add benchmarks to prometheus#346
JonoPrest merged 3 commits into
mainfrom
jp/benchmark-to-prometheus

Conversation

@JonoPrest

Copy link
Copy Markdown
Collaborator

No description provided.

@JonoPrest JonoPrest requested a review from DZakh November 19, 2024 11:06
Comment on lines +109 to +147
module Stats = {
open Belt
type t = {
n: int,
mean: float,
@as("std-dev") stdDev: float,
min: float,
max: float,
sum: float,
}

let round = (float, ~precision=2) => {
let factor = Js.Math.pow_float(~base=10.0, ~exp=precision->Int.toFloat)
Js.Math.round(float *. factor) /. factor
}

let makeFromDataSet = (dataSet: SummaryData.DataSet.t) => {
let n = dataSet.count
let countBigDecimal = n->BigDecimal.fromInt
let mean = dataSet.sum->BigDecimal.div(countBigDecimal)
let variance =
dataSet.sumOfSquares
->BigDecimal.div(countBigDecimal)
->BigDecimal.minus(mean->BigDecimal.times(mean))
let stdDev = BigDecimal.sqrt(variance)
let roundBigDecimal = bd =>
bd->BigDecimal.decimalPlaces(dataSet.decimalPlaces)->BigDecimal.toNumber
let roundFloat = float => float->round(~precision=dataSet.decimalPlaces)
{
n,
mean: mean->roundBigDecimal,
stdDev: stdDev->roundBigDecimal,
min: dataSet.min->roundFloat,
max: dataSet.max->roundFloat,
sum: dataSet.sum->roundBigDecimal,
}
}
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved out of the "Summary" module below

Comment on lines +207 to +230
let saveLiveMetrics = if (
Env.Benchmark.saveDataStrategy->Env.Benchmark.SaveDataStrategy.shouldSavePrometheus
) {
let throttlerMapping = ThrottlerMapping.make()

(dataSet: SummaryData.DataSet.t, ~group, ~label) => {
let throttler = throttlerMapping->ThrottlerMapping.get(~group, ~label)
throttler->Throttler.schedule(() => {
let {n, mean, stdDev, min, max, sum} = dataSet->Stats.makeFromDataSet
Prometheus.setBenchmarkSummaryData(
~group,
~label,
~n,
~mean,
~stdDev,
~min,
~max,
~sum,
)->Promise.resolve
})
}
} else {
(_dataSet, ~group as _, ~label as _) => ()
}

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If ENVIO_SAVE_BENCHMARK_DATA_STRATEGY="prometheus" this will return true. I black boxed the strategy type since we may want to introduce other things like saving to postgres orboth prometheus and another type etc.

let throttlerMapping = ThrottlerMapping.make()

(dataSet: SummaryData.DataSet.t, ~group, ~label) => {
let throttler = throttlerMapping->ThrottlerMapping.get(~group, ~label)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throttler for setting prometheus is per group/label of the data set

@DZakh DZakh left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, but I'd like to have a single env var for the benchmark configuration

Comment on lines +61 to +78
let shouldSaveData = envSafe->EnvSafe.get("ENVIO_SAVE_BENCHMARK_DATA", S.bool, ~fallback=false)
let saveDataStrategy =
envSafe->EnvSafe.get(
"ENVIO_SAVE_BENCHMARK_DATA_STRATEGY",
SaveDataStrategy.schema,
~fallback=SaveDataStrategy.default,
)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about having a single env var with options:

  • true (JsonFile)
  • json-file (JsonFile)
  • prometheus (Prometheus)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok sure that works 👍🏼

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DZakh check the latest commit and lmk if you're happy 👍🏼

@JonoPrest JonoPrest force-pushed the jp/benchmark-to-prometheus branch from 9e938d2 to 599280c Compare November 19, 2024 12:03
@JonoPrest JonoPrest enabled auto-merge (squash) November 19, 2024 12:07
@JonoPrest JonoPrest merged commit 67c3c1f into main Nov 19, 2024
@JonoPrest JonoPrest deleted the jp/benchmark-to-prometheus branch November 19, 2024 12:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants