Effect CommunityEC
Effect Community11mo ago
3 replies
Holder

How to measure the time and maybe memory of an effect?

I need to be able to measure the duration of a function and noticed there is a Metrics https://effect.website/docs/observability/metrics/

I tried to use it like this:

export const processAudit = Effect.fn((auditRequest: typeof AuditRequestSchema.Type) => {
  return Effect.gen(function* () {
    const auditTimer = auditProcessTimer(auditRequest.auditId);

    yield* Effect.log(`Starting processing ${auditRequest.auditId}`);

    const result = yield* Metric.trackDuration(
      runAudit(auditRequest.auditDetails),
      auditProcessTimer(auditRequest.auditId),
    ).pipe();

    const processResult = yield* Metric.value(auditTimer);

    yield* Effect.log(`Completed processing ${auditRequest.auditId} in ${processResult.sum}`);
    yield* Effect.log(processResult);

    return {
      result,
      duration: processResult.sum,
    };
  });
});


But the result i am getting is
  HistogramState {
    buckets: [
      [ 0.5, 0 ],        [ 1, 0 ],          [ 2, 0 ],
      [ 4, 0 ],          [ 8, 0 ],          [ 16, 0 ],
      [ 32, 0 ],         [ 64, 0 ],         [ 128, 0 ],
      [ 256, 0 ],        [ 512, 0 ],        [ 1024, 0 ],
      [ 2048, 0 ],       [ 4096, 0 ],       [ 8192, 0 ],
      [ 16384, 0 ],      [ 32768, 0 ],      [ 65536, 0 ],
      [ 131072, 0 ],     [ 262144, 0 ],     [ 524288, 0 ],
      ...
      [ 4294967296, 0 ], [ Infinity, 0 ]
    ],
    count: 0,
    min: 1.7976931348623157e+308,
    max: 5e-324,
    sum: 0,
    [Symbol(effect/MetricState)]: { _A: [Function: _A] },
    [Symbol(effect/MetricState/Histogram)]: Symbol(effect/MetricState/Histogram)
  }


I guess there is something i'm missing but sum is 0 and the function clearly took much longer.
In the future i might also want to measure the memory consumption of this function.

I'm pretty new to the world of effect but it seems very cool!
Effect Documentation
Effect Metrics provides powerful monitoring tools, including counters, gauges, histograms, summaries, and frequencies, to track your application's performance and behavior.
Metrics in Effect
Was this page helpful?