Skip to content

Commit

Permalink
add test/rpcclient.py for python client
Browse files Browse the repository at this point in the history
  • Loading branch information
niean committed Jun 18, 2015
1 parent 042fd8f commit 88b4ca8
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ _testmain.go
/var
/falcon-transfer*
/cfg.json
/test
/test/build
/test/*.go

gitversion
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ curl -s -X POST -d "[{\"metric\":\"$m\", \"endpoint\":\"$e\", \"timestamp\":$ts,
- enable: true/false, 表示是否开启该jsonrpc数据接收端口, Agent发送数据使用的就是该端口
- listen: 表示监听的http端口

socket
socket #即将被废弃,请避免使用
- enable: true/false, 表示是否开启该telnet方式的数据接收端口,这是为了方便用户一行行的发送数据给transfer
- listen: 表示监听的http端口

Expand Down
2 changes: 1 addition & 1 deletion receiver/rpc/rpc_transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type TransferResp struct {
}

func (t *TransferResp) String() string {
s := fmt.Sprintf("TransferResp total=%d, err_invalid=%d, latency=%dus",
s := fmt.Sprintf("TransferResp total=%d, err_invalid=%d, latency=%dms",
t.Total, t.ErrInvalid, t.Latency)
if t.Msg != "" {
s = fmt.Sprintf("%s, msg=%s", s, t.Msg)
Expand Down
49 changes: 49 additions & 0 deletions test/rpcclient.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import json
import socket
import itertools
import time

class RPCClient(object):

def __init__(self, addr, codec=json):
self._socket = socket.create_connection(addr)
self._id_iter = itertools.count()
self._codec = codec

def _message(self, name, *params):
return dict(id=self._id_iter.next(),
params=list(params),
method=name)

def call(self, name, *params):
req = self._message(name, *params)
id = req.get('id')

mesg = self._codec.dumps(req)
self._socket.sendall(mesg)

# This will actually have to loop if resp is bigger
resp = self._socket.recv(4096)
resp = self._codec.loads(resp)

if resp.get('id') != id:
raise Exception("expected id=%s, received id=%s: %s"
%(id, resp.get('id'), resp.get('error')))

if resp.get('error') is not None:
raise Exception(resp.get('error'))

return resp.get('result')

def close(self):
self._socket.close()


if __name__ == '__main__':
rpc = RPCClient(("127.0.0.1", 8433))
for i in xrange(10000):
mv1 = dict(endpoint='host.niean', metric='metric.niean.1', value=i, step=60,
counterType='GAUGE', tags='tag=t'+str(i), timestamp=int(time.time()))
mv2 = dict(endpoint='host.niean', metric='metric.niean.2', value=i, step=60,
counterType='COUNTER', tags='tag=t'+str(i), timestamp=int(time.time()))
print rpc.call("Transfer.Update", [mv1, mv2])

0 comments on commit 88b4ca8

Please sign in to comment.