|
| 1 | +require_relative './benchmarking_support' |
| 2 | +require_relative './app' |
| 3 | + |
| 4 | +time = 10 |
| 5 | +disable_gc = true |
| 6 | +ActiveModelSerializers.config.key_transform = :unaltered |
| 7 | + |
| 8 | +module AmsBench |
| 9 | + module Api |
| 10 | + module V1 |
| 11 | + class PrimaryResourceSerializer < ActiveModel::Serializer |
| 12 | + attributes :title, :body |
| 13 | + |
| 14 | + has_many :has_many_relationships |
| 15 | + end |
| 16 | + |
| 17 | + class HasManyRelationshipSerializer < ActiveModel::Serializer |
| 18 | + attribute :body |
| 19 | + end |
| 20 | + end |
| 21 | + end |
| 22 | + class PrimaryResourceSerializer < ActiveModel::Serializer |
| 23 | + attributes :title, :body |
| 24 | + |
| 25 | + has_many :has_many_relationships |
| 26 | + |
| 27 | + class HasManyRelationshipSerializer < ActiveModel::Serializer |
| 28 | + attribute :body |
| 29 | + end |
| 30 | + end |
| 31 | +end |
| 32 | + |
| 33 | +resource = PrimaryResource.new( |
| 34 | + id: 1, |
| 35 | + title: 'title', |
| 36 | + body: 'body', |
| 37 | + has_many_relationships: [ |
| 38 | + HasManyRelationship.new(id: 1, body: 'body1'), |
| 39 | + HasManyRelationship.new(id: 2, body: 'body1') |
| 40 | + ] |
| 41 | +) |
| 42 | + |
| 43 | +serialization = lambda do |
| 44 | + ActiveModelSerializers::SerializableResource.new(resource, serializer: AmsBench::PrimaryResourceSerializer).as_json |
| 45 | + ActiveModelSerializers::SerializableResource.new(resource, namespace: AmsBench::Api::V1).as_json |
| 46 | + ActiveModelSerializers::SerializableResource.new(resource).as_json |
| 47 | +end |
| 48 | + |
| 49 | +def clear_cache |
| 50 | + AmsBench::PrimaryResourceSerializer.serializers_cache.clear |
| 51 | + AmsBench::Api::V1::PrimaryResourceSerializer.serializers_cache.clear |
| 52 | + ActiveModel::Serializer.serializers_cache.clear |
| 53 | +end |
| 54 | + |
| 55 | +configurable = lambda do |
| 56 | + clear_cache |
| 57 | + Benchmark.ams('Configurable Lookup Chain', time: time, disable_gc: disable_gc, &serialization) |
| 58 | +end |
| 59 | + |
| 60 | +old = lambda do |
| 61 | + clear_cache |
| 62 | + module ActiveModel |
| 63 | + class Serializer |
| 64 | + def self.serializer_lookup_chain_for(klass, namespace = nil) |
| 65 | + chain = [] |
| 66 | + |
| 67 | + resource_class_name = klass.name.demodulize |
| 68 | + resource_namespace = klass.name.deconstantize |
| 69 | + serializer_class_name = "#{resource_class_name}Serializer" |
| 70 | + |
| 71 | + chain.push("#{namespace}::#{serializer_class_name}") if namespace |
| 72 | + chain.push("#{name}::#{serializer_class_name}") if self != ActiveModel::Serializer |
| 73 | + chain.push("#{resource_namespace}::#{serializer_class_name}") |
| 74 | + chain |
| 75 | + end |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + Benchmark.ams('Old Lookup Chain (v0.10)', time: time, disable_gc: disable_gc, &serialization) |
| 80 | +end |
| 81 | + |
| 82 | +configurable.call |
| 83 | +old.call |
0 commit comments