ringo-main.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. try {
  17. connection = datasource.getConnection();
  18. let randId, world;
  19. randId = ((Math.random() * 10000) | 0) + 1;
  20. world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
  21. return {
  22. status: 200,
  23. headers: { "Content-Type": "application/json" },
  24. body: [JSON.stringify(world)]
  25. }
  26. } catch (e) {
  27. connection.close();
  28. connection = null;
  29. } finally {
  30. if (connection !== null) {
  31. connection.close();
  32. }
  33. }
  34. }
  35. if (path === '/dbquery') {
  36. let queryCount = req.env.servletRequest.getParameter('queries');
  37. try {
  38. connection = datasource.getConnection();
  39. let randId, world;
  40. if (!queryCount || isNaN(queryCount) || queryCount < 1) {
  41. queryCount = 1;
  42. } else {
  43. queryCount = Math.min(Math.max(parseInt(queryCount, 10) || 1, 1), 500);
  44. }
  45. body = [];
  46. for (let i = 0; i < queryCount; i++) {
  47. randId = ((Math.random() * 10000) | 0) + 1;
  48. world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
  49. body.push(world);
  50. }
  51. return {
  52. status: 200,
  53. headers: { "Content-Type": "application/json" },
  54. body: [JSON.stringify(body)]
  55. }
  56. } catch (e) {
  57. connection.close();
  58. connection = null;
  59. } finally {
  60. if (connection !== null) {
  61. connection.close();
  62. }
  63. }
  64. }
  65. if (path === '/plaintext') {
  66. return {
  67. status: 200,
  68. headers: { "Content-Type": 'text/plain' },
  69. body: ['Hello, World!']
  70. };
  71. }
  72. if (path === '/updates') {
  73. let queryCount = parseInt(req.env.servletRequest.getParameter('queries'), 10);
  74. if (isNaN(queryCount) || queryCount < 1) {
  75. queryCount = 1;
  76. } else if (queryCount > 500) {
  77. queryCount = 500;
  78. }
  79. try {
  80. connection = datasource.getConnection();
  81. body = [];
  82. for (let i = 0; i < queryCount; i++) {
  83. let randId = ((Math.random() * 10000) | 0) + 1;
  84. world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
  85. world.randomNumber = ((Math.random() * 10000) | 0) + 1;
  86. sql.execute(connection, 'UPDATE World SET randomNumber = ' + world.randomNumber + ' WHERE id = ' + world.id);
  87. body.push(world);
  88. }
  89. } catch (e) {
  90. connection.close();
  91. connection = null;
  92. } finally {
  93. if (connection !== null) {
  94. connection.close();
  95. }
  96. }
  97. return {
  98. status: 200,
  99. headers: { "Content-Type": "application/json; charset=UTF-8" },
  100. body: [JSON.stringify(body)]
  101. }
  102. }
  103. };
  104. const datasource = module.singleton('pooling-datasource', function () {
  105. 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=true&enableQueryTimeouts=false&useUnbufferedIO=false&useReadAheadInput=false&maintainTimeStats=false&cacheRSMetadata=true&useSSL=false";
  106. return sql.connect("jdbc:mysql://tfb-database/hello_world" + mysqlConnectionProperties, 'benchmarkdbuser', 'benchmarkdbpass');
  107. });
  108. if (require.main == module) {
  109. require("ringo/httpserver").main(module.id);
  110. }