ringo-main.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. const sql = require('sql-ringojs-client');
  2. const fs = require('fs');
  3. exports.app = function (req) {
  4. const path = req.pathInfo;
  5. let connection, body;
  6. if (path === '/json') {
  7. const helloObject = { message: "Hello, World!" };
  8. // JSON Response Test
  9. return {
  10. status: 200,
  11. headers: { "Content-Type": "application/json" },
  12. body: [JSON.stringify(helloObject)]
  13. }
  14. }
  15. if (path === '/db') {
  16. let queryCount = req.env.servletRequest.getParameter('queries');
  17. try {
  18. connection = datasource.getConnection();
  19. let randId, world;
  20. if (!queryCount || isNaN(queryCount) || queryCount < 1) {
  21. randId = ((Math.random() * 10000) | 0) + 1;
  22. world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
  23. return {
  24. status: 200,
  25. headers: { "Content-Type": "application/json" },
  26. body: [JSON.stringify(world)]
  27. }
  28. } else {
  29. queryCount = parseInt(queryCount, 10);
  30. body = [];
  31. for (let i = 0; i < queryCount; i++) {
  32. randId = ((Math.random() * 10000) | 0) + 1;
  33. world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
  34. body.push(world);
  35. }
  36. return {
  37. status: 200,
  38. headers: { "Content-Type": "application/json" },
  39. body: [JSON.stringify(body)]
  40. }
  41. }
  42. } catch (e) {
  43. connection.close();
  44. connection = null;
  45. } finally {
  46. if (connection !== null) {
  47. connection.close();
  48. }
  49. }
  50. }
  51. if (path === '/plaintext') {
  52. return {
  53. status: 200,
  54. headers: { "Content-Type": 'text/plain' },
  55. body: ['Hello, World!']
  56. };
  57. }
  58. if (path === '/updates') {
  59. let queryCount = parseInt(req.env.servletRequest.getParameter('queries'), 10);
  60. if (isNaN(queryCount) || queryCount < 1) {
  61. queryCount = 1;
  62. } else if (queryCount > 500) {
  63. queryCount = 500;
  64. }
  65. try {
  66. connection = datasource.getConnection();
  67. body = [];
  68. for (let i = 0; i < queryCount; i++) {
  69. let randId = ((Math.random() * 10000) | 0) + 1;
  70. world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
  71. world.randomNumber = ((Math.random() * 10000) | 0) + 1;
  72. sql.execute(connection, 'UPDATE World SET randomNumber = ' + world.randomNumber + ' WHERE id = ' + world.id);
  73. body.push(world);
  74. }
  75. } catch (e) {
  76. connection.close();
  77. connection = null;
  78. } finally {
  79. if (connection !== null) {
  80. connection.close();
  81. }
  82. }
  83. return {
  84. status: 200,
  85. headers: { "Content-Type": "application/json; charset=UTF-8" },
  86. body: [JSON.stringify(body)]
  87. }
  88. }
  89. };
  90. const datasource = module.singleton('pooling-datasource', function () {
  91. const mysqlConnectionProperties = "?jdbcCompliantTruncation=false&elideSetAutoCommits=true&useLocalSessionState=true&cachePrepStmts=true&cacheCallableStmts=true&alwaysSendSetIsolation=false&prepStmtCacheSize=4096&cacheServerConfiguration=true&prepStmtCacheSqlLimit=2048&zeroDateTimeBehavior=convertToNull&traceProtocol=false&useServerPrepStmts&enableQueryTimeouts=false&useUnbufferedIO=false&useReadAheadInput=false&maintainTimeStats=false&cacheRSMetadata=true&useSSL=false";
  92. return sql.connect("jdbc:mysql://tfb-database/hello_world" + mysqlConnectionProperties, 'benchmarkdbuser', 'benchmarkdbpass');
  93. });
  94. if (require.main == module) {
  95. require("ringo/httpserver").main(module.id);
  96. }