Sha256: 3fd0fba284ec0168e598652a3505cd93527a9939e516fb511f161513be1610be

Contents?: true

Size: 1.11 KB

Versions: 2

Compression:

Stored size: 1.11 KB

Contents

# frozen_string_literal: true

module Benchmark
  class Malloc
    class AllocationSet
      include Enumerable

      attr_reader :allocations

      def initialize(allocations)
        @allocations = allocations
      end

      def each(&block)
        return to_enum(:each) unless block
        @allocations.each(&block)
      end

      # @api public
      def total_objects
        @allocations.size
      end

      # @api public
      def total_memory
        @allocations.reduce(0) { |acc, alloc| acc + alloc.memsize }
      end

      # @api public
      def count_objects
        @allocations.
          map { |alloc| alloc.object.class }.
          each_with_object(Hash.new(0)) { |name, h| h[name] += 1 }
      end

      # @api public
      def count_memory
        @allocations.
          map { |alloc| [alloc.object.class, alloc.memsize] }.
          each_with_object(Hash.new(0)) { |(name, mem), h| h[name] += mem }
      end

      def filter(*class_names)
        @allocations.
          select { |alloc| class_names.include?(alloc.object.class) }
      end
    end # AllocationSet
  end # Malloc
end # Benchmark

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
benchmark-malloc-0.2.0 lib/benchmark/malloc/allocation_set.rb
benchmark-malloc-0.1.0 lib/benchmark/malloc/allocation_set.rb