Browse Source

Update Ringojs query to pass the test (#4628)

Joan Miquel 6 years ago
parent
commit
8393403c26

+ 7 - 4
frameworks/JavaScript/ringojs/app/views.js

@@ -27,7 +27,13 @@ app.get('/json', function() {
    return response.json(helloObject);
    return response.json(helloObject);
 });
 });
 
 
-app.get('/db/:queries?', function(request, queries) {
+app.get('/db', function() {
+      let randId = ((Math.random() * 10000) | 0) + 1;
+      let world = models.store.query('select * from World where id = :id', {id: randId})[0];
+   return response.json(world);
+});
+
+app.get('/dbquery/:queries?', function(request, queries) {
    queries = formatQueries(queries);
    queries = formatQueries(queries);
    let worlds = [];
    let worlds = [];
    for (let i = 0; i < queries; i++) {
    for (let i = 0; i < queries; i++) {
@@ -35,9 +41,6 @@ app.get('/db/:queries?', function(request, queries) {
       let world = models.store.query('select * from World where id = :id', {id: randId})[0];
       let world = models.store.query('select * from World where id = :id', {id: randId})[0];
       worlds.push({"id": world.id, "randomNumber" : world.randomNumber});
       worlds.push({"id": world.id, "randomNumber" : world.randomNumber});
    }
    }
-   if (queries == 1) {
-      worlds = worlds[0];
-   }
    return response.json(worlds);
    return response.json(worlds);
 });
 });
 
 

+ 2 - 2
frameworks/JavaScript/ringojs/benchmark_config.json

@@ -4,7 +4,7 @@
     "default": {
     "default": {
       "json_url": "/json",
       "json_url": "/json",
       "db_url": "/db",
       "db_url": "/db",
-      "query_url": "/db?queries=",
+      "query_url": "/dbquery?queries=",
       "plaintext_url": "/plaintext",
       "plaintext_url": "/plaintext",
       "update_url": "/updates?queries=",
       "update_url": "/updates?queries=",
       "port": 8080,
       "port": 8080,
@@ -26,7 +26,7 @@
     "convenient": {
     "convenient": {
       "json_url": "/json",
       "json_url": "/json",
       "db_url": "/db",
       "db_url": "/db",
-      "query_url": "/db/",
+      "query_url": "/dbquery/",
       "fortune_url": "/fortune",
       "fortune_url": "/fortune",
       "plaintext_url": "/plaintext",
       "plaintext_url": "/plaintext",
       "update_url": "/updates/",
       "update_url": "/updates/",

+ 33 - 18
frameworks/JavaScript/ringojs/ringo-main.js

@@ -16,31 +16,46 @@ exports.app = function (req) {
   }
   }
 
 
   if (path === '/db') {
   if (path === '/db') {
+    try {
+      connection = datasource.getConnection();
+      let randId, world;
+      randId = ((Math.random() * 10000) | 0) + 1;
+      world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
+      return {
+        status: 200,
+        headers: { "Content-Type": "application/json" },
+        body: [JSON.stringify(world)]
+      }
+    } catch (e) {
+      connection.close();
+      connection = null;
+    } finally {
+      if (connection !== null) {
+        connection.close();
+      }
+    }
+  }
+
+  if (path === '/dbquery') {
     let queryCount = req.env.servletRequest.getParameter('queries');
     let queryCount = req.env.servletRequest.getParameter('queries');
     try {
     try {
       connection = datasource.getConnection();
       connection = datasource.getConnection();
       let randId, world;
       let randId, world;
       if (!queryCount || isNaN(queryCount) || queryCount < 1) {
       if (!queryCount || isNaN(queryCount) || queryCount < 1) {
+        queryCount = 1;
+      } else {
+        queryCount = Math.min(Math.max(parseInt(queryCount, 10) || 1, 1), 500);
+      }
+      body = [];
+      for (let i = 0; i < queryCount; i++) {
         randId = ((Math.random() * 10000) | 0) + 1;
         randId = ((Math.random() * 10000) | 0) + 1;
         world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
         world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
-        return {
-          status: 200,
-          headers: { "Content-Type": "application/json" },
-          body: [JSON.stringify(world)]
-        }
-      } else {
-        queryCount = parseInt(queryCount, 10);
-        body = [];
-        for (let i = 0; i < queryCount; i++) {
-          randId = ((Math.random() * 10000) | 0) + 1;
-          world = sql.query(connection, 'select * from World where World.id = ' + randId)[0];
-          body.push(world);
-        }
-        return {
-          status: 200,
-          headers: { "Content-Type": "application/json" },
-          body: [JSON.stringify(body)]
-        }
+        body.push(world);
+      }
+      return {
+        status: 200,
+        headers: { "Content-Type": "application/json" },
+        body: [JSON.stringify(body)]
       }
       }
     } catch (e) {
     } catch (e) {
       connection.close();
       connection.close();