Commit 0f48d413 authored by 赵威's avatar 赵威

add timer

parent ee69f945
...@@ -22,27 +22,29 @@ def match_tractate_by_device(device_id, n=10): ...@@ -22,27 +22,29 @@ def match_tractate_by_device(device_id, n=10):
try: try:
if device_id not in ["0", "unknown", "87654", ""]: if device_id not in ["0", "unknown", "87654", ""]:
time_begin = time.time() time_begin = time.time()
portrait_time_begin = time.time()
business_tags = get_user_portrait_tag3_business_tags(device_id, tags_num=3) business_tags = get_user_portrait_tag3_business_tags(device_id, tags_num=3)
portrait_time_end = time.time() - portrait_time_begin
res = [] res = []
if business_tags: if business_tags:
vectors_time_begin = time.time()
vectors = [] vectors = []
for tag in business_tags: for tag in business_tags:
lst = json.loads(TAG_EMBEDDING_DICT.get(bytes(tag, "utf-8"), b"[]")) lst = json.loads(TAG_EMBEDDING_DICT.get(bytes(tag, "utf-8"), b"[]"))
if lst: if lst:
vectors.append(np.array(lst).astype("float32")) vectors.append(np.array(lst).astype("float32"))
vectors_time_end = time.time() - vectors_time_begin
search_time_begin = time.time()
if vectors: if vectors:
D, I = FAISS_TAGS_INDEX.search(np.array([np.average(vectors, axis=0)]).astype("float32"), n) average_time_begin = time.time()
average_vectors = np.array([np.average(vectors, axis=0)]).astype("float32")
average_time_end = time.time() - average_time_begin
search_time_begin = time.time()
D, I = FAISS_TAGS_INDEX.search(average_vectors, n)
search_time_end = time.time() - search_time_begin
to_list_time_begin = time.time()
distances = D.tolist()[0] distances = D.tolist()[0]
ids = I.tolist()[0] ids = I.tolist()[0]
to_list_time_end = time.time() - to_list_time_begin
for (index, i) in enumerate(distances): for (index, i) in enumerate(distances):
if i <= 5.0: if i <= 5.0:
res.append(ids[index]) res.append(ids[index])
search_time_end = time.time() - search_time_begin
time_end = time.time() - time_begin time_end = time.time() - time_begin
if time_end > 0.04: if time_end > 0.04:
timer_dict = { timer_dict = {
...@@ -50,8 +52,8 @@ def match_tractate_by_device(device_id, n=10): ...@@ -50,8 +52,8 @@ def match_tractate_by_device(device_id, n=10):
"api": "strategy_embedding/personas_vector/match", "api": "strategy_embedding/personas_vector/match",
"device_id": device_id, "device_id": device_id,
"n": n, "n": n,
"get_business_tags": "{:.3f}ms".format(portrait_time_end * 1000), "average_time": "{:.3f}ms".format(average_time_end * 1000),
"get_vectors": "{:.3f}ms".format(vectors_time_end * 1000), "to_list_time": "{:.3f}ms".format(to_list_time_end * 1000),
"search": "{:.3f}ms".format(search_time_end * 1000), "search": "{:.3f}ms".format(search_time_end * 1000),
"total_time": "{:.3f}ms".format(time_end * 1000) "total_time": "{:.3f}ms".format(time_end * 1000)
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment