Created
March 19, 2019 13:15
-
-
Save valeriansaliou/3ef8315d7282bd173c2cb9eba64fa739 to your computer and use it in GitHub Desktop.
Sonic Benchmark: Batch QUERY
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require("mongoose"); | |
var SonicChannelSearch = require("sonic-channel").Search; | |
var MessageModel = mongoose.model("message", new mongoose.Schema({ | |
website_id : String, | |
type : String, | |
content : Object | |
})); | |
var query_count = 0; | |
var last_count = 0; | |
var last_time_start = 0; | |
var worst_time = -1; | |
var best_time = -1; | |
var query_next = function(sonicChannelSearch, cursor) { | |
cursor.next() | |
.then((message) => { | |
// Done? | |
if (message === null) { | |
console.info("All done. Bye."); | |
process.exit(0); | |
return; | |
} | |
var content = ( | |
(message.content || "").trim().split(" ").slice(0, 5).join(" ") | |
); | |
if (content) { | |
var commited = sonicChannelSearch.query( | |
"messages", message.website_id, content, | |
(results, error) => { | |
if (error) { | |
console.error("Search error", error); | |
} | |
if (last_count++ === 1000) { | |
last_count = 1; | |
var date_now = Date.now(); | |
var time_taken = ((date_now - last_time_start) / 1000); | |
if (worst_time === -1 || time_taken > worst_time) { | |
worst_time = time_taken; | |
} | |
if (best_time === -1 || time_taken < best_time) { | |
best_time = time_taken; | |
} | |
console.debug( | |
"Step: cursor #" + query_count + "; " + | |
"took " + time_taken + " sec (worst " + worst_time + " sec;" + | |
" best " + best_time + " sec)" | |
); | |
last_time_start = date_now; | |
} | |
query_count++; | |
query_next(sonicChannelSearch, cursor); | |
} | |
); | |
if (commited !== true) { | |
console.warn("Not commited yet: #" + last_count); | |
} | |
} else { | |
query_next(sonicChannelSearch, cursor); | |
} | |
}) | |
.catch((error) => { | |
console.error("Cursor error", error); | |
process.exit(1); | |
}); | |
}; | |
mongoose.connect("mongodb://localhost/messages", { | |
useNewUrlParser : true | |
}) | |
.then(() => { | |
console.info("Mongoose connected."); | |
var sonicChannelSearch = new SonicChannelSearch({ | |
host : "::1", | |
port : 1491, | |
auth : "SecretPassword" | |
}).connect({ | |
connected : function() { | |
console.info("Sonic Channel succeeded to connect to host."); | |
var cursor = MessageModel.find( | |
{ | |
type : "text" | |
}, | |
{ | |
website_id : true, | |
content : true | |
} | |
) | |
.cursor(); | |
last_time_start = Date.now(); | |
query_next(sonicChannelSearch, cursor); | |
}, | |
disconnected : function() { | |
console.error("Sonic Channel is now disconnected."); | |
process.exit(1); | |
}, | |
timeout : function() { | |
console.error("Sonic Channel connection timed out."); | |
}, | |
retrying : function() { | |
console.error("Trying to reconnect to Sonic Channel..."); | |
}, | |
error : function(error) { | |
console.error("Sonic Channel failed to connect to host.", error); | |
} | |
}); | |
}) | |
.catch((error) => { | |
console.error("Mongoose connect failed.", error); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment