This repository has been archived by the owner on Apr 17, 2018. It is now read-only.
/
to_json.rb
137 lines (112 loc) · 3.2 KB
/
to_json.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require "rubygems"
require 'pathname'
require 'better-benchmark'
gem 'dm-core', '1.0.0'
require 'dm-core'
spec_dir_path = Pathname(__FILE__).dirname.expand_path
$LOAD_PATH.unshift(spec_dir_path.parent + 'lib/')
require 'dm-serializer'
def load_driver(name, default_uri)
begin
DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
DataMapper::Repository.adapters[:alternate] = DataMapper::Repository.adapters[name]
true
rescue LoadError => e
warn "Could not load do_#{name}: #{e}"
false
end
end
HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
module DataMapper
module Serialize
# Serialize a Resource to JavaScript Object Notation (JSON; RFC 4627)
#
# @return <String> a JSON representation of the Resource
def to_json_old(*args)
options = args.first || {}
result = '{ '
fields = []
propset = properties_to_serialize(options)
fields += propset.map do |property|
"#{property.name.to_json}: #{send(property.name).to_json}"
end
# add methods
(options[:methods] || []).each do |meth|
if self.respond_to?(meth)
fields << "#{meth.to_json}: #{send(meth).to_json}"
end
end
# Note: if you want to include a whole other model via relation, use :methods
# comments.to_json(:relationships=>{:user=>{:include=>[:first_name],:methods=>[:age]}})
# add relationships
# TODO: This needs tests and also needs to be ported to #to_xml and #to_yaml
(options[:relationships] || {}).each do |rel,opts|
if self.respond_to?(rel)
fields << "#{rel.to_json}: #{send(rel).to_json(opts)}"
end
end
result << fields.join(', ')
result << ' }'
result
end
end
class Collection
def to_json_old(*args)
opts = args.first || {}
'[' << map { |e| e.to_json_old(opts) }.join(',') << ']'
end
end
end
class Cow
include DataMapper::Resource
property :id, Integer, :key => true
property :composite, Integer, :key => true
property :name, String
property :breed, String
has n, :baby_cows, :model => 'Cow'
belongs_to :mother_cow, :model => 'Cow', :required => false
end
DataMapper.auto_migrate!
cow = Cow.create(
:id => 89,
:composite => 34,
:name => 'Berta',
:breed => 'Guernsey'
)
10.times do |n|
Cow.create(
:id => n*10,
:composite => n,
:name => "Bertha"*n,
:breed => "Mooing#{n}"
)
end
all_cows = Cow.all.reload
puts "Benchmarking single resource serialization."
puts "Set 1: old method"
puts "Set 2: new method"
result = Benchmark.compare_realtime(
:iterations => 10,
:inner_iterations => 20000,
:verbose => true
) { |iteration|
cow.to_json_old
}.with { |iteration|
cow.to_json
}
Benchmark.report_on result
puts
puts "Benchmarking collection serialization."
puts "Set 1: old method"
puts "Set 2: new method"
result = Benchmark.compare_realtime(
:iterations => 10,
:inner_iterations => 5000,
:verbose => true
) { |iteration|
all_cows.to_json_old
}.with { |iteration|
all_cows.to_json
}
Benchmark.report_on result