[Freeswitch-dev] luasql or freeswitch.dbh
Hi, from performance point of view, what should be used to connect to many different databases, luasql or freeswitch.dbh which does use ODBC? The problem is, that is not only one database but 3-5 databases which are on 2-3 different hosts. Lua will be used to fetch the directory and dialplan directly from the db. Also live-data will be stored on the different databases. The advantage of freeswitch.dbh would be, that the db connections will be re-used. LUAsql would use the native e.g. postgres or mysql driver. Thanks for any hint. Best Regards, Bernhard
Hi Bernhard, I tried out several ways for connecting to a database from inside FreeSWITCH myself before writing the freeswitch.Dbh (which is rather simple actually - it only exposes already existing FreeSWITCH functionality in mod_lua). My findings were that caching the connection as is provided by FreeSWITCH gives a huge speed increase compared to re-connecting all the time as must be done with LuaSQL. Perhaps there are ways to have a connection pool in ODBC but I didn't investigate that. Here's a test with LuaSQL with ODBC driver: require "luasql.odbc"; local env = assert(luasql.odbc()) local con = assert(env:connect("dsn","user","pass")) local cur = assert(con:execute ("select 1 as foo, 2 as bar")) local row = {} while (cur:fetch(row, "a")) do for key, val in pairs(row) do stream:write(string.format("%25s : %s\n", key, val)) end stream:write("\n") end cur:close() con:close() env:close() freeswitch@fs-dev> hammer 1000 lua test_luasql_odbc.lua +OK Finished in 38952 ms (min 29 ms, max 107 ms) The same test with freeswitch.Dbh: local dbh = assert(freeswitch.Dbh("dsn","user","pass")) assert(dbh:query("select 1 as foo, 2 as bar", function(row) for key, val in pairs(row) do stream:write(string.format("%25s : %s\n", key, val)) end stream:write("\n") end)) assert(dbh:release()) freeswitch@fs-dev> hammer 1000 lua test_lua_dbh.lua +OK Finished in 2723 ms (min 1 ms, max 25 ms) As you can see, with these tests the freeswitch.Dbh is about 14 times as fast as luasql.odbc. Of course this test is single threaded (the scripts are executed serially), so you'd get different results in real- world, and if you have 'long' running scripts with many queries on the same handle, the results would even out a bit. (btw mod_hammer is a simple app in contrib/ledr/c that executes another app n times and shows how long it took) I just tried compiling LuaSQL with Postgres driver to do the same tests, but that borked. Maybe you can give results of that ? I am curious how it behaves. About having a native driver or odbc: odbc is just an abstraction layer which uses the native drivers itself, so I'd reckon it should be almost as fast - correct me if I'm wrong ? I wrote some more about these tests on my blog at http://www.toyos.nl Kind regards, Leon On Sep 16, 2010, at 8:52 PM, Bernhard Suttner wrote:
Hi,
from performance point of view, what should be used to connect to many different databases, luasql or freeswitch.dbh which does use ODBC?
The problem is, that is not only one database but 3-5 databases which are on 2-3 different hosts. Lua will be used to fetch the directory and dialplan directly from the db. Also live-data will be stored on the different databases. The advantage of freeswitch.dbh would be, that the db connections will be re-used. LUAsql would use the native e.g. postgres or mysql driver.
Thanks for any hint.
Best Regards, Bernhard
_______________________________________________ FreeSWITCH-dev mailing list FreeSWITCH-dev@lists.freeswitch.org http://lists.freeswitch.org/mailman/listinfo/freeswitch-dev UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-dev http://www.freeswitch.org
Hi, thanks a lot for these details, but I am not sure how thefreeswitch.dbh interface behaves, if there are many threads which will try touse the connection. Does it then build up more connections or does it blocktill the connection will then be released from the other thread? Currently its not possible to me to create the luasql.postges testto access the database with the native driver but I am sure, that thepostgres driver should be much faster than the ODBC. Could someone test that? Best regards, Bernhard Von: freeswitch-dev-bounces@lists.freeswitch.org[mailto:freeswitch-dev-bounces@lists.freeswitch.org] ImAuftrag von Leon de Rooij Gesendet: Dienstag, 28. September 2010 16:11 An: freeswitch-dev@lists.freeswitch.org Betreff: Re: [Freeswitch-dev] luasql or freeswitch.dbh Hi Bernhard, I tried out several ways for connecting to a database frominside FreeSWITCH myself before writing the freeswitch.Dbh (which is rathersimple actually - it only exposes already existing FreeSWITCH functionality inmod_lua). My findings were that caching the connection as is providedby FreeSWITCH gives a huge speed increase compared to re-connecting all thetime as must be done with LuaSQL. Perhaps there are ways to have a connectionpool in ODBC but I didn't investigate that. Here's a test with LuaSQL with ODBC driver: require "luasql.odbc"; local env = assert(luasql.odbc())local con = assert(env:connect("dsn","user","pass"))local cur = assert(con:execute ("select 1 as foo, 2 as bar")) local row = {}while (cur:fetch(row, "a")) do for key, val in pairs(row) do stream:write(string.format("%25s : %s\n", key, val)) end stream:write("\n")end cur:close()con:close()env:close() freeswitch@fs-dev> hammer 1000 lua test_luasql_odbc.lua+OK Finished in 38952 ms (min 29 ms, max 107 ms) The same test with freeswitch.Dbh: local dbh = assert(freeswitch.Dbh("dsn","user","pass")) assert(dbh:query("select 1 as foo, 2 as bar", function(row) for key, val in pairs(row) do stream:write(string.format("%25s : %s\n", key, val)) end stream:write("\n")end)) assert(dbh:release()) freeswitch@fs-dev> hammer 1000 lua test_lua_dbh.lua+OK Finished in 2723 ms (min 1 ms, max 25 ms) As you can see, with these tests the freeswitch.Dbh is about14 times as fast as luasql.odbc. Of course this test is single threaded (thescripts are executed serially), so you'd get different results in real-world,and if you have 'long' running scripts with many queries on the same handle,the results would even out a bit. (btw mod_hammer is a simple app in contrib/ledr/c thatexecutes another app n times and shows how long it took) I just tried compiling LuaSQL with Postgres driver to do thesame tests, but that borked. Maybe you can give results of that ? I am curioushow it behaves. About having a native driver or odbc: odbc is just anabstraction layer which uses the native drivers itself, so I'd reckon it shouldbe almost as fast - correct me if I'm wrong ? I wrote some more about these tests on my blog at http://www.toyos.nl Kind regards, Leon On Sep 16, 2010, at 8:52 PM, Bernhard Suttner wrote: Hi, from performance point of view, what should be used to connect to manydifferent databases, luasql or freeswitch.dbh which does use ODBC? The problem is, that is not only one database but 3-5 databases which are on2-3 different hosts. Lua will be used to fetch the directory and dialplandirectly from the db. Also live-data will be stored on the different databases.The advantage of freeswitch.dbh would be, that the db connections will bere-used. LUAsql would use the native e.g. postgres or mysql driver. Thanks for any hint. Best Regards, Bernhard _______________________________________________ FreeSWITCH-dev mailing list FreeSWITCH-dev@lists.freeswitch.org http://lists.freeswitch.org/mailman/listinfo/freeswitch-dev UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-dev http://www.freeswitch.org
On Sep 28, 2010, at 11:14 AM, Bernhard Suttner wrote:
Currently its not possible to me to create the luasql.postges test to access the database with the native driver but I am sure, that the postgres driver should be much faster than the ODBC. Could someone test that?
Why does everyone seem to think this about ODBC? Is there any basis or just mass hysteria? Mike
Good evening, I got the LuaSQL postgres driver working and ran the same example: require "luasql.postgres"; local env= assert(luasql.postgres()) local con= assert(env:connect("db", "user","pass","172.31.0.5")) local cur= assert(con:execute ("select 1 as foo, 2 as bar")) local row= {} while (cur:fetch(row,"a")) do for key, valin pairs(row) do stream:write(string.format("%25s : %s\n", key, val)) end stream:write("\n") end cur:close() con:close() env:close() And did the same test: freeswitch@fs-dev> hammer 1000 lua test_luasql_postgres.lua +OK Finished in 40625 ms (min 30 ms, max 122 ms) So it's slowest of the three ! :-) - I didn't investigate any further about the cause of this. I ran it a couple of times with the same result, and it was done on the same machine. (for completeness sake: all tests were done on an Intel Q6600 with two KVM guests, one containing an Ubuntu 10.04/64 with Postgres 8.4 and one guest with CentOS 5.5/64 with FS Trunk) -- To answer your other question about whether it blocks: The FreeSWITCH cache db behaves in a way that it makes a new connection if there are no free handles in the pool. It will only block if your db server doesn't allow a new connection. In c, you should normally release your db handle manually, but Lua has garbage collection, that automatically triggers release of any db handles to the pool when they go out of scope (for example when your script terminates). If a free handle is available in the pool it will be re-used by another thread. If I understand it right, a free handle in the pool will be disconnected from the db server if it is not used for 120 seconds. Kind regards, Leon Op 9/28/2010 4:10 PM, Leon de Rooij schreef:
Hi Bernhard,
I tried out several ways for connecting to a database from inside FreeSWITCH myself before writing the freeswitch.Dbh (which is rather simple actually - it only exposes already existing FreeSWITCH functionality in mod_lua).
My findings were that caching the connection as is provided by FreeSWITCH gives a huge speed increase compared to re-connecting all the time as must be done with LuaSQL. Perhaps there are ways to have a connection pool in ODBC but I didn't investigate that.
Here's a test with LuaSQL with ODBC driver: require "luasql.odbc";
local env= assert(luasql.odbc()) local con= assert(env:connect("dsn","user","pass")) local cur= assert(con:execute ("select 1 as foo, 2 as bar"))
local row= {} while (cur:fetch(row,"a")) do for key, valin pairs(row) do stream:write(string.format("%25s : %s\n", key, val)) end stream:write("\n") end
cur:close() con:close() env:close() freeswitch@fs-dev> hammer 1000 lua test_luasql_odbc.lua +OK Finished in 38952 ms (min 29 ms, max 107 ms)
The same test with freeswitch.Dbh:
local dbh= assert(freeswitch.Dbh("dsn","user","pass"))
assert(dbh:query("select 1 as foo, 2 as bar",function(row) for key, valin pairs(row) do stream:write(string.format("%25s : %s\n", key, val)) end stream:write("\n") end))
assert(dbh:release())
freeswitch@fs-dev> hammer 1000 lua test_lua_dbh.lua +OK Finished in 2723 ms (min 1 ms, max 25 ms)
As you can see, with these tests the freeswitch.Dbh is about 14 times as fast as luasql.odbc. Of course this test is single threaded (the scripts are executed serially), so you'd get different results in real-world, and if you have 'long' running scripts with many queries on the same handle, the results would even out a bit.
(btw mod_hammer is a simple app in contrib/ledr/c that executes another app n times and shows how long it took)
I just tried compiling LuaSQL with Postgres driver to do the same tests, but that borked. Maybe you can give results of that ? I am curious how it behaves.
About having a native driver or odbc: odbc is just an abstraction layer which uses the native drivers itself, so I'd reckon it should be almost as fast - correct me if I'm wrong ?
I wrote some more about these tests on my blog at http://www.toyos.nl
Kind regards,
Leon
On Sep 16, 2010, at 8:52 PM, Bernhard Suttner wrote:
Hi,
from performance point of view, what should be used to connect to many different databases, luasql or freeswitch.dbh which does use ODBC?
The problem is, that is not only one database but 3-5 databases which are on 2-3 different hosts. Lua will be used to fetch the directory and dialplan directly from the db. Also live-data will be stored on the different databases. The advantage of freeswitch.dbh would be, that the db connections will be re-used. LUAsql would use the native e.g. postgres or mysql driver.
Thanks for any hint.
Best Regards, Bernhard
_______________________________________________ FreeSWITCH-dev mailing list FreeSWITCH-dev@lists.freeswitch.org <mailto:FreeSWITCH-dev@lists.freeswitch.org> http://lists.freeswitch.org/mailman/listinfo/freeswitch-dev UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-dev http://www.freeswitch.org
_______________________________________________ FreeSWITCH-dev mailing list FreeSWITCH-dev@lists.freeswitch.org http://lists.freeswitch.org/mailman/listinfo/freeswitch-dev UNSUBSCRIBE:http://lists.freeswitch.org/mailman/options/freeswitch-dev http://www.freeswitch.org
participants (3)
-
Bernhard Suttner
-
Leon de Rooij
-
Michael Jerris