------art_2606_30122446.1202056842394
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

On Feb 1, 2008 11:30 AM, ara howard <ara.t.howard / gmail.com> wrote:

>
> On Feb 1, 2008, at 9:09 AM, Eric Mahurin wrote:
>
> >   Maybe just have a little ruby
> > script that generates a stream of repeatable random (but valid) JSON.
>
> cfp2:~ > cat a.rb
> require 'rubygems'
> require 'json'
>

The benchmark below has no gem dependencies, generates tests with deep
structures, and has self-checking.  It found a bug that the original unit
tested didn't.  By default, this benchmark keeps increasing the depth until
the runtime is > s.

Also, I made some wrappers for the json and fjson gems to see how they
perform.  Here are the results on my machine:

fjson: 224823 chars/second
json(pure): 220289 chars/second
json(ext): 1522250 chars/second

require 'benchmark'

class RandomJSON
    def initialize(value
        @number  1
        @string  1
        @boolean  1
        @constant  1
        @value  alue-1
    end
    def number
        case (@numbernumber+1)%3)
        when 0 : 0
        when 1 : 1234
        when 2 : 3.75e+1
        end
    end
    def string
        case (@stringstring+1)%3)
        when 0 : ""
        when 1 : "JSON"
        when 2 : "\"\\\/\b\f\r\t"
        end
    end
    def boolean
        case (@booleanboolean+1)%3)
        when 0 : false
        when 1 : true
        when 2 : nil
        end
    end
    def constant
        case (@constantconstant+1)%3)
        when 0 : number
        when 1 : string
        when 2 : boolean
        end
    end
    def array(depth)
        a  ]
        depth.times {
            a << value(depth-1)
        }
        a
    end
    def object(depth)
        o  }
        depth.times {
            o[string]  alue(depth-1)
        }
        o
    end
    def value(depth, vl)
        case (v||(@valuevalue+1)%3))
        when 0 : array(depth)
        when 1 : object(depth)
        else constant
        end
    end
end

generator  andomJSON.new((ARGV[1]||0).to_i)

parser  SONParser.new
Benchmark.bm { |b|
    l  il; t  il
    13.times { |depth|
        tree  enerator.value(depth, depth%2)
        s  ree.inspect
        #puts s
        s.gsub!(/, ':')
        s.gsub!(/nil/, 'null')
        tree2  il
        #puts s
        l  .length
        t  .report("#{depth} #{l}") { tree2  arser.parse(s) }
        raise if tree2!ee
        break if (t.real>
RGV[0]||1).to_f)
    }
    puts "#{(l/t.real).to_i} chars/second"
}

------art_2606_30122446.1202056842394--