forked from pentilm/FactAI
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resutils.py
63 lines (54 loc) · 2.05 KB
/
resutils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
from resource import *
import json
import time
import requests
import os
import subprocess
import sys
import grpc
import service.service_spec.telemetry_pb2 as telemetry_pb2
import service.service_spec.telemetry_pb2_grpc as telemetry_pb2_grpc
class resutils():
def __init__(self):
self.device_name="factai"
def memory_usage(self):
to_MB = 1024.
to_MB *= to_MB
return getrusage(RUSAGE_SELF).ru_maxrss / to_MB
def cpu_ticks(self):
sec= time.clock()
tick=sec*(10**7)
mtick=tick/10**6
return mtick
def block_in(self):
to_KB = 1024.
return getrusage(RUSAGE_SELF).ru_inblock/to_KB
def get_address(self):
#get deployment type to identify the static port
deployment_type=os.environ['deployment_type']
service_address="localhost"
if deployment_type=="prod":
service_port=60777
else:
service_port=60778
return str(service_address),str(service_port)
def call_telemetry(self,stance_pred,cpu_used,memory_used,net_used,time_taken,call_id,tracer_info):
req_address=self.get_address()
address=req_address[0]
port=req_address[1]
huggingface_adapter_address=address+":"+port
span_id = tracer_info.span_id
trace_id = tracer_info.trace_id
trace_info = str({
'trace_id': trace_id,
'span_id': span_id
})
channel = grpc.insecure_channel("{}".format(huggingface_adapter_address))
stub = telemetry_pb2_grpc.HuggingfaceAdapterStub(channel)
deployment_type=os.environ['deployment_type']
if deployment_type=="prod":
service_name="factai"
else:
service_name="testing-factai"
result=stub.telemetry(telemetry_pb2.TelemetryInput(result=stance_pred,cpu_used=cpu_used,memory_used=memory_used,net_used=net_used,time_taken=time_taken,device_name=self.device_name,call_id=call_id,service_name=service_name,tracer_info=trace_info))
return str(result.response)