Skip to content

Commit

Permalink
switch to yajl for less headaches with Rails
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Jul 17, 2010
1 parent 9e32c72 commit ba93483
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 18 deletions.
5 changes: 4 additions & 1 deletion examples/runner.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
require 'rubygems'
require 'rack'

require 'lib/rack/speedtracer'
$LOAD_PATH.unshift 'lib'
require 'rack/speedtracer'

class SomeApp
def call(env)
Expand All @@ -10,6 +11,8 @@ def call(env)
5**10000
end

env['st.tracer'].run('sleep(0.01)') { sleep(0.01) }

5**100000
end

Expand Down
4 changes: 2 additions & 2 deletions lib/rack/speedtracer.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
14 changes: 7 additions & 7 deletions lib/rack/speedtracer/tracer.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'json'
require 'yajl'

module Rack
module SpeedTracer
Expand Down Expand Up @@ -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' => {
Expand All @@ -49,7 +49,7 @@ def to_json
'label' => @name
},
'children' => @children
}.to_json
})
end
end

Expand Down Expand Up @@ -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
Expand All @@ -101,7 +101,7 @@ def run(name = '', &blk)
def finish
super()

{
Yajl::Encoder.encode({
'trace' => {
'date' => @start.to_i,
'application' => 'Rack SpeedTracer',
Expand All @@ -117,8 +117,8 @@ def finish
'children' => @children
}
}
}.to_json
})
end
end
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require 'rubygems'
require 'rack/speedtracer'
require 'json'
require 'yajl'
require 'spec'
require 'pp'

Expand Down
2 changes: 1 addition & 1 deletion spec/speedtracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down
12 changes: 6 additions & 6 deletions spec/tracer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand All @@ -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')
Expand Down

0 comments on commit ba93483

Please sign in to comment.