ringo-main.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. var sql = require('sql-ringojs-client');
  2. var mustache = require('ringo/mustache');
  3. // DO NOT TOUCH THE FOLLOWING LINE.
  4. // THIS VARIABLE IS REGEX REPLACED BY setup.py
  5. var dbHost = 'localhost';
  6. var mongodbUri = 'mongodb://localhost/hello_world';
  7. var sortFortunes = function(a, b) {
  8. return (a.message < b.message) ? -1 : (a.message > b.message) ? 1 : 0;
  9. };
  10. var fortuneTemplate = require('fs').read(module.resolve('./templates/fortune.mustache'));
  11. exports.app = function(req) {
  12. var path = req.pathInfo;
  13. if (path === '/json') {
  14. var helloObject = {message: "Hello, World!"};
  15. // JSON Response Test
  16. return {
  17. status: 200,
  18. headers: {"Content-Type": "application/json; charset=UTF-8"},
  19. body: [JSON.stringify(helloObject)]
  20. }
  21. } else if (path === '/db') {
  22. var queryCount = req.env.servletRequest.getParameter('queries');
  23. try {
  24. var connection = datasource.getConnection();
  25. if (queryCount === null) {
  26. var randId = ((Math.random() * 10000) | 0) + 1
  27. var world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
  28. return {
  29. status: 200,
  30. headers: {"Content-Type": "application/json; charset=UTF-8"},
  31. body: [JSON.stringify(world)]
  32. }
  33. } else {
  34. queryCount = parseInt(queryCount, 10);
  35. var body = [];
  36. var randId, world;
  37. for (var i = 0; i < queryCount; i++) {
  38. randId = ((Math.random() * 10000) | 0) + 1;
  39. world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
  40. body.push(world);
  41. }
  42. return {
  43. status: 200,
  44. headers: {"Content-Type": "application/json; charset=UTF-8"},
  45. body: [JSON.stringify(body)]
  46. }
  47. }
  48. } catch (e) {
  49. connection.close();
  50. connection = null;
  51. } finally {
  52. if (connection !== null) {
  53. connection.close();
  54. }
  55. }
  56. } else if (path === '/fortune') {
  57. try {
  58. var connection = datasource.getConnection();
  59. var fortunes = sql.query(connection, 'select * from Fortune');
  60. fortunes.push({
  61. id: 0,
  62. message: 'Additional fortune added at request time.'
  63. });
  64. fortunes.sort(sortFortunes);
  65. return {
  66. status: 200,
  67. headers: {"Content-Type": "text/html; charset=UTF-8"},
  68. body: [mustache.to_html(fortuneTemplate, {fortunes: fortunes})]
  69. }
  70. } catch (e) {
  71. connection.close();
  72. connection = null;
  73. } finally {
  74. if (connection !== null) {
  75. connection.close();
  76. }
  77. }
  78. } else if (path === '/plaintext') {
  79. return {
  80. status: 200,
  81. headers: {"Content-Type": 'text/plain'},
  82. body: ['Hello, World!']
  83. };
  84. } else if (path === '/updates') {
  85. var queryCount = parseInt(req.env.servletRequest.getParameter('queries'), 10);
  86. if (isNaN(queryCount) || queryCount < 1) {
  87. queryCount = 1;
  88. } else if (queryCount > 500) {
  89. queryCount = 500;
  90. }
  91. try {
  92. var connection = datasource.getConnection();
  93. var body = [];
  94. for (var i = 0; i < queryCount; i++) {
  95. let randId = ((Math.random() * 10000) | 0) + 1;
  96. world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
  97. world.randomNumber = ((Math.random() * 10000) | 0) + 1;
  98. sql.execute(connection, 'UPDATE World SET randomNumber = ' + world.randomNumber + ' WHERE id = ' + world.id);
  99. body.push(world);
  100. }
  101. } catch (e) {
  102. connection.close();
  103. connection = null;
  104. } finally {
  105. if (connection !== null) {
  106. connection.close();
  107. }
  108. }
  109. return {
  110. status: 200,
  111. headers: {"Content-Type": "application/json; charset=UTF-8"},
  112. body: [JSON.stringify(body)]
  113. }
  114. }
  115. };
  116. var datasource = module.singleton('pooling-datasource', function() {
  117. var 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";
  118. return sql.connect("jdbc:mysql://" + dbHost + "/hello_world" + mysqlConnectionProperties, 'benchmarkdbuser', 'benchmarkdbpass');
  119. });
  120. if (require.main == module) {
  121. require("ringo/httpserver").main(module.id);
  122. }