JSON serialisation

Hello,

I need to make a json serialisation which should work with any data
coming from my jruby (embedded) code.

I use SOJO framework which is ok.
But JSON has some constraints :

  • Object keys must be String
  • Date Time cant be coded, whe need convert them in String

So i realise this little function which prepare the data to be
serialized
(code in string any data which JSON dont know) :

def to_serialise(o)
case o
when Java::JavaUtil::ArrayList then o.map { |no| to_serialiseno)
}
when Java::JavaUtil::HashMap then o.inject({}) {
|h,kv| h[kv[0].to_s] = to_serialisekv[1]) ; h
}
when Array then o.map { |no| to_serialise(no) }
when Hash then o.inject({}) {
|h,kv| h[kv[0].to_s] = to_serialise(kv[1]) ; h
}
when Fixnum then o
else
o.to_s
end
end

All work find, but CPU time is realy excessive (15 ms traitment become
180 ms with
little Hash …)
So i wonder howto write that in Java : i don’t known how iterate on
Hash/Array org.jruby
in java…

Can someone help me ?

Thanks,
Régis

I think your paste got a little messed up, so I made a cleaned-up
version plus a benchmark here: http://gist.github.com/335975

For this benchmark, it takes a bit less than 100ms per iteration for a
10000-element hash containing string keys and hash values which
themselves have fixnum keys and array values containing fixnums. So
the structure is roughly:

10000 * {string => {fixnum => [fixnum]]

That performance seems reasonable, and that seems to be a reasonably
large hash. Here’s the benchmark numbers on my system with Java 6 in
client mode:

~/projects/jruby âž” jruby serializer.rb
0.447000 0.000000 0.447000 ( 0.422000)
0.100000 0.000000 0.100000 ( 0.100000)
0.100000 0.000000 0.100000 ( 0.100000)
0.135000 0.000000 0.135000 ( 0.135000)
0.099000 0.000000 0.099000 ( 0.099000)

In server mode, it’s better still:

~/projects/jruby âž” jruby --server serializer.rb
1.046000 0.000000 1.046000 ( 0.979000)
0.357000 0.000000 0.357000 ( 0.357000)
0.060000 0.000000 0.060000 ( 0.060000)
0.062000 0.000000 0.062000 ( 0.062000)
0.056000 0.000000 0.056000 ( 0.056000)

Granted, I don’t have any code hitting ArrayList or HashMap instances.
Perhaps that’s where your performance bottleneck comes from? There are
improvements in JRuby 1.4 and again in JRuby 1.5 for calling out to
Java code. For example, the cost of iterating a 10000-element
ArrayList in the past three JRuby versions:

http://gist.github.com/335979

So it’s also possible your performance issues may improve with newer
versions anyway.

If you really needed to write this in Java, it’s not going to be too
difficult. In JRuby, a Ruby Hash implements java.util.Map and a Ruby
Array implements java.util.List, so code that can walk those types
will work fine. But I’d ideally like to make it possible for you to
leave the code in Ruby, so we should work together to see if we can
speed it up.

I’d recommend giving JRuby 1.5 dev snapshots a try:
http://ci.jruby.org/snapshots

On Wed, Mar 17, 2010 at 4:26 PM, Regis d’Aubarede removed_email_addr[email protected]
wrote:

    }
All work find, but CPU time is realy excessive (15 ms traitment become

Posted via http://www.ruby-forum.com/.


To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Hello,

Thank you for your benchmark.
I have rewrite the function in java :

http://gist.github.com/336594

Effectively, performances differences are not so big
(sorry, i am embedded in a server container, i have not
try Benhmark class…):

trace with ruby function :

:33,308 | …> Servlet “Vol.test” : 62 ms
:33,637 | …> Servlet “Vol.test” : 297 ms
:33,998 | …> Servlet “Vol.test” : 141 ms
:34,060 | …> Servlet “Vol.test” : 15 ms
:34,154 | …> Servlet “Vol.test” : 78 ms
:34,452 | …> Servlet “Vol.test” : 157 ms
:34,514 | …> Servlet “Vol.test” : 15 ms
:34,859 | …> Servlet “Vol.test” : 63 ms
:35,016 | …> Servlet “Vol.test” : 16 ms
:35,266 | …> Servlet “Vol.test” : 78 ms
:35,454 | …> Servlet “Vol.test” : 31 ms
:35,580 | …> Servlet “Vol.test” : 16 ms
:35,924 | …> Servlet “Vol.test” : 62 ms
:36,191 | …> Servlet “Vol.test” : 16 ms
:36,285 | …> Servlet “Vol.test” : 16 ms
:36,441 | …> Servlet “Vol.test” : 47 ms
:36,692 | …> Servlet “Vol.test” : 63 ms
:36,833 | …> Servlet “Vol.test” : 16 ms
:37,021 | …> Servlet “Vol.test” : 16 ms
:37,757 | …> Servlet “Vol.test” : 141 ms
:37,835 | …> Servlet “Vol.test” : 15 ms
:37,976 | …> Servlet “Vol.test” : 125 ms
:39,041 | …> Servlet “Vol.test” : 125 ms
:39,088 | …> Servlet “Vol.test” : 15 ms
:39,120 | …> Servlet “Vol.test” : 16 ms
:39,261 | …> Servlet “Vol.test” : 16 ms
:39,386 | …> Servlet “Vol.test” : 110 ms
:39,684 | …> Servlet “Vol.test” : 94 ms
:39,981 | …> Servlet “Vol.test” : 94 ms
:40,357 | …> Servlet “Vol.test” : 125 ms
:40,905 | …> Servlet “Vol.test” : 62 ms
:41,548 | …> Servlet “Vol.test” : 141 ms
:41,736 | …> Servlet “Vol.test” : 16 ms
:42,346 | …> Servlet “Vol.test” : 125 ms

traces with java function

2:52,009 | …> Servlet “Vol.test” : 15 ms
2:52,479 | …> Servlet “Vol.test” : 31 ms
2:52,714 | …> Servlet “Vol.test” : 16 ms
2:52,871 | …> Servlet “Vol.test” : 126 ms
2:53,027 | …> Servlet “Vol.test” : 15 ms
2:53,168 | …> Servlet “Vol.test” : 125 ms
2:53,294 | …> Servlet “Vol.test” : 16 ms
2:53,356 | …> Servlet “Vol.test” : 47 ms
2:53,466 | …> Servlet “Vol.test” : 16 ms
2:53,607 | …> Servlet “Vol.test” : 63 ms
2:53,999 | …> Servlet “Vol.test” : 126 ms
2:54,531 | …> Servlet “Vol.test” : 15 ms
2:55,032 | …> Servlet “Vol.test” : 156 ms
2:55,126 | …> Servlet “Vol.test” : 15 ms
2:55,330 | …> Servlet “Vol.test” : 157 ms
2:55,549 | …> Servlet “Vol.test” : 109 ms
2:55,581 | …> Servlet “Vol.test” : 32 ms
2:55,894 | …> Servlet “Vol.test” : 31 ms
2:56,348 | …> Servlet “Vol.test” : 329 ms
2:57,116 | …> Servlet “Vol.test” : 16 ms
2:57,272 | …> Servlet “Vol.test” : 141 ms
2:57,460 | …> Servlet “Vol.test” : 15 ms

I have embeded my JRuby/JSON server in a Jetty server, which is embedded
in a scada system.
and now i work in respond/generate data for a ExtJs Crud application in
navigator…

by

On Thu, Mar 18, 2010 at 12:43 PM, Regis d’Aubarede
[email protected] wrote:

Hello,

Thank you for your benchmark.
I have rewrite the function in java :

http://gist.github.com/336594

Effectively, performances differences are not so big
(sorry, i am embedded in a server container, i have not
 try Benhmark class…):

Yes, the performance seem to be about the same. To me this means that
either the Ruby collections are slowing things down equally in both
cases, or else there’s just a lot of data to walk and the walking
itself isn’t slowing things down. You probably should try running a
Java profiling tool if this performance is not good enough…it may
help you find where the problem lies. If it’s in JRuby’s collections,
we can see about improving them.

  • Charlie

To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs