#1 2013-09-10 16:01:40

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Thread-safety of mORMot

We tried to make mORMot at the same time fast and safe, and able to scale with the best possible performance on the hardware it runs on. Multi-threading is the key to better usage of modern multi-core CPUs, and also client responsiveness.

As a result, on the Server side, our framework was designed to be thread-safe.

On typical production use, the mORMot HTTP server will run on its own optimized thread pool, then call the TSQLRestServer.URI method. This method is therefore expected to be thread-safe, e.g. from the TSQLHttpServer. Request method. Thanks to the RESTful approach of our framework, this method is the only one which is expected to be thread-safe, since it is the single entry point of the whole server. This KISS design ensure better test coverage.


Forum thread for Blog entry of http://blog.synopse.info/post/2013/09/1 … -of-mORMot

Online

#2 2013-09-12 08:53:23

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

I found very interesting site http://www.techempower.com/benchmarks/
I think it's a good idea to participate (for framework popularization).
It seems we must be in top of list for Windows platform smile http://www.techempower.com/benchmarks/# … est=update

Offline

#3 2013-09-12 11:51:01

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

mpv wrote:

It seems we must be in top of list for Windows platform smile http://www.techempower.com/benchmarks/# … est=update

If I understand correctly the scope of this benchmark, and compare to our existing values, I suspect mORMot may perform quite well!

It should be easy to implement some sample applications matching the benchmark expectations at:
http://www.techempower.com/benchmarks/# … est=update

The C# source code available is pretty straightforward to me:
https://github.com/TechEmpower/Framewor … Program.cs
But it does not include ORM feature, just raw SQL - I'm quite confident our ORM will do wonders here.

The only missing feature is IMHO the HTML template.
We may hardcode it at first, adding some HTML escaping dedicated methods to our writer classes (or a dedicated XML/HTML class).
But we may give some code.

We did not make intensive test about MySQL/PostgreSQL, and it sounds like if this benchmark is using MySQL/PostgreSQL as backend, in addition to MongoDB.
Do you think it is possible to use SQLite3 at first? And/or simply rely on ZDBC connection, which is pretty good about reading.

I do not have much usage of Git nor GitHub, perhaps someone may help providing the pull requests?

What do you think?

Online

#4 2013-09-12 19:00:41

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

In the "Test requirements" section exist remark about "If you have limited time, we recommend you start with the easiest tests (1 through 3)" - so we can participate without template. For HTML template I can add Mustache support. SpiderMonkey  implementation not yet ready for including  into mORMot, but this use-case I can implement......
I just test  - not-optimized version give 1300 RPS for they template (w/o database support but using poor hardware (Core i5U). So I can implement section 4.

I think we can try to use sqlite3....
I also do not use git, so can't help in this part.

UPD.
Small optimization and I got 1750 RPS smile

Last edited by mpv (2013-09-12 19:05:42)

Offline

#5 2013-09-13 07:37:00

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

Another small optimization, and we got 19710 Request Per Second!
Result for command: > ab -c128 -n10000 -k http://localhost:888/m3/muchacha  (run 10000 request 128 concurrent thread, keep-alive)

This is ApacheBench, Version 2.3 <$Revision: 1178079 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)


Server Software:        Microsoft-HTTPAPI/2.0
Server Hostname:        localhost
Server Port:            888

Document Path:          /m3/muchacha
Document Length:        191 bytes

Concurrency Level:      128
Time taken for tests:   0.507 seconds
Complete requests:      10000
Failed requests:        0
Write errors:           0
Keep-Alive requests:    10000
Total transferred:      3940000 bytes
HTML transferred:       1910000 bytes
Requests per second:    19710.61 [#/sec] (mean)
Time per request:       6.494 [ms] (mean)
Time per request:       0.051 [ms] (mean, across all concurrent requests)
Transfer rate:          7583.96 [Kbytes/sec] received

Connection Times (ms)
              min  mean[+/-sd] median   max
Connect:        0    0   0.0      0       1
Processing:     0    6   3.0      6     115
Waiting:        0    6   3.0      6     115
Total:          0    6   3.0      6     115

Test source ( this JS code evaluated into every mOROMot working thread )
mustache.js taken here: https://github.com/janl/mustache.js

var muchacha = (function(){
  var 
    data = [{message: "hi!!!"}, {message: "lo!!"}];

var
  tpl='<!DOCTYPE html>\
<html>\
<head><title>Fortunes</title></head>\
<body>\
<table>\
<tr><th>id</th><th>message</th></tr>\
{{#.}}\
<tr><td>{{id}}</td><td>{{message}}</td></tr>\
{{/.}}\
</table>\
</body>\
</html>';

var compiled = Mustache.compile(tpl);
  
  return function(req, resp){
	var output = compiled(data);

  	resp.writeEnd(output);
	resp.writeHead('Content-type: text/html');
  	resp.statusCode = 200;
  }
})();

Offline

#6 2013-09-13 08:31:36

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

Very nice!
smile

What "optimization" are you talking about?
What is your test code?

BTW, what is the amount of memory and CPU used on the server side during the test, if you let it run for some time?
I'm wondering about SpiderMonkey text processing impact on memory and performances.

Can't wait for SpiderMonkey integration within mORMot...

Online

#7 2013-09-13 10:43:36

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

"optimization" -  Just optimize JS code (compile template first).
Max memory usage during 1 million request with 128 concurrency keep-alive  connection is 120 Mb (initial 28 Mb - my server more thick compared to mORMot). I'm use manual garbage collection, so memory under full control.

Such performance is reached because of 2 thing:
  1) Just In Time (JIT) compilation - actually after 2-nd run SpiderMonkey operate in ASM level.
  2) Inside SpiderMonkey all objects organized in structure like your TRawUTF8ListHashed, so all  property lookup operation is O(1).

Last edited by mpv (2013-09-13 10:57:02)

Offline

#8 2013-09-14 15:19:27

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

AB, I send you files for add SM support to mORMot. You cat put SynSM* as initial release into fossil repositiry.

In general - as for me is not a time to extend SM support more then in current implementation - we must wait for new stable SpiderMonkey version. As Mozilla sad: "The next release will be SpiderMonkey 24, around November 2013".

New SM24 API is very different from SM17 I currently use. Many thing become more simple. Debugger interface added, ES6 added (classes and types for variables). So my advice is to wait for SM24 release and when extend current implementation with RTTI support and simplify many things I don't like in current implementation.
But current implementation allow use SpiderMonkey 17 on low level...

Offline

#9 2013-09-14 21:05:10

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

This is great news!

We will take a look at your source code, and include it in the trunk version ASAP!

smile

100% OK with the fact that SM24 will be a huge step forward: classes and typed variables will be a welcome enhancement, also for execution speed.

Thanks a lot!

Online

#10 2013-09-16 03:48:53

BrentG
Member
Registered: 2012-04-05
Posts: 31

Re: Thread-safety of mORMot

"Typical results are the following:
Threads     Clients/
thread     Rows
inserted     Total
Clients     Time
(sec)     Op/sec
1     1     10000     1     15.78     1267
50     1     10000     50     2.96     6737
100     1     10000     100     3.09     6462
100     1     20000     100     6.19     6459
50     2     100000     100     34.99     5714
100     2     100000     200     36.56     5469
500     100     100000     50000     92.92     2152"


Are you sure "15.78" seconds is correct for 1 thread 1 client?

Brent

Offline

#11 2013-09-16 05:58:59

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

BrentG wrote:

Are you sure "15.78" seconds is correct for 1 thread 1 client?

AFAIK 10000/15.78 = 633.71 records write+read per second = 1267 HTTP requests per second.

This is in fact the network latency of the client-server access.
We are using a slow entreprise 100 MB local network with a hub, in this case, not a localhost test - which does not make much sense.

Very little CPU is used on both sides, in this case.

So it does make sense to me.

Online

#12 2013-09-18 12:50:57

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

My colleague gave me access to good hardware (the same as here http://www.techempower.com/benchmarks/# … &test=json ). For request to timeStamp method I reach 180 000 response per second. For request to JavaScript method w/o database access - near 110 000 request per second. This is awesome!

Offline

#13 2013-09-18 13:58:44

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

Impressive results!

Online

#14 2013-09-20 13:06:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

That's it...
Code has been committed...

Introducing JavaScript execution using the SpiderMonkey library in mORMot framework!
See http://synopse.info/fossil/info/744111d689
Thanks a lot Pavel for all this GREAT contribution!

Now we can start integrating it officially within mORMot...
How could we do this in the most "KISS" way?

Online

#15 2013-09-23 17:16:33

louis_riviera
Member
Registered: 2013-09-23
Posts: 61

Re: Thread-safety of mORMot

Could this be used to execute Javascript with Synapse HTTP components? Will it work with libraries such as JQuery, that is the question smile

Offline

#16 2013-09-23 17:21:20

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

I'm afraid you are making a little confusion.
AFAIK JQuery expected a DOM object tree to be available, not only a JavaScript.
IMHO there is no JQuery outside the web.

What you can do is something more like http://nodejs.org/
With the benefit of the mORMot core to be multithreaded, so potentially with much better performance than node.js.

You can use some part of JQuery within node.js - see http://stackoverflow.com/questions/1801 … th-node-js
But I suspect this is not what you want, and I doubt we will ever provide such a feature here, unless we provide compatibility with node.js API - which was envisaged but not yet planned.

Online

#17 2013-09-23 17:22:46

louis_riviera
Member
Registered: 2013-09-23
Posts: 61

Re: Thread-safety of mORMot

Yes but AFAIK nodejs has DOM. smile And yes nodejs can run JQuery.

Last edited by louis_riviera (2013-09-23 17:24:26)

Offline

#18 2013-09-23 17:24:24

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

louis_riviera wrote:

Yes but AFAIK nodejs has DOM. smile

Not nativly, you need third party source code of a node.js library.
See my answer just above.
smile

What we are doing for instance is to use mustache JS library for HTML templates.
Works pretty well.
Or any other nice features like that.

Online

#19 2013-09-23 17:28:22

louis_riviera
Member
Registered: 2013-09-23
Posts: 61

Re: Thread-safety of mORMot

Actually DOM can be used with this. https://github.com/tmpvar/jsdom
Or a better implementation https://github.com/MatthewMueller/cheerio

And this is the end result http://net.tutsplus.com/tutorials/javas … nd-jquery/

It is all possible smile Now you see that JQuery is INDEED used in server environment.

Last edited by louis_riviera (2013-09-23 17:41:48)

Offline

#20 2013-09-23 18:15:17

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

There is no DOM on the server level. DOM is browser-specific object passed to java script. Actually, browser implementation (fireFox or Chrome) pass to javaScript object named "window", and this object (written on C or C++) implement ALL method we usually call DOM ( getElementByID, querySelector and so on). jQuery is just helper build around "window" object.  For example node.js (and our implementation) do not have "window" object, they have "global" object instead.  Things you call "DOM in node.js" is just an emulation of DOM for only one reason - parse HTML downloaded from other sources. If someone really need to work with DOM on server level is better to use http://phantomjs.org/.

Offline

#21 2013-09-23 18:26:07

louis_riviera
Member
Registered: 2013-09-23
Posts: 61

Re: Thread-safety of mORMot

Yes but AFAIK you cannot control PhantomJS ..

Offline

#22 2013-09-23 19:16:12

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

Yes - we cannot control PhantomJS. I mean what if someone REALLY need to have DOM on the server PhantomJS is only one "real" server-side DOM implementation (the same codebase as in WebKit) - anything else is emulation. But IMHO DOM is not needed for server-side. DOM is a GIU. So it must be used only on cilent-side.

Offline

#23 2013-10-03 13:04:47

louis_riviera
Member
Registered: 2013-09-23
Posts: 61

Re: Thread-safety of mORMot

Also why bother with SpiderMonkey? If you can use BESEN. An ECMASCRIPT 5 full implementation in Pascal also cross platform.
No dll's are required.

https://code.google.com/p/besen/

Offline

#24 2013-10-03 15:03:00

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

Yes, we know about BESEN since years (just search this forum).
See http://synopse.info/forum/viewtopic.php?pid=6298#p6298

mpv wrote:

Why SpiderMonkey
I look at many engine before chose SpiderMonkey.
- pure Delphi Besen http://code.google.com/p/besen/  ( bad performance speed/memory compared to V8 & SM)
- V8 (C++ hard to integrate with Delphi)
- Microsoft scripting host ( COM / poor API / bad performance / only for Windows)
I choose SpiderMonkey because:
- Stability. It is the oldest, most heavily tuned engine available.
- Open source  with HUGE community.
- C API – easy to integrate with Delphi
- ECMA-262 Edition 5 (and Harmony soon)
- It is very fast. Really VERY fast.

Online

#25 2013-10-03 16:19:19

louis_riviera
Member
Registered: 2013-09-23
Posts: 61

Re: Thread-safety of mORMot

Well hopefully mpv can provide some examples on how to use it. Specifically how to add new functions etc.

Last edited by louis_riviera (2013-10-03 16:21:09)

Offline

#26 2013-10-07 19:45:02

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

I just add "/Samples/22-JavaScript HTTPApi web server"

Run sample, point browser to http://localhost:888/root/readme.md - response is MarkDown template rendered by SpiderMonkey.
DLL is here http://synopse.info/files/synsm.7z
Tested only for Delphi7 but hope it work with other versions.


AB - Sorry for commit comments - this is my first usage of fossil - I miss autosync is on.

Offline

#27 2013-10-08 09:16:07

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

This is just great.

And do not worry for the comments - it is the first time someone outside me is commited something, and this is another great news!

smile

Online

#28 2013-10-08 22:38:41

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Re: Thread-safety of mORMot

Very nice indeed.

In project 22, can anyone suggest how to achieve auto direct user to login page from "root context"?
I mean if user types in browser-url "http://localhost:888/root", I would expect my login page to load (index.html).

Offline

#29 2013-10-09 10:10:04

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

You can check the "FileName" local variable: if it is '', you may try to change it into 'index.html".

Online

#30 2013-10-11 01:00:37

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Re: Thread-safety of mORMot

Ok, it didn't work as expected, the referenced files inside index.html such as (.css, .js, etc.) are not processed, that is, not sent to client browser and the displayed page is incomplete.
If I set FileName := 'index.html' then Response.Content := is always index.html

Offline

#31 2013-10-11 08:01:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: Thread-safety of mORMot

You have to set FileName := 'index.html' only for a void /root !!!
smile

Online

#32 2013-10-11 14:52:45

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

in JSHttpApiServer.dpr line 169 add this code:

if (FileName = '') then FileName := 'index.html';

smile

Last edited by mpv (2013-10-11 19:12:42)

Offline

#33 2013-10-13 20:28:57

warleyalex
Member
From: Sete Lagoas-MG, Brasil
Registered: 2013-01-20
Posts: 250

Re: Thread-safety of mORMot

mpv wrote:

in JSHttpApiServer.dpr line 169 add this code:

if (FileName = '') then FileName := 'index.html';

smile

Thank you for replying.

Last edited by warleyalex (2013-10-16 20:00:55)

Offline

#34 2013-11-27 12:15:24

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 337

Re: Thread-safety of mORMot

Where I can found mozjs64.dll ? In Delphi XE3 on Windows 7 Home Premium 64 bits, sample 22 raise an access violation:
First chance exception at $5A0DA137. Exception class $C0000005 with message 'access violation at 0x5a0da137: read of address 0x00004a78'. Process JSHttpApiServer.exe (3680).
Works with Delphi 7 in same machine.

Thanks.


Esteban

Offline

#35 2013-11-28 20:43:04

mpv
Member
From: Ukraine
Registered: 2012-03-24
Posts: 1,571
Website

Re: Thread-safety of mORMot

Unfortunately SM support not tested under x64. Current SynSM unit export SpiderMonkey 17 h files, so you can try  to compile mozjs for x64 from sources http://ftp.mozilla.org/pub/mozilla.org/ … 0.0.tar.gz using this instruction https://developer.mozilla.org/en-US/doc … umentation. But this is huge work.....

Last edited by mpv (2013-11-28 20:44:06)

Offline

#36 2013-11-28 21:45:47

EMartin
Member
From: Buenos Aires - Argentina
Registered: 2013-01-09
Posts: 337

Re: Thread-safety of mORMot

Thank you very much !!!


Esteban

Offline

Board footer

Powered by FluxBB