Commit ee69f945 authored by 赵威's avatar 赵威

add timer

parent d5c8c8c0
...@@ -20,16 +20,21 @@ TAG_EMBEDDING_DICT = redis_client_db.hgetall("personas_tags_embedding") ...@@ -20,16 +20,21 @@ TAG_EMBEDDING_DICT = redis_client_db.hgetall("personas_tags_embedding")
@bind("strategy_embedding/personas_vector/match") @bind("strategy_embedding/personas_vector/match")
def match_tractate_by_device(device_id, n=10): 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) D, I = FAISS_TAGS_INDEX.search(np.array([np.average(vectors, axis=0)]).astype("float32"), n)
distances = D.tolist()[0] distances = D.tolist()[0]
...@@ -37,10 +42,26 @@ def match_tractate_by_device(device_id, n=10): ...@@ -37,10 +42,26 @@ def match_tractate_by_device(device_id, n=10):
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:
send_performance_msg_to_dingtalk("match_tractate_by_device {} n={} cost {:.3f}ms".format( timer_dict = {
device_id, n, time_end * 1000)) "method": "match_tractate_by_device",
"api": "strategy_embedding/personas_vector/match",
"device_id": device_id,
"n": n,
"get_business_tags": "{:.3f}ms".format(portrait_time_end * 1000),
"get_vectors": "{:.3f}ms".format(vectors_time_end * 1000),
"search": "{:.3f}ms".format(search_time_end * 1000),
"total_time": "{:.3f}ms".format(time_end * 1000)
}
msg_res = ""
for (k, v) in timer_dict.items():
msg_res += str(k)
msg_res += ": "
msg_res += str(v)
msg_res += "\n"
send_performance_msg_to_dingtalk(msg_res)
return res return res
return [] return []
except Exception as e: except Exception as e:
......
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