Permalink
Browse files

tally and report search performance

  • Loading branch information...
1 parent 4b4908a commit 7f95a24cc8935910c990caa8a812eb1c73b72fad @WardCunningham committed Sep 10, 2012
Showing with 107 additions and 46 deletions.
  1. +33 −12 client/client.js
  2. +19 −6 client/lib/neighborhood.coffee
  3. +8 −2 client/lib/search.coffee
  4. +7 −7 client/test/neighborhood.coffee
  5. +40 −19 client/test/testclient.js
View
45 client/client.js
@@ -1663,28 +1663,47 @@ require.define("/lib/neighborhood.coffee", function (require, module, exports, _
};
neighborhood.search = function(searchQuery) {
- var match, matches, matchingPages, neighborInfo, neighborSite, sitemap, _ref2;
- matches = [];
- match = function(text) {
- return (text != null) && text.toLowerCase().indexOf(searchQuery.toLowerCase()) >= 0;
+ var finds, match, matchingPages, neighborInfo, neighborSite, sitemap, start, tally, tick, _ref2;
+ finds = [];
+ tally = {};
+ tick = function(key) {
+ if (tally[key] != null) {
+ return tally[key]++;
+ } else {
+ return tally[key] = 1;
+ }
+ };
+ match = function(key, text) {
+ var hit;
+ hit = (text != null) && text.toLowerCase().indexOf(searchQuery.toLowerCase()) >= 0;
+ if (hit) tick(key);
+ return hit;
};
+ start = Date.now();
_ref2 = wiki.neighborhood;
for (neighborSite in _ref2) {
if (!__hasProp.call(_ref2, neighborSite)) continue;
neighborInfo = _ref2[neighborSite];
sitemap = neighborInfo.sitemap;
+ if (sitemap != null) tick('sites');
matchingPages = _.each(sitemap, function(page) {
- if (!(match(page.title) || match(page.synopsis) || match(page.slug))) {
+ tick('pages');
+ if (!(match('title', page.title) || match('text', page.synopsis) || match('slug', page.slug))) {
return;
}
- return matches.push({
+ tick('finds');
+ return finds.push({
page: page,
site: neighborSite,
rank: 1
});
});
}
- return matches;
+ tally['msec'] = Date.now() - start;
+ return {
+ finds: finds,
+ tally: tally
+ };
};
$(function() {
@@ -1728,18 +1747,20 @@ require.define("/lib/search.coffee", function (require, module, exports, __dirna
var neighborhood, performSearch;
neighborhood = _arg.neighborhood;
performSearch = function(searchQuery) {
- var $searchResultPage, explanatoryPara, result, searchResultPageData, searchResultReferences, searchResults;
+ var $searchResultPage, explanatoryPara, result, searchResultPageData, searchResultReferences, searchResults, tally;
searchResults = neighborhood.search(searchQuery);
+ tally = searchResults.tally;
explanatoryPara = {
type: 'paragraph',
id: util.randomBytes(8),
- text: "These are the search results for '" + searchQuery + "'."
+ text: "String '" + searchQuery + "' found on " + (tally.finds || 'none') + " of " + (tally.pages || 'no') + " pages from " + (tally.sites || 'no') + " sites.\nText matched on " + (tally.title || 'no') + " titles, " + (tally.text || 'no') + " paragraphs, and " + (tally.slug || 'no') + " slugs.\nElapsed time " + tally.msec + " milliseconds."
};
searchResultReferences = (function() {
- var _i, _len, _results;
+ var _i, _len, _ref, _results;
+ _ref = searchResults.finds;
_results = [];
- for (_i = 0, _len = searchResults.length; _i < _len; _i++) {
- result = searchResults[_i];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ result = _ref[_i];
_results.push({
"type": "reference",
"id": util.randomBytes(8),
View
25 client/lib/neighborhood.coffee
@@ -54,18 +54,31 @@ neighborhood.listNeighbors = ()->
_.keys( wiki.neighborhood )
neighborhood.search = (searchQuery)->
- matches = []
- match = (text) ->
- text? and text.toLowerCase().indexOf( searchQuery.toLowerCase() ) >= 0
+ finds = []
+ tally = {}
+
+ tick = (key) ->
+ if tally[key]? then tally[key]++ else tally[key] = 1
+
+ match = (key, text) ->
+ hit = text? and text.toLowerCase().indexOf( searchQuery.toLowerCase() ) >= 0
+ tick key if hit
+ hit
+
+ start = Date.now()
for own neighborSite,neighborInfo of wiki.neighborhood
sitemap = neighborInfo.sitemap
+ tick 'sites' if sitemap?
matchingPages = _.each sitemap, (page)->
- return unless match(page.title) or match(page.synopsis) or match(page.slug)
- matches.push
+ tick 'pages'
+ return unless match('title', page.title) or match('text', page.synopsis) or match('slug', page.slug)
+ tick 'finds'
+ finds.push
page: page,
site: neighborSite,
rank: 1 # HARDCODED FOR NOW
- matches
+ tally['msec'] = Date.now() - start
+ { finds, tally }
$ ->
View
10 client/lib/search.coffee
@@ -5,13 +5,19 @@ require('./dom')
createSearch = ({neighborhood})->
performSearch = (searchQuery)->
searchResults = neighborhood.search(searchQuery)
+ tally = searchResults.tally
+
explanatoryPara = {
type: 'paragraph'
id: util.randomBytes(8)
- text: "These are the search results for '#{searchQuery}'."
+ text: """
+ String '#{searchQuery}' found on #{tally.finds||'none'} of #{tally.pages||'no'} pages from #{tally.sites||'no'} sites.
+ Text matched on #{tally.title||'no'} titles, #{tally.text||'no'} paragraphs, and #{tally.slug||'no'} slugs.
+ Elapsed time #{tally.msec} milliseconds.
+ """
}
- searchResultReferences = for result in searchResults
+ searchResultReferences = for result in searchResults.finds
{
"type": "reference"
"id": util.randomBytes(8)
View
14 client/test/neighborhood.coffee
@@ -5,7 +5,7 @@ describe 'neighborhood', ->
describe 'no neighbors', ->
it 'should return an empty array for our search', ->
searchResult = neighborhood.search( "query string" )
- expect(searchResult).to.eql( [] )
+ expect(searchResult.finds).to.eql( [] )
describe 'a single neighbor with a few pages', ->
@@ -25,11 +25,11 @@ describe 'neighborhood', ->
it 'returns all pages that match the query', ->
searchResult = neighborhood.search( "Page" )
- expect( searchResult ).to.have.length(3)
+ expect( searchResult.finds ).to.have.length(3)
it 'returns only pages that match the query', ->
searchResult = neighborhood.search( "Page T" )
- expect( searchResult ).to.have.length(2)
+ expect( searchResult.finds ).to.have.length(2)
it 'should package the results in the correct format', ->
expectedResult = [
@@ -40,7 +40,7 @@ describe 'neighborhood', ->
}
]
searchResult = neighborhood.search( "Page Two" )
- expect( searchResult ).to.eql( expectedResult )
+ expect( searchResult.finds ).to.eql( expectedResult )
it 'searches both the slug and the title'
@@ -66,8 +66,8 @@ describe 'neighborhood', ->
it 'returns matching pages from every neighbor', ->
searchResult = neighborhood.search( "Page Two" )
- expect( searchResult ).to.have.length(2)
- sites = _.pluck( searchResult, 'site' )
+ expect( searchResult.finds ).to.have.length(2)
+ sites = _.pluck( searchResult.finds, 'site' )
expect( sites.sort() ).to.eql( ['site-one','site-two'].sort() )
@@ -78,6 +78,6 @@ describe 'neighborhood', ->
it 'gracefully ignores unpopulated neighbors', ->
searchResult = neighborhood.search( "some search query" )
- expect( searchResult ).to.be.empty()
+ expect( searchResult.finds ).to.be.empty()
it 'should re-populate the neighbor'
View
59 client/test/testclient.js
@@ -1615,28 +1615,47 @@ require.define("/lib/neighborhood.coffee", function (require, module, exports, _
};
neighborhood.search = function(searchQuery) {
- var match, matches, matchingPages, neighborInfo, neighborSite, sitemap, _ref2;
- matches = [];
- match = function(text) {
- return (text != null) && text.toLowerCase().indexOf(searchQuery.toLowerCase()) >= 0;
+ var finds, match, matchingPages, neighborInfo, neighborSite, sitemap, start, tally, tick, _ref2;
+ finds = [];
+ tally = {};
+ tick = function(key) {
+ if (tally[key] != null) {
+ return tally[key]++;
+ } else {
+ return tally[key] = 1;
+ }
+ };
+ match = function(key, text) {
+ var hit;
+ hit = (text != null) && text.toLowerCase().indexOf(searchQuery.toLowerCase()) >= 0;
+ if (hit) tick(key);
+ return hit;
};
+ start = Date.now();
_ref2 = wiki.neighborhood;
for (neighborSite in _ref2) {
if (!__hasProp.call(_ref2, neighborSite)) continue;
neighborInfo = _ref2[neighborSite];
sitemap = neighborInfo.sitemap;
+ if (sitemap != null) tick('sites');
matchingPages = _.each(sitemap, function(page) {
- if (!(match(page.title) || match(page.synopsis) || match(page.slug))) {
+ tick('pages');
+ if (!(match('title', page.title) || match('text', page.synopsis) || match('slug', page.slug))) {
return;
}
- return matches.push({
+ tick('finds');
+ return finds.push({
page: page,
site: neighborSite,
rank: 1
});
});
}
- return matches;
+ tally['msec'] = Date.now() - start;
+ return {
+ finds: finds,
+ tally: tally
+ };
};
$(function() {
@@ -1680,18 +1699,20 @@ require.define("/lib/search.coffee", function (require, module, exports, __dirna
var neighborhood, performSearch;
neighborhood = _arg.neighborhood;
performSearch = function(searchQuery) {
- var $searchResultPage, explanatoryPara, result, searchResultPageData, searchResultReferences, searchResults;
+ var $searchResultPage, explanatoryPara, result, searchResultPageData, searchResultReferences, searchResults, tally;
searchResults = neighborhood.search(searchQuery);
+ tally = searchResults.tally;
explanatoryPara = {
type: 'paragraph',
id: util.randomBytes(8),
- text: "These are the search results for '" + searchQuery + "'."
+ text: "String '" + searchQuery + "' found on " + (tally.finds || 'none') + " of " + (tally.pages || 'no') + " pages from " + (tally.sites || 'no') + " sites.\nText matched on " + (tally.title || 'no') + " titles, " + (tally.text || 'no') + " paragraphs, and " + (tally.slug || 'no') + " slugs.\nElapsed time " + tally.msec + " milliseconds."
};
searchResultReferences = (function() {
- var _i, _len, _results;
+ var _i, _len, _ref, _results;
+ _ref = searchResults.finds;
_results = [];
- for (_i = 0, _len = searchResults.length; _i < _len; _i++) {
- result = searchResults[_i];
+ for (_i = 0, _len = _ref.length; _i < _len; _i++) {
+ result = _ref[_i];
_results.push({
"type": "reference",
"id": util.randomBytes(8),
@@ -2043,7 +2064,7 @@ require.define("/test/neighborhood.coffee", function (require, module, exports,
return it('should return an empty array for our search', function() {
var searchResult;
searchResult = neighborhood.search("query string");
- return expect(searchResult).to.eql([]);
+ return expect(searchResult.finds).to.eql([]);
});
});
describe('a single neighbor with a few pages', function() {
@@ -2071,12 +2092,12 @@ require.define("/test/neighborhood.coffee", function (require, module, exports,
it('returns all pages that match the query', function() {
var searchResult;
searchResult = neighborhood.search("Page");
- return expect(searchResult).to.have.length(3);
+ return expect(searchResult.finds).to.have.length(3);
});
it('returns only pages that match the query', function() {
var searchResult;
searchResult = neighborhood.search("Page T");
- return expect(searchResult).to.have.length(2);
+ return expect(searchResult.finds).to.have.length(2);
});
it('should package the results in the correct format', function() {
var expectedResult, searchResult;
@@ -2092,7 +2113,7 @@ require.define("/test/neighborhood.coffee", function (require, module, exports,
}
];
searchResult = neighborhood.search("Page Two");
- return expect(searchResult).to.eql(expectedResult);
+ return expect(searchResult.finds).to.eql(expectedResult);
});
return it('searches both the slug and the title');
});
@@ -2125,8 +2146,8 @@ require.define("/test/neighborhood.coffee", function (require, module, exports,
return it('returns matching pages from every neighbor', function() {
var searchResult, sites;
searchResult = neighborhood.search("Page Two");
- expect(searchResult).to.have.length(2);
- sites = _.pluck(searchResult, 'site');
+ expect(searchResult.finds).to.have.length(2);
+ sites = _.pluck(searchResult.finds, 'site');
return expect(sites.sort()).to.eql(['site-one', 'site-two'].sort());
});
});
@@ -2138,7 +2159,7 @@ require.define("/test/neighborhood.coffee", function (require, module, exports,
it('gracefully ignores unpopulated neighbors', function() {
var searchResult;
searchResult = neighborhood.search("some search query");
- return expect(searchResult).to.be.empty();
+ return expect(searchResult.finds).to.be.empty();
});
return it('should re-populate the neighbor');
});

0 comments on commit 7f95a24

Please sign in to comment.