panko_serializer 0.7.7 → 0.8.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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/lint.yml +40 -0
  3. data/.github/workflows/ruby.yml +35 -39
  4. data/.standard.yml +4 -0
  5. data/Gemfile +4 -3
  6. data/README.md +1 -1
  7. data/Rakefile +32 -33
  8. data/benchmarks/allocs.rb +1 -1
  9. data/benchmarks/app.rb +1 -1
  10. data/benchmarks/benchmarking_support.rb +3 -3
  11. data/benchmarks/bm_ams_0_10.rb +2 -5
  12. data/benchmarks/bm_object_writer.rb +1 -0
  13. data/benchmarks/bm_panko_json.rb +1 -1
  14. data/benchmarks/bm_panko_object.rb +1 -2
  15. data/benchmarks/bm_plain_object.rb +7 -7
  16. data/benchmarks/bm_serialization_descriptor.rb +5 -5
  17. data/benchmarks/bm_serializer_resolver.rb +4 -3
  18. data/benchmarks/bm_to_object.rb +2 -10
  19. data/benchmarks/profile.rb +5 -7
  20. data/benchmarks/sanity.rb +2 -10
  21. data/benchmarks/setup.rb +2 -2
  22. data/benchmarks/type_casts/bm_active_record.rb +6 -8
  23. data/benchmarks/type_casts/bm_panko.rb +11 -11
  24. data/benchmarks/type_casts/support.rb +8 -3
  25. data/docs/docs/introduction.md +1 -1
  26. data/docs/docs/performance.md +6 -6
  27. data/docs/package-lock.json +399 -330
  28. data/ext/panko_serializer/attributes_writer/active_record.c +6 -6
  29. data/ext/panko_serializer/attributes_writer/active_record.h +1 -2
  30. data/ext/panko_serializer/attributes_writer/attributes_writer.c +4 -6
  31. data/ext/panko_serializer/attributes_writer/attributes_writer.h +7 -2
  32. data/ext/panko_serializer/attributes_writer/common.h +2 -1
  33. data/ext/panko_serializer/attributes_writer/hash.c +3 -2
  34. data/ext/panko_serializer/attributes_writer/hash.h +2 -4
  35. data/ext/panko_serializer/attributes_writer/plain.c +2 -1
  36. data/ext/panko_serializer/attributes_writer/plain.h +2 -4
  37. data/ext/panko_serializer/attributes_writer/type_cast/type_cast.c +10 -10
  38. data/ext/panko_serializer/attributes_writer/type_cast/type_cast.h +4 -4
  39. data/ext/panko_serializer/common.h +0 -2
  40. data/ext/panko_serializer/panko_serializer.c +8 -9
  41. data/ext/panko_serializer/serialization_descriptor/association.c +3 -1
  42. data/ext/panko_serializer/serialization_descriptor/association.h +1 -1
  43. data/ext/panko_serializer/serialization_descriptor/attribute.c +3 -1
  44. data/ext/panko_serializer/serialization_descriptor/attribute.h +2 -2
  45. data/ext/panko_serializer/serialization_descriptor/serialization_descriptor.h +1 -1
  46. data/lib/panko/attribute.rb +5 -5
  47. data/lib/panko/object_writer.rb +5 -1
  48. data/lib/panko/response.rb +3 -3
  49. data/lib/panko/serialization_descriptor.rb +2 -2
  50. data/lib/panko/serializer.rb +3 -3
  51. data/lib/panko/serializer_resolver.rb +6 -4
  52. data/lib/panko/version.rb +1 -1
  53. data/panko_serializer.gemspec +8 -8
  54. metadata +5 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a55772372c4ae9e4d147a131c5dd59090b7dc928adc5e6830403246918fdb90b
4
- data.tar.gz: e6ee39b058b87b81d19385d4ae03fb938e2c04859641499454ca7debe99aceb9
3
+ metadata.gz: 00eff973f0a008e93332c9a83da475001203d49479126e90311577aa8a3f4fd5
4
+ data.tar.gz: e94e6290b5103ddb0481110cf901d69da86b6831cc5ed59f95b94abf810dac29
5
5
  SHA512:
6
- metadata.gz: eac9a9cad9ca9b99146527c25a935347b51a110ab36b0bc58165c961780415068c9899ebbe15ec3eb594a112d18665c54b5d5afd77ebd2449b078faa85e37981
7
- data.tar.gz: 87081d7cc24b5a69b3c409232abedb79daab75c263aea45b405d24ef71570df0c10f8821a741f88bb4e690d38b802bd6f43a82c0cee4a5e24d0ebba04849318a
6
+ metadata.gz: b87365560a2953a4f5b4027672b6f1721f89cc199345b82ed4ebb421263bfcb91f621651184e0d0c0f88c77f82d45acef61a061a0a863c21e7c2b066f57691be
7
+ data.tar.gz: 58c278c92461ef9f6f10ebee1579ad8bd801f6b5395625c982fc8ca1a32ab8e5e59d91527b820ddb9f662eea45df85952f7acf6060a9c9ac216208fac2db4523
@@ -0,0 +1,40 @@
1
+ name: Lint
2
+
3
+ on: [push, pull_request]
4
+
5
+ jobs:
6
+ build:
7
+ runs-on: ubuntu-latest
8
+
9
+ steps:
10
+ - uses: actions/checkout@v2
11
+ - name: Set up Ruby
12
+ uses: ruby/setup-ruby@v1
13
+ with:
14
+ ruby-version: 3.1.0
15
+
16
+ - name: Install deps
17
+ run: |
18
+ sudo apt update -y
19
+ sudo apt install -y libsqlite3-dev
20
+
21
+ - name: Install gems
22
+ env:
23
+ RAILS_VERSION: "7.0"
24
+ run: |
25
+ gem install bundler
26
+ bundle config set path 'vendor/bundle'
27
+ bundle check || bundle install --jobs 4 --retry 3
28
+
29
+ - name: Lint ruby
30
+ env:
31
+ RAILS_VERSION: "7.0"
32
+ run: |
33
+ bundle exec rake standard
34
+
35
+ - name: Lint C
36
+ uses: jidicula/clang-format-action@v4.11.0
37
+ with:
38
+ clang-format-version: "16"
39
+ check-path: "ext/panko_serializer"
40
+ fallback-style: "Google"
@@ -4,48 +4,44 @@ on: [push, pull_request]
4
4
 
5
5
  jobs:
6
6
  build:
7
-
8
7
  runs-on: ubuntu-latest
9
8
  strategy:
10
9
  fail-fast: false
11
10
  matrix:
12
- ruby: ['2.6', '2.7', '3.0', '3.1.0']
13
- rails: ['6.0.0', '6.1.0', '7.0.0']
14
- exclude:
15
- - ruby: '2.6'
16
- rails: '7.0.0'
11
+ ruby: ["3.0", "3.1", "3.2"]
12
+ rails: ["6.1.0", "7.0.0"]
17
13
 
18
14
  steps:
19
- - uses: actions/checkout@v2
20
- - name: Set up Ruby ${{ matrix.ruby }}
21
- uses: ruby/setup-ruby@v1
22
- with:
23
- ruby-version: ${{ matrix.ruby }}
24
-
25
- - name: Install deps
26
- run: |
27
- sudo apt update -y
28
- sudo apt install -y libsqlite3-dev
29
-
30
- - name: Gems Cache
31
- id: gem-cache
32
- uses: actions/cache@v1
33
- with:
34
- path: vendor/bundle
35
- key: ${{ runner.os }}-${{ matrix.ruby }}-${{ matrix.rails }}-gem
36
- restore-keys: |
37
- ${{ runner.os }}-${{ matrix.ruby }}-${{ matrix.rails }}-gem
38
-
39
- - name: Install gems
40
- env:
41
- RAILS_VERSION: ${{ matrix.rails }}
42
- run: |
43
- gem install bundler
44
- bundle config set path 'vendor/bundle'
45
- bundle check || bundle install --jobs 4 --retry 3
46
-
47
- - name: Compile & test
48
- env:
49
- RAILS_VERSION: ${{ matrix.rails }}
50
- run: |
51
- bundle exec rake
15
+ - uses: actions/checkout@v2
16
+ - name: Set up Ruby ${{ matrix.ruby }}
17
+ uses: ruby/setup-ruby@v1
18
+ with:
19
+ ruby-version: ${{ matrix.ruby }}
20
+
21
+ - name: Install deps
22
+ run: |
23
+ sudo apt update -y
24
+ sudo apt install -y libsqlite3-dev
25
+
26
+ - name: Gems Cache
27
+ id: gem-cache
28
+ uses: actions/cache@v1
29
+ with:
30
+ path: vendor/bundle
31
+ key: ${{ runner.os }}-${{ matrix.ruby }}-${{ matrix.rails }}-gem
32
+ restore-keys: |
33
+ ${{ runner.os }}-${{ matrix.ruby }}-${{ matrix.rails }}-gem
34
+
35
+ - name: Install gems
36
+ env:
37
+ RAILS_VERSION: ${{ matrix.rails }}
38
+ run: |
39
+ gem install bundler
40
+ bundle config set path 'vendor/bundle'
41
+ bundle check || bundle install --jobs 4 --retry 3
42
+
43
+ - name: Compile & test
44
+ env:
45
+ RAILS_VERSION: ${{ matrix.rails }}
46
+ run: |
47
+ bundle exec rake
data/.standard.yml ADDED
@@ -0,0 +1,4 @@
1
+ ignore:
2
+ - "ext/**/*"
3
+ - "spec/**/*":
4
+ - Lint/ConstantDefinitionInBlock
data/Gemfile CHANGED
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  source "https://rubygems.org"
3
4
 
4
5
  gemspec
@@ -10,7 +11,6 @@ gem "activesupport", rails_version
10
11
  gem "activemodel", rails_version
11
12
  gem "activerecord", rails_version, group: :test
12
13
 
13
-
14
14
  group :benchmarks do
15
15
  gem "sqlite3", "~> 1.4"
16
16
  gem "pg", ">= 0.18", "< 2.0"
@@ -20,7 +20,7 @@ group :benchmarks do
20
20
  gem "ruby-prof-flamegraph", platforms: [:mri]
21
21
 
22
22
  gem "benchmark-ips"
23
- gem "active_model_serializers"
23
+ gem "active_model_serializers", "~> 0.10"
24
24
  gem "terminal-table"
25
25
  end
26
26
 
@@ -33,5 +33,6 @@ group :development do
33
33
  gem "rake"
34
34
  gem "rspec", "~> 3.0"
35
35
  gem "rake-compiler"
36
- gem "rubocop"
37
36
  end
37
+
38
+ gem "standard", group: [:development, :test]
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Panko
2
2
 
3
- ![Build Status](https://github.com/panko-serializer/panko_serializer/workflows/Panko%20Serializer%20CI/badge.svg?branch=github-actions)
3
+ ![Build Status](https://github.com/panko-serializer/panko_serializer/workflows/Panko%20Serializer%20CI/badge.svg?branch=master)
4
4
 
5
5
  Panko is library which is inspired by ActiveModelSerializers 0.9 for serializing ActiveRecord/Ruby objects to JSON strings, fast.
6
6
 
data/Rakefile CHANGED
@@ -1,13 +1,14 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "bundler/gem_tasks"
3
4
  require "rspec/core/rake_task"
4
5
  require "json"
5
6
  require "terminal-table"
6
7
  require "rake/extensiontask"
7
8
  require "pty"
9
+ require "standard/rake"
8
10
 
9
- gem = Gem::Specification.load( File.dirname(__FILE__) + "/panko_serializer.gemspec" )
10
-
11
+ gem = Gem::Specification.load(File.dirname(__FILE__) + "/panko_serializer.gemspec")
11
12
 
12
13
  Rake::ExtensionTask.new("panko_serializer", gem) do |ext|
13
14
  ext.lib_dir = "lib/panko"
@@ -31,15 +32,14 @@ end
31
32
  def run_process(cmd)
32
33
  puts "> Running #{cmd}"
33
34
  lines = []
34
- PTY.spawn(cmd) do |stdout, stdin, pid|
35
- begin
36
- stdout.each do |line|
37
- print_and_flush '.'
38
- lines << line
39
- end
40
- rescue Errno::EIO
41
- # ignore this
35
+ _stderr_reader, stderr_writer = IO.pipe
36
+ PTY.spawn(cmd, err: stderr_writer.fileno) do |stdout, stdin, pid|
37
+ stdout.each do |line|
38
+ print_and_flush "."
39
+ lines << line
42
40
  end
41
+ rescue Errno::EIO
42
+ # ignore this
43
43
  end
44
44
 
45
45
  lines
@@ -51,15 +51,12 @@ end
51
51
  def run_benchmarks(files, items_count: 2_300)
52
52
  headings = ["Benchmark", "ip/s", "allocs/retained"]
53
53
  files.each do |benchmark_file|
54
-
55
54
  lines = run_process "ITEMS_COUNT=#{items_count} RAILS_ENV=production ruby #{benchmark_file}"
56
55
  rows = lines.map do |line|
57
- begin
58
- row = JSON.parse(line)
59
- row.values
60
- rescue JSON::ParserError
61
- puts "> [ERROR] Failed running #{benchmark_file} - #{lines.join}"
62
- end
56
+ row = JSON.parse(line)
57
+ row.values
58
+ rescue JSON::ParserError
59
+ puts "> [ERROR] Failed running #{benchmark_file} - #{lines.join}"
63
60
  end
64
61
 
65
62
  puts "\n\n"
@@ -69,24 +66,26 @@ def run_benchmarks(files, items_count: 2_300)
69
66
  end
70
67
  end
71
68
 
72
- desc "Run all benchmarks"
73
- task :benchmarks do
74
- run_benchmarks Dir[File.join(__dir__, "benchmarks", "**", "bm_*")]
75
- end
69
+ namespace :benchmarks do
70
+ desc "All"
71
+ task :all do
72
+ run_benchmarks Dir[File.join(__dir__, "benchmarks", "**", "bm_*")]
73
+ end
76
74
 
77
- desc "Type Casts - Benchmarks"
78
- task :bm_type_casts do
79
- run_benchmarks Dir[File.join(__dir__, "benchmarks", "type_casts", "bm_*")], items_count: 0
80
- end
75
+ desc "Type Casts"
76
+ task :type_casts do
77
+ run_benchmarks Dir[File.join(__dir__, "benchmarks", "type_casts", "bm_*")], items_count: 0
78
+ end
81
79
 
82
- desc "Sanity Benchmarks"
83
- task :sanity do
84
- puts Time.now.strftime("%d/%m %H:%M:%S")
85
- puts "=========================="
80
+ desc "Sanity"
81
+ task :sanity do
82
+ puts Time.now.strftime("%d/%m %H:%M:%S")
83
+ puts "=========================="
86
84
 
87
- run_benchmarks [
88
- File.join(__dir__, "benchmarks", "sanity.rb")
89
- ], items_count: 2300
85
+ run_benchmarks [
86
+ File.join(__dir__, "benchmarks", "sanity.rb")
87
+ ], items_count: 2300
90
88
 
91
- puts "\n\n"
89
+ puts "\n\n"
90
+ end
92
91
  end
data/benchmarks/allocs.rb CHANGED
@@ -1,5 +1,5 @@
1
-
2
1
  # frozen_string_literal: true
2
+
3
3
  require_relative "./benchmarking_support"
4
4
  require_relative "./app"
5
5
  require_relative "./setup"
data/benchmarks/app.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
- require "bundler/setup"
3
2
 
3
+ require "bundler/setup"
4
4
 
5
5
  require "active_model"
6
6
  require "active_record"
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require "benchmark/ips"
3
4
  require "json"
4
5
  require "memory_profiler"
@@ -8,11 +9,11 @@ module Benchmark
8
9
  def data
9
10
  posts = Post.all.includes(:author).to_a
10
11
  posts_50 = posts.first(50).to_a
11
- { all: posts, small: posts_50 }
12
+ {all: posts, small: posts_50}
12
13
  end
13
14
 
14
15
  def run(label = nil, time: 10, disable_gc: true, warmup: 3, &block)
15
- fail ArgumentError.new, "block should be passed" unless block_given?
16
+ fail ArgumentError.new, "block should be passed" unless block
16
17
 
17
18
  GC.start
18
19
 
@@ -35,7 +36,6 @@ module Benchmark
35
36
  }.to_json
36
37
 
37
38
  puts results
38
-
39
39
  end
40
40
  end
41
41
 
@@ -1,11 +1,11 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "./benchmarking_support"
3
4
  require_relative "./app"
4
5
  require_relative "./setup"
5
6
 
6
7
  # disable logging for benchmarks
7
- ActiveModelSerializers.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new('/dev/null'))
8
-
8
+ ActiveModelSerializers.logger = ActiveSupport::TaggedLogging.new(ActiveSupport::Logger.new("/dev/null"))
9
9
 
10
10
  class AmsAuthorFastSerializer < ActiveModel::Serializer
11
11
  attributes :id, :name
@@ -28,7 +28,6 @@ def benchmark_ams(prefix, serializer, options = {})
28
28
  posts = data[:all]
29
29
  posts_50 = data[:small]
30
30
 
31
-
32
31
  Benchmark.run("AMS_#{prefix}_Posts_#{posts.count}") do
33
32
  ActiveModelSerializers::SerializableResource.new(posts, merged_options).to_json
34
33
  end
@@ -42,9 +41,7 @@ def benchmark_ams(prefix, serializer, options = {})
42
41
  end
43
42
  end
44
43
 
45
-
46
44
  benchmark_ams "Attributes_Simple", AmsPostFastSerializer
47
45
  benchmark_ams "Attributes_HasOne", AmsPostWithHasOneFastSerializer
48
46
  benchmark_ams "Attributes_Except", AmsPostWithHasOneFastSerializer, except: [:title]
49
47
  benchmark_ams "Attributes_Only", AmsPostWithHasOneFastSerializer, only: [:id, :body, :author_id, :author]
50
-
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "./benchmarking_support"
3
4
  require_relative "./app"
4
5
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "./benchmarking_support"
3
4
  require_relative "./app"
4
5
  require_relative "./setup"
@@ -7,7 +8,6 @@ class AuthorFastSerializer < Panko::Serializer
7
8
  attributes :id, :name
8
9
  end
9
10
 
10
-
11
11
  class PostFastSerializer < Panko::Serializer
12
12
  attributes :id, :body, :title, :author_id, :created_at
13
13
  end
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "./benchmarking_support"
3
4
  require_relative "./app"
4
5
  require_relative "./setup"
@@ -7,7 +8,6 @@ class AuthorFastSerializer < Panko::Serializer
7
8
  attributes :id, :name
8
9
  end
9
10
 
10
-
11
11
  class PostFastSerializer < Panko::Serializer
12
12
  attributes :id, :body, :title, :author_id
13
13
  end
@@ -24,7 +24,6 @@ class AuthorWithHasManyFastSerializer < Panko::Serializer
24
24
  has_many :posts, serializer: PostFastSerializer
25
25
  end
26
26
 
27
-
28
27
  def benchmark(prefix, serializer, options = {})
29
28
  data = Benchmark.data
30
29
  posts = data[:all]
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "./benchmarking_support"
3
4
  require_relative "./app"
4
5
 
@@ -6,7 +7,6 @@ class AuthorFastSerializer < Panko::Serializer
6
7
  attributes :id, :name
7
8
  end
8
9
 
9
-
10
10
  class PostFastSerializer < Panko::Serializer
11
11
  attributes :id, :body, :title, :author_id, :created_at
12
12
  end
@@ -32,11 +32,12 @@ class AuthorWithHasManyFastSerializer < Panko::Serializer
32
32
  end
33
33
 
34
34
  class Post
35
- attr_accessor :id, :body, :title, :created_at, :author, :author_id
35
+ attr_accessor :id, :body, :title, :created_at, :author_id
36
+ attr_reader :author
36
37
 
37
38
  def self.create(attrs)
38
39
  p = Post.new
39
- attrs.each do |k,v|
40
+ attrs.each do |k, v|
40
41
  p.send :"#{k}=", v
41
42
  end
42
43
  p
@@ -44,7 +45,7 @@ class Post
44
45
 
45
46
  def author=(author)
46
47
  @author = author
47
- @author_id = author_id
48
+ @author_id = author.id
48
49
  end
49
50
  end
50
51
 
@@ -53,7 +54,7 @@ class Author
53
54
 
54
55
  def self.create(attrs)
55
56
  a = Author.new
56
- attrs.each do |k,v|
57
+ attrs.each do |k, v|
57
58
  a.send :"#{k}=", v
58
59
  end
59
60
  a
@@ -71,10 +72,9 @@ def benchmark_data
71
72
  )
72
73
  end
73
74
 
74
- { all: posts, small: posts.first(50) }
75
+ {all: posts, small: posts.first(50)}
75
76
  end
76
77
 
77
-
78
78
  def benchmark(prefix, serializer, options = {})
79
79
  data = benchmark_data
80
80
  posts = data[:all]
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "./benchmarking_support"
3
4
  require "active_support"
4
5
 
@@ -9,15 +10,15 @@ def generate_attributes(count)
9
10
  end
10
11
 
11
12
  class LeafASerializer < Panko::Serializer
12
- attributes *generate_attributes(5)
13
+ attributes(*generate_attributes(5))
13
14
  end
14
15
 
15
16
  class LeafBSerializer < Panko::Serializer
16
- attributes *generate_attributes(6)
17
+ attributes(*generate_attributes(6))
17
18
  end
18
19
 
19
20
  class ChildrenSerializer < Panko::Serializer
20
- attributes *generate_attributes(28)
21
+ attributes(*generate_attributes(28))
21
22
 
22
23
  has_one :leaf_a, serializer: LeafASerializer
23
24
  has_one :leaf_b, serializer: LeafBSerializer
@@ -39,11 +40,10 @@ class ChildrenSerializer < Panko::Serializer
39
40
  end
40
41
 
41
42
  class ParentSerializer < Panko::Serializer
42
- attributes *generate_attributes(46)
43
+ attributes(*generate_attributes(46))
43
44
 
44
45
  has_many :children, serializer: ChildrenSerializer
45
46
 
46
-
47
47
  def attr_1
48
48
  end
49
49
 
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "./benchmarking_support"
3
4
  require_relative "./app"
4
5
 
@@ -9,13 +10,13 @@ class RealSerializer < Panko::Serializer
9
10
  end
10
11
 
11
12
  Benchmark.run("CantFindConst") do
12
- Panko::SerializerResolver.resolve("cant_find_const")
13
+ Panko::SerializerResolver.resolve("cant_find_const", Object)
13
14
  end
14
15
 
15
16
  Benchmark.run("NotSerializer") do
16
- Panko::SerializerResolver.resolve("not")
17
+ Panko::SerializerResolver.resolve("not", Object)
17
18
  end
18
19
 
19
20
  Benchmark.run("RealSerializer") do
20
- Panko::SerializerResolver.resolve("real")
21
+ Panko::SerializerResolver.resolve("real", Object)
21
22
  end
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "./benchmarking_support"
3
4
  require_relative "./app"
4
5
  require_relative "./setup"
5
6
 
6
7
  class PostWithAliasModel < ActiveRecord::Base
7
- self.table_name = 'posts'
8
+ self.table_name = "posts"
8
9
 
9
10
  alias_attribute :new_id, :id
10
11
  alias_attribute :new_body, :body
@@ -20,7 +21,6 @@ end
20
21
  def benchmark_aliased(prefix, serializer, options = {})
21
22
  posts = PostWithAliasModel.all.to_a
22
23
  posts_50 = posts.first(50).to_a
23
- data = { all: posts, small: posts_50 }
24
24
 
25
25
  merged_options = options.merge(each_serializer: serializer)
26
26
 
@@ -28,10 +28,6 @@ def benchmark_aliased(prefix, serializer, options = {})
28
28
  Panko::ArraySerializer.new(posts, merged_options).to_json
29
29
  end
30
30
 
31
- posts = PostWithAliasModel.all.to_a
32
- posts_50 = posts.first(50).to_a
33
- data = { all: posts, small: posts_50 }
34
-
35
31
  Benchmark.run("Panko_#{prefix}_Posts_50") do
36
32
  Panko::ArraySerializer.new(posts_50, merged_options).to_json
37
33
  end
@@ -41,7 +37,6 @@ class AuthorFastSerializer < Panko::Serializer
41
37
  attributes :id, :name
42
38
  end
43
39
 
44
-
45
40
  class PostFastSerializer < Panko::Serializer
46
41
  attributes :id, :body, :title, :author_id, :created_at
47
42
  end
@@ -66,7 +61,6 @@ class AuthorWithHasManyFastSerializer < Panko::Serializer
66
61
  has_many :posts, serializer: PostFastSerializer
67
62
  end
68
63
 
69
-
70
64
  def benchmark(prefix, serializer, options = {})
71
65
  data = Benchmark.data
72
66
  posts = data[:all]
@@ -87,7 +81,5 @@ def benchmark(prefix, serializer, options = {})
87
81
  end
88
82
  end
89
83
 
90
-
91
-
92
84
  benchmark "Simple", PostFastSerializer
93
85
  benchmark "HasOne", PostWithHasOneFastSerializer
@@ -1,4 +1,5 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "./benchmarking_support"
3
4
  require_relative "./app"
4
5
  require_relative "./setup"
@@ -22,7 +23,7 @@ class BenchmarkApp < Rails::Application
22
23
  get "/serialize_to_stream" => "streaming#serialize_to_stream"
23
24
  end
24
25
 
25
- config.secret_token = "s"*30
26
+ config.secret_token = "s" * 30
26
27
  config.secret_key_base = "foo"
27
28
  config.consider_all_requests_local = false
28
29
 
@@ -55,7 +56,7 @@ class StreamingController < ActionController::Base
55
56
  include ActionController::Live
56
57
 
57
58
  def serialize_to_stream
58
- headers["Content-Type".freeze] = "application/json".freeze
59
+ headers["Content-Type"] = "application/json"
59
60
 
60
61
  data = Benchmark.data[:all]
61
62
  serializer = Panko::ArraySerializer.new([], each_serializer: PostWithHasOneFastSerializer)
@@ -67,19 +68,16 @@ class StreamingController < ActionController::Base
67
68
  end
68
69
  end
69
70
 
70
-
71
- class RouteNotFoundError < StandardError;end
72
-
71
+ class RouteNotFoundError < StandardError; end
73
72
 
74
73
  def request(method, path)
75
74
  response = Rack::MockRequest.new(BenchmarkApp).send(method, path)
76
75
  if response.status.in?([404, 500])
77
- raise RouteNotFoundError.new, 'not found #{method.to_s.upcase} #{path}'
76
+ raise RouteNotFoundError.new, "not found #{method.to_s.upcase} #{path}"
78
77
  end
79
78
  response
80
79
  end
81
80
 
82
-
83
81
  def memory(&block)
84
82
  mem = MemoryProfiler.report(&block)
85
83
  mem.pretty_print
data/benchmarks/sanity.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  # frozen_string_literal: true
2
+
2
3
  require_relative "./benchmarking_support"
3
4
  require_relative "./app"
4
5
  require_relative "./setup"
5
6
 
6
7
  class PostWithAliasModel < ActiveRecord::Base
7
- self.table_name = 'posts'
8
+ self.table_name = "posts"
8
9
 
9
10
  alias_attribute :new_id, :id
10
11
  alias_attribute :new_body, :body
@@ -20,7 +21,6 @@ end
20
21
  def benchmark_aliased(prefix, serializer, options = {})
21
22
  posts = PostWithAliasModel.all.to_a
22
23
  posts_50 = posts.first(50).to_a
23
- data = { all: posts, small: posts_50 }
24
24
 
25
25
  merged_options = options.merge(each_serializer: serializer)
26
26
 
@@ -28,10 +28,6 @@ def benchmark_aliased(prefix, serializer, options = {})
28
28
  Panko::ArraySerializer.new(posts, merged_options).to_json
29
29
  end
30
30
 
31
- posts = PostWithAliasModel.all.to_a
32
- posts_50 = posts.first(50).to_a
33
- data = { all: posts, small: posts_50 }
34
-
35
31
  Benchmark.run("Panko_#{prefix}_Posts_50") do
36
32
  Panko::ArraySerializer.new(posts_50, merged_options).to_json
37
33
  end
@@ -41,7 +37,6 @@ class AuthorFastSerializer < Panko::Serializer
41
37
  attributes :id, :name
42
38
  end
43
39
 
44
-
45
40
  class PostFastSerializer < Panko::Serializer
46
41
  attributes :id, :body, :title, :author_id, :created_at
47
42
  end
@@ -66,7 +61,6 @@ class AuthorWithHasManyFastSerializer < Panko::Serializer
66
61
  has_many :posts, serializer: PostFastSerializer
67
62
  end
68
63
 
69
-
70
64
  def benchmark(prefix, serializer, options = {})
71
65
  data = Benchmark.data
72
66
  posts = data[:all]
@@ -87,8 +81,6 @@ def benchmark(prefix, serializer, options = {})
87
81
  end
88
82
  end
89
83
 
90
-
91
-
92
84
  benchmark "Simple", PostFastSerializer
93
85
  benchmark "HasOne", PostWithHasOneFastSerializer
94
86
  benchmark "SimpleWithMethodCall", PostFastWithMethodCallSerializer