Sha256: 2d02ba216cc116552f109691baacbbb7563e17db4adf703b7379e528e6865180

Contents?: true

Size: 1.38 KB

Versions: 8

Compression:

Stored size: 1.38 KB

Contents

##
# Benchmark for speed of selecting what view to use.
class ViewSelection
  Accomplishment = Struct.new(:name, :description)
  Person = Struct.new(:first_name, :last_name, :accomplishments)

  MyPerson = Person.new(
    'Joeseph',
    'Biden',
    [
      Accomplishment.new('Became President', 'Won a Presidential Election'),
      Accomplishment.new('Oldest President', 'Oldest man to be elected president at time of election'),
      Accomplishment.new('Became Senator', 'Got Elected to the Senate'),
      Accomplishment.new('Youngest Senator', 'Youngest person elected Senator at time of election')
    ]
  )

  AccomplishmentSerializer = SoberSwag::OutputObject.define do
    field :name, primitive(:String)

    view :detail do
      field :description, primitive(:String)
    end
  end

  PersonSerializer = SoberSwag::OutputObject.define do
    field :first_name, primitive(:String)
    field :last_name, primitive(:String)

    # make a bunch of dummy views
    1.upto(10).each { |n| view(:"view_#{n}") {} }

    view :detail do
      field :accomplishments, AccomplishmentSerializer.view(:detail)
    end

    1.upto(10).each { |n| view(:"view_after_#{n}") {} }
  end

  Bench.report 'View Selection' do |bm|
    bm.report('With no view') { PersonSerializer.serialize(MyPerson) }

    bm.report('With a view') { PersonSerializer.serialize(MyPerson, { view: :detail }) }

    bm.compare!
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
sober_swag-0.25.2 bench/benchmarks/view_selection.rb
sober_swag-0.25.1 bench/benchmarks/view_selection.rb
sober_swag-0.25.0 bench/benchmarks/view_selection.rb
sober_swag-0.24.1 bench/benchmarks/view_selection.rb
sober_swag-0.24.0 bench/benchmarks/view_selection.rb
sober_swag-0.23.0 bench/benchmarks/view_selection.rb
sober_swag-0.22.0 bench/benchmarks/view_selection.rb
sober_swag-0.21.0 bench/benchmarks/view_selection.rb