Permalink
Browse files

switch to yajl for less headaches with Rails

  • Loading branch information...
1 parent 9e32c72 commit ba93483a77fa8a14ea1002ad6ced9e08ed5f4734 @igrigorik committed Jul 17, 2010
Showing with 21 additions and 18 deletions.
  1. +4 −1 examples/runner.rb
  2. +2 −2 lib/rack/speedtracer.rb
  3. +7 −7 lib/rack/speedtracer/tracer.rb
  4. +1 −1 spec/spec_helper.rb
  5. +1 −1 spec/speedtracer_spec.rb
  6. +6 −6 spec/tracer_spec.rb
View
@@ -1,7 +1,8 @@
require 'rubygems'
require 'rack'
-require 'lib/rack/speedtracer'
+$LOAD_PATH.unshift 'lib'
+require 'rack/speedtracer'
class SomeApp
def call(env)
@@ -10,6 +11,8 @@ def call(env)
5**10000
end
+ env['st.tracer'].run('sleep(0.01)') { sleep(0.01) }
+
5**100000
end
View
@@ -1,7 +1,7 @@
require 'rack'
-require 'lib/rack/speedtracer/context'
-require 'lib/rack/speedtracer/tracer'
+require 'rack/speedtracer/context'
+require 'rack/speedtracer/tracer'
module Rack
module SpeedTracer
@@ -1,4 +1,4 @@
-require 'json'
+require 'yajl'
module Rack
module SpeedTracer
@@ -36,7 +36,7 @@ def initialize(id, file, line, method, name)
end
def to_json
- {
+ Yajl::Encoder.encode({
'range' => range(@start, @finish),
'id' => @id,
'operation' => {
@@ -49,7 +49,7 @@ def to_json
'label' => @name
},
'children' => @children
- }.to_json
+ })
end
end
@@ -90,7 +90,7 @@ def run(name = '', &blk)
event.finish # finalize current event timers
@pstack.pop # pop current event from parent stack
- if parent = @pstack.pop
+ if parent = @pstack.last
parent.children.push event
else
# no parent, means this is a child of root node
@@ -101,7 +101,7 @@ def run(name = '', &blk)
def finish
super()
- {
+ Yajl::Encoder.encode({
'trace' => {
'date' => @start.to_i,
'application' => 'Rack SpeedTracer',
@@ -117,8 +117,8 @@ def finish
'children' => @children
}
}
- }.to_json
+ })
end
end
end
-end
+end
View
@@ -2,7 +2,7 @@
require 'rubygems'
require 'rack/speedtracer'
-require 'json'
+require 'yajl'
require 'spec'
require 'pp'
View
@@ -31,7 +31,7 @@
end
it 'should return a stored trace in JSON format' do
- sample_trace = {'trace' => {}}.to_json
+ sample_trace = Yajl::Encoder.encode({'trace' => {}})
respond_with(200)
response = get('/speedtracer?id=test') do |st|
View
@@ -10,11 +10,11 @@
let(:tracer) { Rack::SpeedTracer::Tracer.new(1, 'GET', '/test') }
it 'should serialize to json on finish' do
- lambda { JSON.parse(tracer.finish) }.should_not raise_error
+ lambda { Yajl::Parser.parse(tracer.finish) }.should_not raise_error
end
it 'should conform to base speedtracer JSON schema' do
- trace = JSON.parse(tracer.finish)['trace']
+ trace = Yajl::Parser.parse(tracer.finish)['trace']
# Example base trace:
# {"date"=>1279403357,
@@ -64,14 +64,14 @@
it 'should measure execution time in milliseconds' do
tracer.run { sleep(0.01) }
- trace = JSON.parse(tracer.finish)['trace']
+ trace = Yajl::Parser.parse(tracer.finish)['trace']
trace['range']['duration'].to_i.should == 10
end
it 'should report traced codeblocks' do
tracer.run { sleep(0.01) }
- trace = JSON.parse(tracer.finish)['trace']
+ trace = Yajl::Parser.parse(tracer.finish)['trace']
trace['frameStack']['children'].size.should == 1
@@ -84,7 +84,7 @@
it 'should accept optional label for each trace' do
tracer.run('label') { sleep(0.01) }
- trace = JSON.parse(tracer.finish)['trace']
+ trace = Yajl::Parser.parse(tracer.finish)['trace']
trace['frameStack']['children'].first['operation']['label'].should match('label')
end
@@ -94,7 +94,7 @@
tracer.run('child') { sleep(0.01) }
end
- trace = JSON.parse(tracer.finish)['trace']
+ trace = Yajl::Parser.parse(tracer.finish)['trace']
parent = trace['frameStack']['children'].first
parent['operation']['label'].should match('parent')

0 comments on commit ba93483

Please sign in to comment.