Skip to content

Instantly share code, notes, and snippets.

@direvius
Created September 17, 2013 11:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save direvius/6593008 to your computer and use it in GitHub Desktop.
Save direvius/6593008 to your computer and use it in GitHub Desktop.
An example of a thrift shooter for yandex.tank
from Tank.Plugins.bfg import Sample
import logging
import time
import json
from MyService.Service import Service
import MyService.Service.ttypes as tt
from thrift.transport import TTransport
from thrift.transport import TSocket
from thrift.protocol import TBinaryProtocol
def shoot(self, missile, marker):
'''
Parse missile here, create a request based on it,
send it to a server, measure time and deal with
possible errors. Then fill in the Sample and
return it.
'''
logging.debug("Missile: %s\n%s", marker, missile)
socket = TSocket.TSocket('example.org', 9898)
transport = TTransport.TBufferedTransport(socket)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = Service.Client(protocol)
# парсим патрон -- он у нас в JSON
params = json.loads(missile)
par1 = map(lambda x: x.encode('utf-8'), params['par1'])
par2 = params['par2']
# создаем запрос
request = tt.TestRequest(par1, par2)
# засекаем время
start_ts = time.time()
transport.open()
connect_ts = time.time() # время соединения
response = client.ShowsDistribution(request)
answ_ts = time.time() # время ответа
transport.close()
# считаем метрики
rt = int((answ_ts - start_ts) * 1000)
latency = int((answ_ts - connect_ts) * 1000)
connect_time = int((connect_ts - start_ts) * 1000)
# пишем ответ в лог
logging.debug(response)
# Заполняем результат:
data_item = Sample(
marker, # маркер
0, # число активных потоков
rt, # время отклика (основная метрика)
0, # код ошибки прикладного уровня
0, # код ошибки сетевого уровня
0, # отправлено байт
0, # принято байт
connect_time, # время соединения
0, # время отправки
latency, # время от завершения отправки до начала приема
0, # время приема
0, # точность
)
# возвращаем текущую секунду и данные
return (int(time.time()), data_item)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment