|
| 1 | +$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib')) |
| 2 | +require 'grape-entity' |
| 3 | +require 'benchmark' |
| 4 | + |
| 5 | +module Models |
| 6 | + class School |
| 7 | + attr_reader :classrooms |
| 8 | + def initialize |
| 9 | + @classrooms = [] |
| 10 | + end |
| 11 | + end |
| 12 | + |
| 13 | + class ClassRoom |
| 14 | + attr_reader :students |
| 15 | + attr_accessor :teacher |
| 16 | + def initialize(opts = {}) |
| 17 | + @teacher = opts[:teacher] |
| 18 | + @students = [] |
| 19 | + end |
| 20 | + end |
| 21 | + |
| 22 | + class Person |
| 23 | + attr_accessor :name |
| 24 | + def initialize(opts = {}) |
| 25 | + @name = opts[:name] |
| 26 | + end |
| 27 | + end |
| 28 | + |
| 29 | + class Teacher < Models::Person |
| 30 | + attr_accessor :tenure |
| 31 | + def initialize(opts = {}) |
| 32 | + super(opts) |
| 33 | + @tenure = opts[:tenure] |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + class Student < Models::Person |
| 38 | + attr_reader :grade |
| 39 | + def initialize(opts = {}) |
| 40 | + super(opts) |
| 41 | + @grade = opts[:grade] |
| 42 | + end |
| 43 | + end |
| 44 | +end |
| 45 | + |
| 46 | +module Entities |
| 47 | + class School < Grape::Entity |
| 48 | + expose :classrooms, using: 'Entities::ClassRoom' |
| 49 | + end |
| 50 | + |
| 51 | + class ClassRoom < Grape::Entity |
| 52 | + expose :teacher, using: 'Entities::Teacher' |
| 53 | + expose :students, using: 'Entities::Student' |
| 54 | + expose :size do |model, opts| |
| 55 | + model.students.count |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + class Person < Grape::Entity |
| 60 | + expose :name |
| 61 | + end |
| 62 | + |
| 63 | + class Student < Entities::Person |
| 64 | + expose :grade |
| 65 | + expose :failing do |model, opts| |
| 66 | + model.grade == 'F' |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + class Teacher < Entities::Person |
| 71 | + expose :tenure |
| 72 | + end |
| 73 | +end |
| 74 | + |
| 75 | + |
| 76 | +teacher1 = Models::Teacher.new(:name => "John Smith", :tenure => 2) |
| 77 | +classroom1 = Models::ClassRoom.new(:teacher => teacher1) |
| 78 | +classroom1.students << Models::Student.new(:name => "Bobby", :grade => 'A' ) |
| 79 | +classroom1.students << Models::Student.new(:name => "Billy", :grade => 'B' ) |
| 80 | + |
| 81 | +teacher2 = Models::Teacher.new(:name => "Lisa Barns") |
| 82 | +classroom2 = Models::ClassRoom.new(:teacher => teacher2, :tenure => 15) |
| 83 | +classroom2.students << Models::Student.new(:name => "Eric", :grade => 'A' ) |
| 84 | +classroom2.students << Models::Student.new(:name => "Eddie", :grade => 'C' ) |
| 85 | +classroom2.students << Models::Student.new(:name => "Arnie", :grade => 'C' ) |
| 86 | +classroom2.students << Models::Student.new(:name => "Alvin", :grade => 'F' ) |
| 87 | +school = Models::School.new |
| 88 | +school.classrooms << classroom1 |
| 89 | +school.classrooms << classroom2 |
| 90 | + |
| 91 | +iters = 5000 |
| 92 | + |
| 93 | +Benchmark.bm do |bm| |
| 94 | + bm.report("serializing") do |
| 95 | + iters.times do |
| 96 | + Entities::School.represent(school, :serializable => true) |
| 97 | + end |
| 98 | + end |
| 99 | +end |
0 commit comments