jsonify 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -18,7 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
19
  s.require_paths = ["lib"]
20
20
 
21
- s.add_dependency 'json'
21
+ s.add_dependency 'json_pure'
22
22
 
23
23
  s.add_development_dependency 'bundler'
24
24
  s.add_development_dependency 'rake'
@@ -26,4 +26,5 @@ Gem::Specification.new do |s|
26
26
  s.add_development_dependency 'autotest'
27
27
  s.add_development_dependency 'yard'
28
28
  s.add_development_dependency 'rdiscount'
29
+ s.add_development_dependency 'ruby-prof'
29
30
  end
@@ -3,4 +3,5 @@ require 'jsonify/blank_slate'
3
3
  require 'jsonify/version'
4
4
  require 'jsonify/json_value'
5
5
  require 'jsonify/generate'
6
- require 'jsonify/builder'
6
+ require 'jsonify/builder'
7
+ require 'benchmark'
@@ -44,66 +44,4 @@ module Jsonify
44
44
 
45
45
  instance_methods.each { |m| hide(m) }
46
46
  end
47
- end
48
- ######################################################################
49
- # Since Ruby is very dynamic, methods added to the ancestors of
50
- # BlankSlate <em>after BlankSlate is defined</em> will show up in the
51
- # list of available BlankSlate methods. We handle this by defining a
52
- # hook in the Object and Kernel classes that will hide any method
53
- # defined after BlankSlate has been loaded.
54
- #
55
- # module Kernel
56
- # class << self
57
- # alias_method :blank_slate_method_added, :method_added
58
- #
59
- # # Detect method additions to Kernel and remove them in the
60
- # # BlankSlate class.
61
- # def method_added(name)
62
- # result = blank_slate_method_added(name)
63
- # return result if self != Kernel
64
- # BlankSlate.hide(name)
65
- # result
66
- # end
67
- # end
68
- # end
69
-
70
- ######################################################################
71
- # Same as above, except in Object.
72
- #
73
- # class Object
74
- # class << self
75
- # alias_method :blank_slate_method_added, :method_added
76
- #
77
- # # Detect method additions to Object and remove them in the
78
- # # BlankSlate class.
79
- # def method_added(name)
80
- # result = blank_slate_method_added(name)
81
- # return result if self != Object
82
- # BlankSlate.hide(name)
83
- # result
84
- # end
85
- #
86
- # def find_hidden_method(name)
87
- # nil
88
- # end
89
- # end
90
- # end
91
-
92
- ######################################################################
93
- # Also, modules included into Object need to be scanned and have their
94
- # instance methods removed from blank slate. In theory, modules
95
- # included into Kernel would have to be removed as well, but a
96
- # "feature" of Ruby prevents late includes into modules from being
97
- # exposed in the first place.
98
- #
99
- # class Module
100
- # alias blankslate_original_append_features append_features
101
- # def append_features(mod)
102
- # result = blankslate_original_append_features(mod)
103
- # return result if mod != Object
104
- # instance_methods.each do |name|
105
- # BlankSlate.hide(name)
106
- # end
107
- # result
108
- # end
109
- # end
47
+ end
@@ -1,3 +1,3 @@
1
1
  module Jsonify
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -0,0 +1,133 @@
1
+ require "benchmark"
2
+ require "rspec"
3
+
4
+ module RSpec
5
+ module Benchmark
6
+ class Result
7
+ attr_accessor :slowest, :fastest, :average, :elapsed
8
+
9
+ def initialize(elapsed)
10
+ @elapsed = elapsed
11
+ @slowest = elapsed.max
12
+ @fastest = elapsed.min
13
+ @average = elapsed.inject(0) {|b, t| b + t} / elapsed.size
14
+ end
15
+
16
+ def to_s
17
+ "[average: #{average}, slowest: #{slowest}, fastest: #{fastest}]"
18
+ end
19
+ end
20
+
21
+ # Run a given block and calculate the average execution time.
22
+ # The block will be executed 1000 times by default.
23
+ #
24
+ # benchmark { do something }
25
+ # benchmark(100) { do something }
26
+ #
27
+ def benchmark(times = 1_000, &block)
28
+ elapsed = (1..times).collect do
29
+ GC.start
30
+ ::Benchmark.realtime(&block) * 1000
31
+ end
32
+
33
+ Result.new(elapsed)
34
+ end
35
+ end
36
+ end
37
+
38
+ # Check if the slowest execution is less than expected.
39
+ #
40
+ # it "should do something fast" do
41
+ # benchmark { do something }.should be_faster_than(1.3)
42
+ # end
43
+ #
44
+ RSpec::Matchers.define :be_faster_than do |expected|
45
+ match do |result|
46
+ result.slowest < expected
47
+ end
48
+ end
49
+
50
+ # Check if the slowest execution is greater than expected.
51
+ #
52
+ # it "should do something slow" do
53
+ # benchmark { do something }.should_not be_slower_than(1.3)
54
+ # end
55
+ #
56
+ RSpec::Matchers.define :be_slower_than do |expected|
57
+ match do |result|
58
+ result.slowest > expected
59
+ end
60
+ end
61
+
62
+ # Check if the execution average is close to expected.
63
+ #
64
+ # it "should do something average time" do
65
+ # benchmark { do something }.should be_on_average(1.3, 0.01)
66
+ # end
67
+ #
68
+ RSpec::Matchers.define :be_on_average do |expected, delta|
69
+ match do |result|
70
+ (result.average - expected).abs < delta
71
+ end
72
+ end
73
+
74
+ # Include matchers and <tt>benchmark</tt> method into RSpec context.
75
+ RSpec.configure do |config|
76
+ config.include(RSpec::Benchmark)
77
+ end
78
+
79
+ if ARGV[0] == __FILE__
80
+ describe RSpec::Benchmark do
81
+ before do
82
+ GC.stub(:start)
83
+ end
84
+
85
+ it "should be faster than expected" do
86
+ stub(:slowest => 0.01).should be_faster_than(0.5)
87
+ end
88
+
89
+ it "should not be faster than expected" do
90
+ stub(:slowest => 2).should_not be_faster_than(0.5)
91
+ end
92
+
93
+ it "should be slower than expected" do
94
+ stub(:slowest => 2).should be_slower_than(0.5)
95
+ end
96
+
97
+ it "should not be slower than expected" do
98
+ stub(:slowest => 0.5).should_not be_slower_than(2)
99
+ end
100
+
101
+ it "should be on average" do
102
+ stub(:average => 0.51).should be_on_average(0.5, 0.019)
103
+ stub(:average => 0.49).should be_on_average(0.5, 0.019)
104
+ end
105
+
106
+ it "should call garbage collector" do
107
+ GC.should_receive(:start).exactly(5).times
108
+ benchmark(5) { true }
109
+ end
110
+
111
+ it "should return result with collected data" do
112
+ result = benchmark(5) { true }
113
+
114
+ result.average.should be_kind_of(Float)
115
+ result.slowest.should be_kind_of(Float)
116
+ result.fastest.should be_kind_of(Float)
117
+ end
118
+
119
+ it "should run block" do
120
+ object = mock
121
+ object.should_receive(:run).exactly(1000).times
122
+
123
+ benchmark { object.run }
124
+ end
125
+
126
+ it "should run block with custom range" do
127
+ object = mock
128
+ object.should_receive(:run).exactly(3).times
129
+
130
+ benchmark(3) { object.run }
131
+ end
132
+ end
133
+ end
@@ -0,0 +1,46 @@
1
+ require 'spec_helper'
2
+
3
+ describe Jsonify::Builder do
4
+
5
+ let(:j) { Jsonify::Builder.new }
6
+
7
+ describe 'hello world' do
8
+ it "runs under 0.3 milliseconds" do
9
+ benchmark do
10
+ j.hello 'world'
11
+ j.compile!
12
+ j.reset!
13
+ end.should be_faster_than( 0.3 ) #milliseconds
14
+ end
15
+ end
16
+
17
+
18
+ describe 'json_builder example' do
19
+ it 'should be better than builder (0.5 milliseconds)' do
20
+ benchmark do
21
+ j.name "Garrett Bjerkhoel"
22
+ j.birthday Time.local(1991, 9, 14)
23
+ j.street do
24
+ j.address "1143 1st Ave"
25
+ j.address2 "Apt 200"
26
+ j.city "New York"
27
+ j.state "New York"
28
+ j.zip 10065
29
+ end
30
+ j.skills do
31
+ j.ruby true
32
+ j.asp false
33
+ j.php true
34
+ j.mysql true
35
+ j.mongodb true
36
+ j.haproxy true
37
+ j.marathon false
38
+ end
39
+ j.single_skills ['ruby', 'php', 'mysql', 'mongodb', 'haproxy']
40
+ j.booleans [true, true, false, nil]
41
+ j.reset!
42
+ end.should be_faster_than(0.5)
43
+ end
44
+ end
45
+
46
+ end
@@ -0,0 +1,16 @@
1
+ require 'bundler'
2
+ require 'bundler/setup'
3
+ require 'jsonify'
4
+ require 'ruby-prof'
5
+
6
+ result = RubyProf.profile do
7
+ 1.times do
8
+ json=Jsonify::Builder.new
9
+ json.hello 'world'
10
+ json.compile!
11
+ end
12
+ end
13
+
14
+ # Print a flat profile to text
15
+ printer = RubyProf::FlatPrinter.new(result)
16
+ printer.print(STDOUT)
@@ -1,7 +1,8 @@
1
+ require 'benchmark'
1
2
  require 'bundler'
2
3
  require 'bundler/setup'
3
4
  require 'jsonify'
5
+ require 'benchmark_helper'
4
6
 
5
7
  RSpec.configure do |config|
6
- # some (optional) config here
7
8
  end
@@ -0,0 +1,39 @@
1
+ require 'bundler'
2
+ require 'bundler/setup'
3
+ require 'jsonify'
4
+ require 'benchmark'
5
+
6
+ class Speed
7
+ def self.test
8
+ Benchmark.bm do |b|
9
+ b.report('Jsonify') do
10
+ 15_000.times {
11
+ j = Jsonify::Builder.new
12
+ j.name "Garrett Bjerkhoel"
13
+ j.birthday Time.local(1991, 9, 14)
14
+ j.street do
15
+ j.address "1143 1st Ave"
16
+ j.address2 "Apt 200"
17
+ j.city "New York"
18
+ j.state "New York"
19
+ j.zip 10065
20
+ end
21
+ j.skills do
22
+ j.ruby true
23
+ j.asp false
24
+ j.php true
25
+ j.mysql true
26
+ j.mongodb true
27
+ j.haproxy true
28
+ j.marathon false
29
+ end
30
+ j.single_skills ['ruby', 'php', 'mysql', 'mongodb', 'haproxy']
31
+ j.booleans [true, true, false, nil]
32
+ j.compile!
33
+ }
34
+ end
35
+ end
36
+ end
37
+ end
38
+
39
+ Speed.test
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jsonify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-08-02 00:00:00.000000000Z
12
+ date: 2011-08-03 00:00:00.000000000Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
- name: json
16
- requirement: &70365091068360 !ruby/object:Gem::Requirement
15
+ name: json_pure
16
+ requirement: &70097037247660 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70365091068360
24
+ version_requirements: *70097037247660
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: bundler
27
- requirement: &70365091067280 !ruby/object:Gem::Requirement
27
+ requirement: &70097037247160 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70365091067280
35
+ version_requirements: *70097037247160
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70365091066160 !ruby/object:Gem::Requirement
38
+ requirement: &70097037246620 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70365091066160
46
+ version_requirements: *70097037246620
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70365091065240 !ruby/object:Gem::Requirement
49
+ requirement: &70097037245940 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70365091065240
57
+ version_requirements: *70097037245940
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: autotest
60
- requirement: &70365091063920 !ruby/object:Gem::Requirement
60
+ requirement: &70097037245420 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70365091063920
68
+ version_requirements: *70097037245420
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yard
71
- requirement: &70365091062820 !ruby/object:Gem::Requirement
71
+ requirement: &70097037244760 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,10 +76,10 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70365091062820
79
+ version_requirements: *70097037244760
80
80
  - !ruby/object:Gem::Dependency
81
81
  name: rdiscount
82
- requirement: &70365091061600 !ruby/object:Gem::Requirement
82
+ requirement: &70097037244220 !ruby/object:Gem::Requirement
83
83
  none: false
84
84
  requirements:
85
85
  - - ! '>='
@@ -87,7 +87,18 @@ dependencies:
87
87
  version: '0'
88
88
  type: :development
89
89
  prerelease: false
90
- version_requirements: *70365091061600
90
+ version_requirements: *70097037244220
91
+ - !ruby/object:Gem::Dependency
92
+ name: ruby-prof
93
+ requirement: &70097037243620 !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ type: :development
100
+ prerelease: false
101
+ version_requirements: *70097037243620
91
102
  description: Turn Ruby objects into JSON -- correctly!
92
103
  email:
93
104
  - bsiggelkow@me.com
@@ -108,11 +119,15 @@ files:
108
119
  - lib/jsonify/generate.rb
109
120
  - lib/jsonify/json_value.rb
110
121
  - lib/jsonify/version.rb
122
+ - spec/benchmark_helper.rb
123
+ - spec/benchmark_spek.rb
111
124
  - spec/builder_spec.rb
112
125
  - spec/generate_spec.rb
113
126
  - spec/json_value_spec.rb
114
127
  - spec/jsonify_spec.rb
128
+ - spec/profile.rb
115
129
  - spec/spec_helper.rb
130
+ - spec/speed.rb
116
131
  homepage: http://github.com/bsiggelkow/jsonify
117
132
  licenses: []
118
133
  post_install_message:
@@ -127,7 +142,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
127
142
  version: '0'
128
143
  segments:
129
144
  - 0
130
- hash: -3226176864107262675
145
+ hash: 2291352512493608136
131
146
  required_rubygems_version: !ruby/object:Gem::Requirement
132
147
  none: false
133
148
  requirements:
@@ -136,7 +151,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
151
  version: '0'
137
152
  segments:
138
153
  - 0
139
- hash: -3226176864107262675
154
+ hash: 2291352512493608136
140
155
  requirements: []
141
156
  rubyforge_project: jsonify
142
157
  rubygems_version: 1.8.6
@@ -144,8 +159,12 @@ signing_key:
144
159
  specification_version: 3
145
160
  summary: Turn Ruby objects into JSON
146
161
  test_files:
162
+ - spec/benchmark_helper.rb
163
+ - spec/benchmark_spek.rb
147
164
  - spec/builder_spec.rb
148
165
  - spec/generate_spec.rb
149
166
  - spec/json_value_spec.rb
150
167
  - spec/jsonify_spec.rb
168
+ - spec/profile.rb
151
169
  - spec/spec_helper.rb
170
+ - spec/speed.rb