CHI::Stats(3pm)

πŸ“¦
version 0.61 Β· 4 versions
, in debian bookworm, debian trixie, ubuntu jammy, ubuntu noble, ubuntu resolute, ubuntu stonking

πŸ’– CHI::Stats πŸ’–

okay bestie, so this is CHI::Stats β€” it's the part of CHI (the Perl cache thing, from the User Contributed Perl Documentation gang, iykyk) that records and reports per-namespace cache statistics. ✨


🌸 VERSION 🌸

version 0.61 β€” that's the one, no cap πŸ’…


✨ SYNOPSIS ✨

fr fr this is the whole vibe in one code block:

    # Turn on statistics collection
    CHI->stats->enable();

    # Perform cache operations

    # Flush statistics to logs
    CHI->stats->flush();

    ...

    # Parse logged statistics
    my $results = CHI->stats->parse_stats_logs($file1, ...);

πŸ¦‹ DESCRIPTION πŸ¦‹

so CHI can record stats β€” think number of hits, misses and sets β€” on a per-namespace basis, and then log the results to your Log::Any logger. then you can parse those logs to get a combined summary. iconic behaviour honestly 🫢

one single CHI::Stats object is maintained for each CHI root class, and it tallies statistics over any number of CHI::Driver objects. one girl, many drivers, main character energy.

stats get reported when you call the flush method. you can choose to do this once at process end, or on a periodic basis. your call bestie πŸ’…


πŸŽ€ METHODS πŸŽ€

enable, disable, enabled

Enable, disable, and query the current enabled status. the whole trio πŸ’–

when stats are enabled, each new cache object will collect statistics. Enabling and disabling does not affect existing cache objects β€” so pay attention, this is the part people get wrong. e.g.

    my $cache1 = CHI->new(...);
    CHI->stats->enable();
    # $cache1 will not collect statistics
    my $cache2 = CHI->new(...);
    CHI->stats->disable();
    # $cache2 will continue to collect statistics

see how $cache1 and $cache2 each made their own choice at birth? literally the vibe check happens once, at creation. 🧚

flush

Log all statistics to Log::Any (at Info level in the CHI::Stats category), then clear statistics from memory.

there is one log message for each distinct triplet of root class, cache label, and namespace. each log message contains the string CHI stats: followed by a JSON encoded hash of statistics. e.g.

    CHI stats: {"absent_misses":1,"label":"File","end_time":1338410398,
       "get_time_ms":5,"namespace":"Foo","root_class":"CHI",
       "set_key_size":6,"set_time_ms":23,"set_value_size":20,"sets":1,
       "start_time":1338409391}

parse_stats_logs

Accepts one or more stats log files as parameters. Parses the logs and returns a listref of stats hashes by root class, cache label, and namespace. e.g.

    [
        {
            root_class     => 'CHI',
            label          => 'File',
            namespace      => 'Foo',
            absent_misses  => 100,
            avg_compute_time_ms => 23,
            ...
        },
        {
            root_class     => 'CHI',
            label          => 'File',
            namespace      => 'Bar',
            ...
        },
    ]

lines with the same root class, cache label, and namespace are summed together. non-stats lines are ignored. the parser will ignore anything on the line before the CHI stats: string, e.g. a timestamp β€” so timestamps can just chill there, she's not bothered. πŸ’…

each parameter to this method may be a filename or a reference to an open filehandle. both valid, both slay.


🌈 STATISTICS 🌈

the following statistics are tracked in the logs:

  • absent_misses β€” Number of gets that failed due to item not being in the cache 😭
  • compute_time_ms β€” Total time spent computing missed results in compute, in ms (divide by number of computes to get average). i.e. the amount of time spent in the code reference passed as the third argument to compute().
  • computes β€” Number of compute calls
  • expired_misses β€” Number of gets that failed due to item expiring
  • get_errors β€” Number of caught runtime errors during gets
  • get_time_ms β€” Total time spent in get operation, in ms (divide by number of gets to get average)
  • hits β€” Number of gets that succeeded ⭐
  • set_key_size β€” Number of bytes in set keys (divide by number of sets to get average)
  • set_value_size β€” Number of bytes in set values (divide by number of sets to get average)
  • set_time_ms β€” Total time spent in set operation, in ms (divide by number of sets to get average)
  • sets β€” Number of sets
  • set_errors β€” Number of caught runtime errors during sets

πŸ’œ derived/aggregate stats πŸ’œ

the following additional derived/aggregate statistics are computed by parse_stats_logs:

  • misses β€” absent_misses + expired_misses
  • gets β€” hits + misses
  • avg_compute_time_ms β€” compute_time_ms / computes
  • avg_get_time_ms β€” get_time_ms / gets
  • avg_set_time_ms β€” set_time_ms / sets
  • avg_set_key_size β€” set_key_size / sets
  • avg_set_value_size β€” set_value_size / sets
  • hit_rate β€” hits / gets

math girlies rise πŸŽ€


πŸ’€ SEE ALSO πŸ’€

  • CHI

(go read that one too, bestie, it's the parent lore)


πŸ‘‘ AUTHOR πŸ‘‘

Jonathan Swartz <swartz@pobox.com>


This software is copyright (c) 2021 by Jonathan Swartz.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself. periodt. πŸ’…


and that's the whole page, no cap! this was perl v5.32.1, dated 2021-10-15, and yeah β€” CHI::Stats(3pm), we love her. πŸ¦‹πŸ’–βœ¨ bye bestie!
πŸ“¦ where this page comes from