Skip to content

Instantly share code, notes, and snippets.

@elevine
Created February 18, 2016 20:52
Show Gist options
  • Select an option

  • Save elevine/5da9a4096f1893ef7c80 to your computer and use it in GitHub Desktop.

Select an option

Save elevine/5da9a4096f1893ef7c80 to your computer and use it in GitHub Desktop.
Script to try and record the time it takes for documents to be synced by the Sync Gateway
var WebSocket = require('ws');
var ws = new WebSocket('ws://<server-address>:4984/db/_changes?feed=websocket');
var messageCount = 0;
var totalTime = 0;
var wsOptions = {
include_docs : true,
channels : "test",
filter: "sync_gateway/bychannel",
since: 83
};
ws.on("open", function() {
console.log("Open");
ws.send(JSON.stringify(wsOptions));
});
ws.on("message", function(message) {
console.log(message);
var now = Date.now();
var docArray;
try{
docArray = JSON.parse(message);
}
catch(err){
return;
}
if(docArray.length > 0){
if(! docArray[0].doc && ! docArray[0].doc.timestamp) return;
var messageTimestamp = docArray[0].doc.timestamp;
var then = Date.parse(messageTimestamp);
var ellapsedTime = now - then;
messageCount++;
totalTime += ellapsedTime;
console.log("Ellapsed time: " + ellapsedTime);
console.log("Average time: " + totalTime/messageCount);
}
//console.log(data);
// flags.binary will be set if a binary data is received.
// flags.masked will be set if the data was masked.
});
ws.on("text ", function(){
console.log("on text");
});
ws.on('close', function close() {
console.log('disconnected');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment