use JSON for message format, send server-times to client
authorMike Shaver <shaver@mozilla.org>
Sun, 31 Oct 2010 03:13:54 +0000
changeset 1 b02788734e3d3a7b7b4a815462b731f4529797ec
parent 0 590ab8c2a3ea87b37df7d6a2882f8b3f03126aaa
child 2 4dc435d1e3393f0687a7b094e8c47371d3aae65e
push id2
push usershaver@mozilla.com
push dateSun, 31 Oct 2010 03:15:39 +0000
use JSON for message format, send server-times to client
server/entry.html
server/websocket.js
--- a/server/entry.html
+++ b/server/entry.html
@@ -43,16 +43,19 @@ var conn;
   function log(data){
     $("#matchingbugs").html(data);
   };
 
   if (window["WebSocket"]) {
     conn = new WebSocket("ws://"+document.location.host+"/");
 
     conn.onmessage = function(evt) {
-      log(evt.data + " (" + (Date.now() - conn.sendTime) + " ms)");
+      var results = JSON.parse(evt.data);
+      log(Object.keys(results.bugs).join(" ") + " (" + 
+          (Date.now() - conn.sendTime) + "/" +
+          results.times.server + "/" + results.times.redis + " ms)");
     };
 
   }
 });
     </script>
   </body>
 </html>
--- a/server/websocket.js
+++ b/server/websocket.js
@@ -3,44 +3,48 @@ var util = require('util'),
     fs = require('fs'),
     path = require('path'),
     url = require('url'),
     ws = require('websocket-server');
 
 var indexer = require('../load/indexer');
 
 var redis = require('redis');
-
 var redisConn = redis.createClient();
 
 redisConn.on("error",  function (err) {
     console.log("Redis connection error to " + redisConn.host + ":" + redisConn.port + " - " + err);
 });
 
 function runSearch(message, conn)
 {
     var words = indexer.keywordsForSummary(message);
+    if (words.length == 0)
+	return;
+
     var startTime = Date.now();
     redisConn.sinter(words.map(function (v) { return "word:" + v;}),
 		     function (err, results) {
 			 if (err) {
 			     util.puts("ERR (redis): " + err);
 			     return;
 			 }
 			 
-			 var bugs = [];
+			 var resultmsg = { bugs: {} };
 			 if (results != null) {
 			     for (var i = 0; i < results.length; i++)
-				 bugs.push(results[i]);
+				 resultmsg.bugs[results[i]] = "";
 			 }
 
-			 conn.send(JSON.stringify(bugs));
 			 var now = Date.now();
-			 util.puts(conn.id + ": search " + words.length + " -> " + bugs.length + " in " +
-				   (now- startTime) + "/" + (now - conn.startTime) + " ms");
+			 resultmsg.times = { redis: now - startTime,
+					     server: now - conn.startTime };
+			 conn.send(JSON.stringify(resultmsg));
+			 util.puts(conn.id + ": search " + words.length + " -> " + results.length + " in " +
+				   resultmsg.times.redis+ "/" + resultmsg.times.server + " ms");
 		     });
 }
 
 var httpServer = http.createServer(function (req, res) {
     function send404() { res.writeHead(404); res.end(); }
 
     if (req.method != "GET")
 	return send404();