From 1d870e2e4232ce442703b9c8b64ccd2ee86103ff Mon Sep 17 00:00:00 2001 From: Kevin Lynx Date: Wed, 31 Jul 2013 22:06:18 +0800 Subject: [PATCH] add sphinx search stats --- src/http_front/http_handler.erl | 9 ++++++--- src/http_front/sphinx_search.erl | 11 ++++++++--- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/http_front/http_handler.erl b/src/http_front/http_handler.erl index de51061..f5ceab3 100644 --- a/src/http_front/http_handler.erl +++ b/src/http_front/http_handler.erl @@ -116,11 +116,14 @@ do_search(Keyword) -> Tip ++ Body. do_search_sphinx(Keyword, Page) -> - Rets = db_frontend:search_by_sphinx(Keyword, Page, ?COUNT_PER_PAGE + 1), + {Stats, Rets} = db_frontend:search_by_sphinx(Keyword, Page, ?COUNT_PER_PAGE + 1), + {SphinxTime, DBTime} = Stats, ThisPage = lists:sublist(Rets, ?COUNT_PER_PAGE), + StatsDesc = ?TEXT("

search ~s, sphinx ~f ms, db ~f ms

", + [Keyword, SphinxTime / 1000, DBTime / 1000]), BodyList = format_search_result(ThisPage), Body = ?TEXT("
    ~s
", [lists:flatten(BodyList)]), - Body ++ append_page_nav(Keyword, Page, Rets). + StatsDesc ++ Body ++ append_page_nav(Keyword, Page, Rets). append_page_nav(Key, Page, ThisRet) -> Nav = case length(ThisRet) of @@ -131,7 +134,7 @@ append_page_nav(Key, Page, ThisRet) -> Size when Page > 0 -> format_page_nav(Key, Page - 1, "Prev") ++ if Size > ?COUNT_PER_PAGE -> - "|" ++ format_page_nav(Key, Page + 1, "Next"); + " " ++ format_page_nav(Key, Page + 1, "Next"); true -> [] end; Size -> diff --git a/src/http_front/sphinx_search.erl b/src/http_front/sphinx_search.erl index ea8ae5f..9d9a41b 100644 --- a/src/http_front/sphinx_search.erl +++ b/src/http_front/sphinx_search.erl @@ -14,13 +14,18 @@ search(Conn, Key, Offset, Count) -> Q2 = giza_query:port(Q1, ?PORT), Q3 = giza_query:offset(Q2, Offset), Q4 = giza_query:limit(Q3, Count), - case catch giza_request:send(Q4) of + T1 = now(), + {T2, TDocs} = case catch giza_request:send(Q4) of {'EXIT', R} -> ?W(?FMT("sphinx search error ~p", [R])), []; {ok, Ret} -> - decode_search_ret(Conn, Ret) - end. + T = now(), + {T, decode_search_ret(Conn, Ret)} + end, + T3 = now(), + Stats = {timer:now_diff(T2, T1), timer:now_diff(T3, T2)}, + {Stats, TDocs}. decode_search_ret(Conn, Ret) -> Hashes = [translate_hash(Item) || Item <- Ret],