panko_serializer 0.8.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (144) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +8 -6
  3. data/lib/panko/array_serializer.rb +55 -16
  4. data/lib/panko/code_gen/active_record/access_classifier.rb +65 -0
  5. data/lib/panko/code_gen/active_record/define_attribute_methods.rb +13 -0
  6. data/lib/panko/code_gen/code_builder.rb +32 -0
  7. data/lib/panko/code_gen/compile_cache.rb +28 -0
  8. data/lib/panko/code_gen/compiler.rb +56 -0
  9. data/lib/panko/code_gen/config.rb +79 -0
  10. data/lib/panko/code_gen/datetime_format.rb +38 -0
  11. data/lib/panko/code_gen/descriptor.rb +136 -0
  12. data/lib/panko/code_gen/descriptor_builder.rb +166 -0
  13. data/lib/panko/code_gen/dump.rb +52 -0
  14. data/lib/panko/code_gen/errors.rb +21 -0
  15. data/lib/panko/code_gen/filter.rb +29 -0
  16. data/lib/panko/code_gen/filter_adapter.rb +44 -0
  17. data/lib/panko/code_gen/filters/indexed.rb +62 -0
  18. data/lib/panko/code_gen/filters/none.rb +19 -0
  19. data/lib/panko/code_gen/generator.rb +23 -0
  20. data/lib/panko/code_gen/generators/banner.rb +27 -0
  21. data/lib/panko/code_gen/generators/class_emitter.rb +120 -0
  22. data/lib/panko/code_gen/generators/cycle_membership.rb +53 -0
  23. data/lib/panko/code_gen/generators/descriptor_walk.rb +22 -0
  24. data/lib/panko/code_gen/generators/fanout.rb +60 -0
  25. data/lib/panko/code_gen/generators/field_index.rb +31 -0
  26. data/lib/panko/code_gen/generators/field_walk.rb +38 -0
  27. data/lib/panko/code_gen/generators/generated_names.rb +73 -0
  28. data/lib/panko/code_gen/generators/hash_sink.rb +217 -0
  29. data/lib/panko/code_gen/generators/json_sink.rb +290 -0
  30. data/lib/panko/code_gen/generators/record_access/generic.rb +92 -0
  31. data/lib/panko/code_gen/generators/record_access/specialized.rb +90 -0
  32. data/lib/panko/code_gen/generators/release.rb +60 -0
  33. data/lib/panko/code_gen/generators/sink.rb +80 -0
  34. data/lib/panko/code_gen/instance_pool.rb +27 -0
  35. data/lib/panko/code_gen/runtime.rb +27 -0
  36. data/lib/panko/code_gen/serializer_cache.rb +242 -0
  37. data/lib/panko/code_gen/validators/callable_arity.rb +42 -0
  38. data/lib/panko/code_gen/validators/name_uniqueness.rb +44 -0
  39. data/lib/panko/code_gen/validators/source_resolution.rb +48 -0
  40. data/lib/panko/code_gen/validators/validator.rb +19 -0
  41. data/lib/panko/code_gen/writers_pool.rb +51 -0
  42. data/lib/panko/code_gen.rb +82 -0
  43. data/lib/panko/compile_all.rb +80 -0
  44. data/lib/panko/compile_all_result.rb +21 -0
  45. data/lib/panko/config.rb +71 -0
  46. data/lib/panko/descriptor.rb +93 -0
  47. data/lib/panko/response.rb +40 -8
  48. data/lib/panko/serializer.rb +175 -91
  49. data/lib/panko/serializer_resolver.rb +2 -8
  50. data/lib/panko/version.rb +1 -1
  51. data/lib/panko_serializer.rb +3 -5
  52. metadata +49 -99
  53. data/.clang-format +0 -3
  54. data/.github/dependabot.yml +0 -6
  55. data/.github/workflows/database_matrix.yml +0 -85
  56. data/.github/workflows/docs.yml +0 -39
  57. data/.github/workflows/lint.yml +0 -30
  58. data/.github/workflows/tests.yml +0 -34
  59. data/.gitignore +0 -20
  60. data/.rspec +0 -2
  61. data/.rubocop.yml +0 -37
  62. data/Appraisals +0 -28
  63. data/Gemfile +0 -37
  64. data/Rakefile +0 -93
  65. data/benchmarks/BENCHMARKS.md +0 -48
  66. data/benchmarks/allocs.rb +0 -23
  67. data/benchmarks/app.rb +0 -11
  68. data/benchmarks/benchmarking_support.rb +0 -44
  69. data/benchmarks/bm_ams_0_10.rb +0 -43
  70. data/benchmarks/bm_object_writer.rb +0 -65
  71. data/benchmarks/bm_panko_json.rb +0 -60
  72. data/benchmarks/bm_panko_object.rb +0 -46
  73. data/benchmarks/bm_plain_object.rb +0 -99
  74. data/benchmarks/bm_serialization_descriptor.rb +0 -76
  75. data/benchmarks/bm_serializer_resolver.rb +0 -22
  76. data/benchmarks/bm_to_object.rb +0 -81
  77. data/benchmarks/profile.rb +0 -86
  78. data/benchmarks/sanity.rb +0 -83
  79. data/benchmarks/setup.rb +0 -56
  80. data/benchmarks/type_casts/bm_active_record.rb +0 -58
  81. data/benchmarks/type_casts/bm_panko.rb +0 -65
  82. data/benchmarks/type_casts/support.rb +0 -27
  83. data/docs/docs/associations.md +0 -107
  84. data/docs/docs/attributes.md +0 -150
  85. data/docs/docs/design-choices.md +0 -127
  86. data/docs/docs/getting-started.md +0 -70
  87. data/docs/docs/introduction.md +0 -13
  88. data/docs/docs/performance.md +0 -32
  89. data/docs/docs/response-bag.md +0 -83
  90. data/docs/docusaurus.config.js +0 -86
  91. data/docs/package-lock.json +0 -17872
  92. data/docs/package.json +0 -18
  93. data/docs/sidebars.json +0 -15
  94. data/docs/src/css/customTheme.css +0 -9
  95. data/docs/static/.DS_Store +0 -0
  96. data/docs/static/CNAME +0 -1
  97. data/docs/static/css/custom.css +0 -51
  98. data/docs/static/img/favicon.ico +0 -0
  99. data/docs/static/img/oss_logo.png +0 -0
  100. data/docs/static/img/undraw_code_review.svg +0 -1
  101. data/docs/static/img/undraw_monitor.svg +0 -1
  102. data/docs/static/img/undraw_note_list.svg +0 -1
  103. data/docs/static/img/undraw_online.svg +0 -1
  104. data/docs/static/img/undraw_open_source.svg +0 -1
  105. data/docs/static/img/undraw_operating_system.svg +0 -1
  106. data/docs/static/img/undraw_react.svg +0 -1
  107. data/docs/static/img/undraw_tweetstorm.svg +0 -1
  108. data/docs/static/img/undraw_youtube_tutorial.svg +0 -1
  109. data/docs/static/index.html +0 -14
  110. data/ext/panko_serializer/attributes_writer/active_record.c +0 -181
  111. data/ext/panko_serializer/attributes_writer/active_record.h +0 -17
  112. data/ext/panko_serializer/attributes_writer/attributes_writer.c +0 -55
  113. data/ext/panko_serializer/attributes_writer/attributes_writer.h +0 -35
  114. data/ext/panko_serializer/attributes_writer/common.c +0 -10
  115. data/ext/panko_serializer/attributes_writer/common.h +0 -9
  116. data/ext/panko_serializer/attributes_writer/hash.c +0 -13
  117. data/ext/panko_serializer/attributes_writer/hash.h +0 -7
  118. data/ext/panko_serializer/attributes_writer/plain.c +0 -13
  119. data/ext/panko_serializer/attributes_writer/plain.h +0 -7
  120. data/ext/panko_serializer/attributes_writer/type_cast/time_conversion.c +0 -156
  121. data/ext/panko_serializer/attributes_writer/type_cast/time_conversion.h +0 -10
  122. data/ext/panko_serializer/attributes_writer/type_cast/type_cast.c +0 -398
  123. data/ext/panko_serializer/attributes_writer/type_cast/type_cast.h +0 -81
  124. data/ext/panko_serializer/common.h +0 -9
  125. data/ext/panko_serializer/extconf.rb +0 -28
  126. data/ext/panko_serializer/panko_serializer.c +0 -181
  127. data/ext/panko_serializer/panko_serializer.h +0 -12
  128. data/ext/panko_serializer/serialization_descriptor/association.c +0 -90
  129. data/ext/panko_serializer/serialization_descriptor/association.h +0 -20
  130. data/ext/panko_serializer/serialization_descriptor/attribute.c +0 -96
  131. data/ext/panko_serializer/serialization_descriptor/attribute.h +0 -27
  132. data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.c +0 -177
  133. data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.h +0 -30
  134. data/gemfiles/7.2.0.gemfile +0 -41
  135. data/gemfiles/7.2.0.gemfile.lock +0 -221
  136. data/gemfiles/8.0.0.gemfile +0 -41
  137. data/gemfiles/8.0.0.gemfile.lock +0 -239
  138. data/gemfiles/8.1.0.gemfile +0 -41
  139. data/gemfiles/8.1.0.gemfile.lock +0 -248
  140. data/lib/panko/association.rb +0 -17
  141. data/lib/panko/attribute.rb +0 -29
  142. data/lib/panko/object_writer.rb +0 -68
  143. data/lib/panko/serialization_descriptor.rb +0 -180
  144. data/panko_serializer.gemspec +0 -36
data/benchmarks/allocs.rb DELETED
@@ -1,23 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "benchmarking_support"
4
- require_relative "app"
5
- require_relative "setup"
6
-
7
- require "memory_profiler"
8
-
9
- class PostFastSerializer < Panko::Serializer
10
- attributes :id, :body, :title, :author_id
11
- end
12
-
13
- def count_allocs(&)
14
- memory_report = MemoryProfiler.report(&)
15
- puts memory_report.pretty_print
16
- end
17
-
18
- posts = Post.all.to_a
19
- merged_options = {}.merge(each_serializer: PostFastSerializer)
20
- posts_array_serializer = Panko::ArraySerializer.new([], merged_options)
21
-
22
- # prints out 18402
23
- count_allocs { posts_array_serializer.serialize_to_json posts }
data/benchmarks/app.rb DELETED
@@ -1,11 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bundler/setup"
4
-
5
- require "active_model"
6
- require "active_record"
7
- require "active_support"
8
- require "active_support/json"
9
-
10
- require "active_model_serializers"
11
- require "panko_serializer"
@@ -1,44 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "benchmark/ips"
4
- require "json"
5
- require "memory_profiler"
6
-
7
- module Benchmark
8
- module Runner
9
- def data
10
- posts = Post.all.includes(:author).to_a
11
- posts_50 = posts.first(50).to_a
12
- posts_single = posts.first(1).to_a
13
- {all: posts, small: posts_50, single: posts_single}
14
- end
15
-
16
- def run(label = nil, time: 10, disable_gc: true, warmup: 3, &block)
17
- fail ArgumentError.new, "block should be passed" unless block
18
-
19
- GC.start
20
-
21
- if disable_gc
22
- GC.disable
23
- else
24
- GC.enable
25
- end
26
-
27
- memory_report = MemoryProfiler.report(&block)
28
-
29
- report = Benchmark.ips(time, warmup, true) do |x|
30
- x.report(label) { yield }
31
- end
32
-
33
- results = {
34
- label: label,
35
- ips: ActiveSupport::NumberHelper.number_to_delimited(report.entries.first.ips.round(2)),
36
- allocs: "#{memory_report.total_allocated}/#{memory_report.total_retained}"
37
- }.to_json
38
-
39
- puts results
40
- end
41
- end
42
-
43
- extend Benchmark::Runner
44
- end
@@ -1,43 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "benchmarking_support"
4
- require_relative "app"
5
- require_relative "setup"
6
-
7
- # disable logging for benchmarks
8
- ActiveModelSerializers.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new(File::NULL))
9
-
10
- class AmsAuthorFastSerializer < ActiveModel::Serializer
11
- attributes :id, :name
12
- end
13
-
14
- class AmsPostFastSerializer < ActiveModel::Serializer
15
- attributes :id, :body, :title, :author_id, :created_at
16
- end
17
-
18
- class AmsPostWithHasOneFastSerializer < ActiveModel::Serializer
19
- attributes :id, :body, :title, :author_id
20
-
21
- has_one :author, serializer: AmsAuthorFastSerializer
22
- end
23
-
24
- def benchmark_ams(prefix, serializer, options = {})
25
- merged_options = options.merge(each_serializer: serializer)
26
-
27
- posts = Benchmark.data[:all]
28
-
29
- Benchmark.run("AMS_#{prefix}_Posts_#{posts.count}") do
30
- ActiveModelSerializers::SerializableResource.new(posts, merged_options).to_json
31
- end
32
-
33
- posts_50 = Benchmark.data[:small]
34
-
35
- Benchmark.run("AMS_#{prefix}_Posts_50") do
36
- ActiveModelSerializers::SerializableResource.new(posts_50, merged_options).to_json
37
- end
38
- end
39
-
40
- benchmark_ams "Attributes_Simple", AmsPostFastSerializer
41
- benchmark_ams "Attributes_HasOne", AmsPostWithHasOneFastSerializer
42
- benchmark_ams "Attributes_Except", AmsPostWithHasOneFastSerializer, except: [:title]
43
- benchmark_ams "Attributes_Only", AmsPostWithHasOneFastSerializer, only: [:id, :body, :author_id, :author]
@@ -1,65 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "benchmarking_support"
4
- require_relative "app"
5
-
6
- Benchmark.run("ObjectWriter_OneProperty_PushValue") do
7
- writer = Panko::ObjectWriter.new
8
-
9
- writer.push_object
10
- writer.push_value "value1", "key1"
11
- writer.pop
12
-
13
- writer.output
14
- end
15
-
16
- Benchmark.run("ObjectWriter_TwoProperty_PushValue") do
17
- writer = Panko::ObjectWriter.new
18
-
19
- writer.push_object
20
- writer.push_value "value1", "key1"
21
- writer.push_value "value2", "key2"
22
- writer.pop
23
-
24
- writer.output
25
- end
26
-
27
- Benchmark.run("ObjectWriter_OneProperty_PushValuePushKey") do
28
- writer = Panko::ObjectWriter.new
29
-
30
- writer.push_object
31
- writer.push_key "key1"
32
- writer.push_value "value1"
33
- writer.pop
34
-
35
- writer.output
36
- end
37
-
38
- Benchmark.run("ObjectWriter_TwoProperty_PushValuePushKey") do
39
- writer = Panko::ObjectWriter.new
40
-
41
- writer.push_object
42
- writer.push_key "key1"
43
- writer.push_value "value1"
44
-
45
- writer.push_key "key2"
46
- writer.push_value "value2"
47
- writer.pop
48
-
49
- writer.output
50
- end
51
-
52
- Benchmark.run("ObjectWriter_NestedObject") do
53
- writer = Panko::ObjectWriter.new
54
-
55
- writer.push_object
56
- writer.push_value "value1", "key1"
57
-
58
- writer.push_object "key2"
59
- writer.push_value "value2", "key2"
60
- writer.pop
61
-
62
- writer.pop
63
-
64
- writer.output
65
- end
@@ -1,60 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "benchmarking_support"
4
- require_relative "app"
5
- require_relative "setup"
6
-
7
- class AuthorFastSerializer < Panko::Serializer
8
- attributes :id, :name
9
- end
10
-
11
- class PostFastSerializer < Panko::Serializer
12
- attributes :id, :body, :title, :author_id, :created_at
13
- end
14
-
15
- class PostFastWithJsonSerializer < Panko::Serializer
16
- attributes :id, :body, :title, :author_id, :created_at, :data
17
- end
18
-
19
- class PostFastWithMethodCallSerializer < Panko::Serializer
20
- attributes :id, :body, :title, :author_id, :method_call
21
-
22
- def method_call
23
- object.id * 2
24
- end
25
- end
26
-
27
- class PostWithHasOneFastSerializer < Panko::Serializer
28
- attributes :id, :body, :title, :author_id
29
-
30
- has_one :author, serializer: AuthorFastSerializer
31
- end
32
-
33
- class AuthorWithHasManyFastSerializer < Panko::Serializer
34
- attributes :id, :name
35
-
36
- has_many :posts, serializer: PostFastSerializer
37
- end
38
-
39
- def benchmark(prefix, serializer, options = {})
40
- posts = Benchmark.data[:all]
41
-
42
- merged_options = options.merge(each_serializer: serializer)
43
-
44
- Benchmark.run("Panko_ActiveRecord_#{prefix}_Posts_#{posts.count}") do
45
- Panko::ArraySerializer.new(posts, merged_options).to_json
46
- end
47
-
48
- posts_50 = Benchmark.data[:small]
49
-
50
- Benchmark.run("Panko_ActiveRecord_#{prefix}_Posts_50") do
51
- Panko::ArraySerializer.new(posts_50, merged_options).to_json
52
- end
53
- end
54
-
55
- benchmark "Simple", PostFastSerializer
56
- benchmark "SimpleWithJson", PostFastWithJsonSerializer
57
- benchmark "HasOne", PostWithHasOneFastSerializer
58
- benchmark "SimpleWithMethodCall", PostFastWithMethodCallSerializer
59
- benchmark "Except", PostWithHasOneFastSerializer, except: [:title]
60
- benchmark "Only", PostWithHasOneFastSerializer, only: [:id, :body, :author_id, :author]
@@ -1,46 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "benchmarking_support"
4
- require_relative "app"
5
- require_relative "setup"
6
-
7
- class AuthorFastSerializer < Panko::Serializer
8
- attributes :id, :name
9
- end
10
-
11
- class PostFastSerializer < Panko::Serializer
12
- attributes :id, :body, :title, :author_id
13
- end
14
-
15
- class PostWithHasOneFastSerializer < Panko::Serializer
16
- attributes :id, :body, :title, :author_id
17
-
18
- has_one :author, serializer: AuthorFastSerializer
19
- end
20
-
21
- class AuthorWithHasManyFastSerializer < Panko::Serializer
22
- attributes :id, :name
23
-
24
- has_many :posts, serializer: PostFastSerializer
25
- end
26
-
27
- def benchmark(prefix, serializer, options = {})
28
- posts = Benchmark.data[:all]
29
-
30
- merged_options = options.merge(each_serializer: serializer)
31
-
32
- Benchmark.run("Panko_ActiveRecord_#{prefix}_Posts_#{posts.count}") do
33
- Panko::ArraySerializer.new(posts, merged_options).to_a
34
- end
35
-
36
- posts_50 = Benchmark.data[:small]
37
-
38
- Benchmark.run("Panko_ActiveRecord_#{prefix}_Posts_50") do
39
- Panko::ArraySerializer.new(posts_50, merged_options).to_a
40
- end
41
- end
42
-
43
- benchmark "Simple", PostFastSerializer
44
- benchmark "HasOne", PostWithHasOneFastSerializer
45
- benchmark "Except", PostWithHasOneFastSerializer, except: [:title]
46
- benchmark "Only", PostWithHasOneFastSerializer, only: [:id, :body, :author_id, :author]
@@ -1,99 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "benchmarking_support"
4
- require_relative "app"
5
-
6
- class AuthorFastSerializer < Panko::Serializer
7
- attributes :id, :name
8
- end
9
-
10
- class PostFastSerializer < Panko::Serializer
11
- attributes :id, :body, :title, :author_id, :created_at
12
- end
13
-
14
- class PostFastWithMethodCallSerializer < Panko::Serializer
15
- attributes :id, :body, :title, :author_id, :method_call
16
-
17
- def method_call
18
- object.id * 2
19
- end
20
- end
21
-
22
- class PostWithHasOneFastSerializer < Panko::Serializer
23
- attributes :id, :body, :title, :author_id
24
-
25
- has_one :author, serializer: AuthorFastSerializer
26
- end
27
-
28
- class AuthorWithHasManyFastSerializer < Panko::Serializer
29
- attributes :id, :name
30
-
31
- has_many :posts, serializer: PostFastSerializer
32
- end
33
-
34
- class Post
35
- attr_accessor :id, :body, :title, :created_at, :author_id
36
- attr_reader :author
37
-
38
- def self.create(attrs)
39
- p = Post.new
40
- attrs.each do |k, v|
41
- p.send :"#{k}=", v
42
- end
43
- p
44
- end
45
-
46
- def author=(author)
47
- @author = author
48
- @author_id = author.id
49
- end
50
- end
51
-
52
- class Author
53
- attr_accessor :id, :name
54
-
55
- def self.create(attrs)
56
- a = Author.new
57
- attrs.each do |k, v|
58
- a.send :"#{k}=", v
59
- end
60
- a
61
- end
62
- end
63
-
64
- def benchmark_data
65
- posts = []
66
- ENV.fetch("ITEMS_COUNT", "2300").to_i.times do |i|
67
- posts << Post.create(
68
- id: i,
69
- body: "something about how password restrictions are evil, and less secure, and with the math to prove it.",
70
- title: "Your bank is does not know how to do security",
71
- author: Author.create(id: i, name: "Preston Sego")
72
- )
73
- end
74
-
75
- {all: posts, small: posts.first(50)}
76
- end
77
-
78
- def benchmark(prefix, serializer, options = {})
79
- data = benchmark_data
80
- posts = data[:all]
81
-
82
- merged_options = options.merge(each_serializer: serializer)
83
-
84
- Benchmark.run("Panko_Plain_#{prefix}_Posts_#{posts.count}") do
85
- Panko::ArraySerializer.new(posts, merged_options).to_json
86
- end
87
-
88
- posts_50 = benchmark_data[:small]
89
-
90
- Benchmark.run("Panko_Plain_#{prefix}_Posts_50") do
91
- Panko::ArraySerializer.new(posts_50, merged_options).to_json
92
- end
93
- end
94
-
95
- benchmark "Simple", PostFastSerializer
96
- benchmark "HasOne", PostWithHasOneFastSerializer
97
- benchmark "SimpleWithMethodCall", PostFastWithMethodCallSerializer
98
- benchmark "Except", PostWithHasOneFastSerializer, except: [:title]
99
- benchmark "Only", PostWithHasOneFastSerializer, only: [:id, :body, :author_id, :author]
@@ -1,76 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "benchmarking_support"
4
- require "active_support"
5
-
6
- require "panko_serializer"
7
-
8
- def generate_attributes(count)
9
- (1..count).map { |i| :"attr_#{i}" }
10
- end
11
-
12
- class LeafASerializer < Panko::Serializer
13
- attributes(*generate_attributes(5))
14
- end
15
-
16
- class LeafBSerializer < Panko::Serializer
17
- attributes(*generate_attributes(6))
18
- end
19
-
20
- class ChildrenSerializer < Panko::Serializer
21
- attributes(*generate_attributes(28))
22
-
23
- has_one :leaf_a, serializer: LeafASerializer
24
- has_one :leaf_b, serializer: LeafBSerializer
25
-
26
- def attr_1
27
- end
28
-
29
- def attr_2
30
- end
31
-
32
- def attr_3
33
- end
34
-
35
- def attr_4
36
- end
37
-
38
- def attr_5
39
- end
40
- end
41
-
42
- class ParentSerializer < Panko::Serializer
43
- attributes(*generate_attributes(46))
44
-
45
- has_many :children, serializer: ChildrenSerializer
46
-
47
- def attr_1
48
- end
49
-
50
- def attr_2
51
- end
52
-
53
- def attr_3
54
- end
55
-
56
- def attr_4
57
- end
58
- end
59
-
60
- attrs = generate_attributes(21)
61
- attrs << :children
62
- filters = {
63
- instance: attrs,
64
- children: generate_attributes(11)
65
- }
66
- Benchmark.run("NoFilters") do
67
- Panko::SerializationDescriptor.build(ParentSerializer)
68
- end
69
-
70
- Benchmark.run("Attribute") do
71
- Panko::SerializationDescriptor.build(ParentSerializer, only: [:children])
72
- end
73
-
74
- Benchmark.run("AssociationFilters") do
75
- Panko::SerializationDescriptor.build(ParentSerializer, only: filters)
76
- end
@@ -1,22 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "benchmarking_support"
4
- require_relative "app"
5
-
6
- class NotSerializer
7
- end
8
-
9
- class RealSerializer < Panko::Serializer
10
- end
11
-
12
- Benchmark.run("CantFindConst") do
13
- Panko::SerializerResolver.resolve("cant_find_const", Object)
14
- end
15
-
16
- Benchmark.run("NotSerializer") do
17
- Panko::SerializerResolver.resolve("not", Object)
18
- end
19
-
20
- Benchmark.run("RealSerializer") do
21
- Panko::SerializerResolver.resolve("real", Object)
22
- end
@@ -1,81 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "benchmarking_support"
4
- require_relative "app"
5
- require_relative "setup"
6
-
7
- class PostWithAliasModel < ActiveRecord::Base
8
- self.table_name = "posts"
9
-
10
- alias_attribute :new_id, :id
11
- alias_attribute :new_body, :body
12
- alias_attribute :new_title, :title
13
- alias_attribute :new_author_id, :author_id
14
- alias_attribute :new_created_at, :created_at
15
- end
16
-
17
- class PostWithAliasFastSerializer < Panko::Serializer
18
- attributes :new_id, :new_body, :new_title, :new_author_id, :new_created_at
19
- end
20
-
21
- def benchmark_aliased(prefix, serializer, options = {})
22
- posts = PostWithAliasModel.all.to_a
23
- posts_50 = posts.first(50).to_a
24
-
25
- merged_options = options.merge(each_serializer: serializer)
26
-
27
- Benchmark.run("Panko_#{prefix}_PostWithAliasModels_#{posts.count}") do
28
- Panko::ArraySerializer.new(posts, merged_options).to_json
29
- end
30
-
31
- Benchmark.run("Panko_#{prefix}_Posts_50") do
32
- Panko::ArraySerializer.new(posts_50, merged_options).to_json
33
- end
34
- end
35
-
36
- class AuthorFastSerializer < Panko::Serializer
37
- attributes :id, :name
38
- end
39
-
40
- class PostFastSerializer < Panko::Serializer
41
- attributes :id, :body, :title, :author_id, :created_at
42
- end
43
-
44
- class PostFastWithMethodCallSerializer < Panko::Serializer
45
- attributes :id, :body, :title, :author_id, :created_at, :method_call
46
-
47
- def method_call
48
- object.id
49
- end
50
- end
51
-
52
- class PostWithHasOneFastSerializer < Panko::Serializer
53
- attributes :id, :body, :title, :author_id, :created_at
54
-
55
- has_one :author, serializer: AuthorFastSerializer
56
- end
57
-
58
- class AuthorWithHasManyFastSerializer < Panko::Serializer
59
- attributes :id, :name
60
-
61
- has_many :posts, serializer: PostFastSerializer
62
- end
63
-
64
- def benchmark(prefix, serializer, options = {})
65
- posts = Benchmark.data[:all]
66
-
67
- merged_options = options.merge(each_serializer: serializer)
68
-
69
- Benchmark.run("Panko_#{prefix}_Posts_#{posts.count}") do
70
- Panko::ArraySerializer.new(posts, merged_options).to_a
71
- end
72
-
73
- posts_50 = Benchmark.data[:small]
74
-
75
- Benchmark.run("Panko_#{prefix}_Posts_50") do
76
- Panko::ArraySerializer.new(posts_50, merged_options).to_a
77
- end
78
- end
79
-
80
- benchmark "Simple", PostFastSerializer
81
- benchmark "HasOne", PostWithHasOneFastSerializer
@@ -1,86 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require_relative "benchmarking_support"
4
- require_relative "app"
5
- require_relative "setup"
6
-
7
- require "memory_profiler"
8
-
9
- class NullLogger < Logger
10
- def initialize(*args)
11
- end
12
-
13
- def add(*args, &block)
14
- end
15
- end
16
-
17
- class BenchmarkApp < Rails::Application
18
- routes.append do
19
- get "/simple" => "main#simple"
20
- get "/text" => "main#text"
21
-
22
- get "/serialize_to_string" => "main#serialize_to_string"
23
- get "/serialize_to_stream" => "streaming#serialize_to_stream"
24
- end
25
-
26
- config.secret_token = "s" * 30
27
- config.secret_key_base = "foo"
28
- config.consider_all_requests_local = false
29
-
30
- # simulate production
31
- config.cache_classes = true
32
- config.eager_load = true
33
- config.action_controller.perform_caching = true
34
-
35
- # otherwise deadlock occured
36
- config.middleware.delete "Rack::Lock"
37
-
38
- # to disable log files
39
- config.logger = NullLogger.new
40
- config.active_support.deprecation = :log
41
- end
42
-
43
- BenchmarkApp.initialize!
44
-
45
- class AuthorFastSerializer < Panko::Serializer
46
- attributes :id, :name
47
- end
48
-
49
- class PostWithHasOneFastSerializer < Panko::Serializer
50
- attributes :id, :body, :title, :author_id
51
-
52
- has_one :author, serializer: AuthorFastSerializer
53
- end
54
-
55
- class StreamingController < ActionController::Base
56
- include ActionController::Live
57
-
58
- def serialize_to_stream
59
- headers["Content-Type"] = "application/json"
60
-
61
- data = Benchmark.data[:all]
62
- serializer = Panko::ArraySerializer.new([], each_serializer: PostWithHasOneFastSerializer)
63
- writer = Oj::StreamWriter.new(response.stream, mode: :rails)
64
-
65
- serializer.serialize_to_writer(data, writer)
66
-
67
- response.stream.close
68
- end
69
- end
70
-
71
- class RouteNotFoundError < StandardError; end
72
-
73
- def request(method, path)
74
- response = Rack::MockRequest.new(BenchmarkApp).send(method, path)
75
- if response.status.in?([404, 500])
76
- raise RouteNotFoundError.new, "not found #{method.to_s.upcase} #{path}"
77
- end
78
- response
79
- end
80
-
81
- def memory(&)
82
- mem = MemoryProfiler.report(&)
83
- mem.pretty_print
84
- end
85
-
86
- memory { request(:get, "/serialize_to_stream") }