NetTalk Central

Author Topic: Webservice and (threading?) performance issues with json output  (Read 7879 times)

AtoB

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Webservice and (threading?) performance issues with json output
« on: November 19, 2015, 09:52:50 AM »
Hi (Bruce),

I think there is an issue with a webmethods that proceduce (large sets of) json output on several threads concurrently.

I have a method that produces a rather large resultset (10,000+ items in a Queue). When I serve this data as JSON and activate this method multiple times (more or less concurrently) the server starts launching new threads (as it should), but then the server starts to crawl.

I've reproduced this by adding a new "method" to  the 77 example app that simply loads data from an xml file to a queue and than simply serves that queue as JSON. Serving a single set takes roughly 20 seconds, asking for more sets while the first is still being processed, gets the webserver on its knees (I think memory consumption is also too high ... and sometimes it crashes (and gpf reporter doesn't kick in) ...

Simply letting the method serve xml instead of JSON (very same Queue with the same data, but now only served as xml) and the problem doesn't appear!!! Asking for three sets "concurrently" takes a little over a minute and nicely ends in all threads being closed and server ready for other requests.

Now if can have that for json too, I'll be more than happy <g>

using NT 8.65 and jFiles 1.13/1.14

BTW, just starting with webservices, but really love the nettalk product very much, there's been some great work done in there!

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Webservice and (threading?) performance issues with json output
« Reply #1 on: November 19, 2015, 09:19:15 PM »
Hi Vic,

Can you send me your example so I can duplicate here please?

Also - can you give me some idea of the machine you are testing on so I can gauge relative performance - specifically the number of cores you have, and how much ram the machine has.

cheers
Bruce

AtoB

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Webservice and (threading?) performance issues with json output
« Reply #2 on: November 20, 2015, 01:24:41 AM »
Hi Bruce,

just sent you a pm with steps to (hopefully) reproduce the issue

Instructions;
Run the server and from a client "get" the localhost:88//Test/TestLargeResult method just once. Depends on hardware but over here it finished in 2.00 seconds (as xml), now request this several times in a row, let's say 5 times), now it takes 35 seconds but finishes normally

Now ask for json ("Accept: application/json") once and the request finishes in 3.78 seconds over here. Now ask for json 5 times in a row and it will take the server 790 seconds (to proces these 5 request)

The "first" request finished in 60 seconds than several minutes the cpu settles at around 25 % (4 cores over here) and the last couple of seconds the cpu on the web77 proces goes up to 50 % and it "serves" the other 4 pending requests (gathering from the performance tab on the webserver)

Also note:

after serving 1 xml set, the web77.exe uses 7.072 kB of memory
after serving 1 json set the web77.exe uses 269.044 kB of memory !

I'm running latest NT/JFiles/xFiles as of this morning on an i5 , 4 cores with over 2 gB available when starting web77. After serving 5 json sets (concurrently) the exe uses 1.8 gB ...

If you need any more info/details, just let me know
« Last Edit: November 20, 2015, 05:01:47 AM by Bruce »

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Webservice and (threading?) performance issues with json output
« Reply #3 on: November 22, 2015, 11:54:28 PM »
Hi Ton,

I spent some time on this over the weekend. The results were most interesting.

I used the WebStrain example (examples\NetTalk\WebClient\WebStrain) as the test client. This allows you to re-do a test consistently, across multiple threads to see what's happening. Obviously the tests are very server-dependent, so are not comparable from one machine to another, but it's useful to test difference scenarios.

Basically the major time consumption is in three places;
a) loading the xml file from the disk, and parsing this into a queue. The file is large (around 2 megs) and contains about 42000 records so this takes a bit of time to do. On my machine this takes roughly 1.8 seconds.

b) converting the queue to json objects, 1.2 seconds (*)

c) saving the json objects to a serialized string for saving out to disk - about 0.3 seconds.
Total time is about 3.4 seconds for a single request.

(*) This is after the optimizations I made. It was going a bit slower than this for this step so I made some small tweaks to the class, and the StringTheory class, to speed it up a bit.

Memory usage was quite high - you're making 42000 instances of the object (in JsonClass each node is a separate instance.) Again I made a number of tweaks to both StringTheory and jFiles to reduce memory usage. It was using about 270 megs for the test - this is now down to 60 megs. (Which sounds like a lot, but the object does not last long.)

Incidentally because of the jFiles approach to JSON - where the nodes are parsed and stored as a collection of classes, the speed of processing and the memory used are very different to xFiles. xFiles is built for raw speed, which makes it very fast (and also uses very little ram) but jFiles is MUCH easier to work with when complex structures are encountered. It's an interesting trade-off, and it's interesting to see it contrasted so well here.

Then the really interesting stuff started to happen. I set WebString to use 4 threads, and make 4 requests (simultaneously.)  Given the request time is 3.4 seconds I would have expected the test to take about 13.6 seconds (maximum) and in fact (knowing it can leverage multiple cores) perhaps a bit less than that.

Interestingly it didn't do this. Processing the incoming XML multi-tasked well (it took 3.8 seconds instead of 1.8, but it did 4 files, not 1). The final step of converting the Json tree to a Serialized string also multi-tasked well. But the middle step of constructing the tree took about 52 seconds (to do all 4).

Clearly allowing the OS to switch the threads while constructing the tree is massive inefficient. I don't fully understand why, but I'm guessing it surrounds the getting of Ram. (This isn't simply a contention issue, because then it would just take 4* 3.4 at worst).

I then added a critical section around the construction of the tree - in other words forcing one to complete before the next one could begin. I only wrapped the tree-construction phase leaving the other two phases to multi-task as before.

Now the test (running 4 requests on 4 threads at the same time) takes 9 seconds to complete. Which is more in line with what one might expect.

The solution definitely seems to be serializing the creation of the tree, but at this point I'm not 100% sure if I want to do that in jFiles itself, or in the NetTalk Method class. I'll be checking into that as the week progresses.

update: The critical section has been built into jFiles itself and released in build 1.17, so download that and please try your test again.

Thanks for the report though, and the example, it's been fascinating to work through this.

The StringTheory and jFiles improvements for memory usage are on the web site and available for download.

cheers
Bruce







« Last Edit: November 23, 2015, 02:25:53 AM by Bruce »

AtoB

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Webservice and (threading?) performance issues with json output
« Reply #4 on: November 23, 2015, 07:31:33 AM »
Hi Bruce,

first of all : thanks very much for ruining your weekend for this!

But, I still have performance issues ...

The single json request runs in 3.22 (that's faster!). But the 5 request still take roughly 445 seconds ...

I tried the Strain tester too and it does finish in 8 seconds (for four requests), but can it be that this is only serving xml? Can I ask for json with this application somehow? I

b.t.w. I upgraded jFiles and Stringtheory, both Nettalk and xFiles already were on the latest

I removed all obj/rsc etc. and did a full rebuild (I think ... still not sure about that in c10 ...), but I can't get it any faster

sorry for ruining your day

If you need any additional info or want me to test something let me know!

Regards
Ton

AtoB

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Webservice and (threading?) performance issues with json output
« Reply #5 on: November 23, 2015, 07:52:33 AM »
now I'm 99% sure that webstrain was asking for xml in my case ..., just tried with Fiddler and it shows an xml result ... still figuring out how to ask for json with webstrain ...

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Webservice and (threading?) performance issues with json output
« Reply #6 on: November 24, 2015, 12:52:09 AM »
I tweaked it here to support that (in the next build.)

For now you can force it, BrowseTest procedure
In the Get Routine
before the call to net.Fetch

net.Accept_ = 'application/json'

cheers
Bruce

AtoB

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Webservice and (threading?) performance issues with json output
« Reply #7 on: November 24, 2015, 04:33:28 AM »
Hi Bruce,

yep, that was it to produce json.

And letting webstrain put 4 request in simultaniously shows almost identical result as you have indicated But my example still choked (by pressing a button with a second or so pause between presses). Then (for fun) I started two session of the webstrain app and pressed the "play" button in each app with a second or two pause between them. Now I see the performance degredation too !

In this case I asked for 2 x 4 = a total of 8 sessions. Two finish in normal/expected time (3.19 s each), but the other 6 take 141 seconds in total (so for 24s each request)

When I asked for xml the 8 request get served in 22 seconds (so 2.69s each). What I did notice that the server never hit 8 thread running (actualy the maximum was 2 threads ....), don't know wether this has something to do with it ...

Again if you any additional info I'll do my best

Regards,
Ton

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Webservice and (threading?) performance issues with json output
« Reply #8 on: November 26, 2015, 01:52:14 AM »
I'll bump the test out a bit to see if I can refine the code. However bear in ind that comparing it to XML is not useful. xFiles is built primarily for speed, jFiles is built for easier traversal of the json tree.

Therefore xFiles will always be massively faster and consume much much less ram than jFiles. No matter what I do that won't change, and because the libraries are so different they are not comparable in performance in any way.

Cheers
Bruce

AtoB

  • Jr. Member
  • **
  • Posts: 74
    • View Profile
    • Email
Re: Webservice and (threading?) performance issues with json output
« Reply #9 on: November 26, 2015, 02:37:28 AM »
Hi Bruce,

I've looked at the jFiles class a little trying to improve performance and can't really tell what's making it slower (but the mechanism is not entirely clear (yet), so that might be a problem too <g>).

In itself the json is roughly a factor 1.2 to 1.5 slower than xml (for the tests I'v run) that's fine/acceptable. but when the requests come in randomly serving json becomes really troublesome. My production server will have to server even larger result sets and I can't hope that there will be no "collisions"

So I'm still looking for a way to produce and serve json fast

Maybe a really stupid question: but is there a chance of producing JSON with the xFiles class (in the end it goes through the same structures and I can see "some" resemblense between xml and json <g>)?

Or what would be another simple method of producing "flat" json output easily (my result sets are always "simply" a set of queues and some of them will have child/grandchild result sets, that it <g>)?

Regards,
Ton



Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Webservice and (threading?) performance issues with json output
« Reply #10 on: November 27, 2015, 03:21:01 AM »
I've started a new thread to continue this discussion, so this thread does not get too long;
http://www.nettalkcentral.com/index.php?option=com_smf&Itemid=36&topic=6687.0