Sha256: 850678961708d62565e4b0680fef2cc361faa98ab7fe4302c1343c0d6718cebc

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

$LOAD_PATH.push File.expand_path('../../lib', __FILE__)

require 'mongoid'
require 'mongoid/history'

require 'benchmark/ips'
require './perf/gc_suite'

Mongoid.connect_to('mongoid_history_perf_test')
Mongo::Logger.logger.level = ::Logger::FATAL
Mongoid.purge!

Attributes = Mongoid::History::Attributes

module ZeroPointEight
  class Create < Attributes::Create
    def attributes
      @attributes = {}
      trackable.attributes.each do |k, v|
        next unless trackable_class.tracked_field?(k, :create)
        modified = if changes[k]
                     changes[k].class == Array ? changes[k].last : changes[k]
                   else
                     v
                   end
        @attributes[k] = [nil, format_field(k, modified)]
      end
      insert_embeds_one_changes
      insert_embeds_many_changes
      @attributes
    end
  end
end

class Person
  include Mongoid::Document
  include Mongoid::History::Trackable

  field :first_name, type: String
  field :last_name, type: String
  field :birth_date, type: Date
  field :title, type: String

  track_history on: %i[first_name last_name birth_date]
end

new_person = Person.new(first_name: 'Eliot', last_name: 'Horowitz', birth_date: '1981-05-01', title: 'CTO')

Benchmark.ips do |bm|
  bm.config(suite: GCSuite.new)

  bm.report('HEAD') do
    Attributes::Create.new(new_person).attributes
  end

  bm.report('v0.8.2') do
    ZeroPointEight::Create.new(new_person).attributes
  end

  bm.report('v0.5.0') do
    new_person.attributes.each_with_object({}) { |(k, v), h| h[k] = [nil, v] }.select { |k, _| new_person.class.tracked_field?(k, :create) }
  end

  bm.compare!
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-history-0.8.5 perf/benchmark_modified_attributes_for_create.rb