mirror of
https://github.com/cgrates/cgrates.git
synced 2026-02-11 18:16:24 +05:00
stress test from pytghon
This commit is contained in:
@@ -3,8 +3,10 @@ package main
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/rpc"
|
||||
"net/rpc/jsonrpc"
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
@@ -42,22 +44,40 @@ func CallRater(key string) (reply string) {
|
||||
return
|
||||
}
|
||||
|
||||
func listenToTheWorld() {
|
||||
l, err := net.Listen("tcp", ":5090")
|
||||
defer l.Close()
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Print("listening:", l.Addr())
|
||||
|
||||
responder := new(Responder)
|
||||
rpc.Register(responder)
|
||||
|
||||
for {
|
||||
c, err := l.Accept()
|
||||
if err != nil {
|
||||
log.Printf("accept error: %s", c)
|
||||
continue
|
||||
}
|
||||
|
||||
log.Printf("connection started: %v", c.RemoteAddr())
|
||||
go jsonrpc.ServeConn(c)
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
raterList = NewRaterList()
|
||||
raterServer := new(RaterServer)
|
||||
rpc.Register(raterServer)
|
||||
rpc.HandleHTTP()
|
||||
|
||||
go StopSingnalHandler()
|
||||
go StopSingnalHandler()
|
||||
go listenToTheWorld()
|
||||
|
||||
responder := new(Responder)
|
||||
srvr := rpc.NewServer()
|
||||
srvr.Register(responder)
|
||||
f1 := func(w http.ResponseWriter, req *http.Request) {
|
||||
srvr.ServeHTTP(w, req)
|
||||
}
|
||||
http.HandleFunc("/rpc", f1)
|
||||
|
||||
http.HandleFunc("/", handler)
|
||||
log.Print("The server is listening...")
|
||||
http.ListenAndServe(":2000", nil)
|
||||
|
||||
@@ -9,11 +9,12 @@ import (
|
||||
var (
|
||||
fileName = flag.String("fileName", "storage.kch", "kyoto storage file")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
db, _ := kc.Open(*fileName, kc.WRITE)
|
||||
defer db.Close()
|
||||
|
||||
|
||||
db.Set("test", "12223")
|
||||
fmt.Println("Done!")
|
||||
}
|
||||
|
||||
@@ -1,149 +0,0 @@
|
||||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
# This program is free software; you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Lesser General Public License as published by the
|
||||
# Free Software Foundation; either version 3, or (at your option) any later
|
||||
# version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful, but
|
||||
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY
|
||||
# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
# for more details.
|
||||
|
||||
"Pythonic simple JSON RPC Client implementation"
|
||||
|
||||
__author__ = "Mariano Reingart (reingart@gmail.com)"
|
||||
__copyright__ = "Copyright (C) 2011 Mariano Reingart"
|
||||
__license__ = "LGPL 3.0"
|
||||
__version__ = "0.04"
|
||||
|
||||
|
||||
import urllib
|
||||
from xmlrpclib import Transport, SafeTransport
|
||||
from cStringIO import StringIO
|
||||
import random
|
||||
import sys
|
||||
try:
|
||||
import gluon.contrib.simplejson as json # try web2py json serializer
|
||||
except ImportError:
|
||||
try:
|
||||
import json # try stdlib (py2.6)
|
||||
except:
|
||||
import simplejson as json # try external module
|
||||
|
||||
|
||||
class JSONRPCError(RuntimeError):
|
||||
"Error object for remote procedure call fail"
|
||||
def __init__(self, code, message):
|
||||
self.code = code
|
||||
self.message = message
|
||||
def __unicode__(self):
|
||||
return u"%s: %s" % (self.code, self.message)
|
||||
def __str__(self):
|
||||
return self.__unicode__().encode("ascii","ignore")
|
||||
|
||||
|
||||
class JSONDummyParser:
|
||||
"json wrapper for xmlrpclib parser interfase"
|
||||
def __init__(self):
|
||||
self.buf = StringIO()
|
||||
def feed(self, data):
|
||||
self.buf.write(data)
|
||||
def close(self):
|
||||
return self.buf.getvalue()
|
||||
|
||||
|
||||
class JSONTransportMixin:
|
||||
"json wrapper for xmlrpclib transport interfase"
|
||||
|
||||
def send_content(self, connection, request_body):
|
||||
connection.putheader("Content-Type", "application/json")
|
||||
connection.putheader("Content-Length", str(len(request_body)))
|
||||
connection.endheaders()
|
||||
if request_body:
|
||||
connection.send(request_body)
|
||||
# todo: add gzip compression
|
||||
|
||||
def getparser(self):
|
||||
# get parser and unmarshaller
|
||||
parser = JSONDummyParser()
|
||||
return parser, parser
|
||||
|
||||
|
||||
class JSONTransport(JSONTransportMixin, Transport):
|
||||
pass
|
||||
|
||||
class JSONSafeTransport(JSONTransportMixin, SafeTransport):
|
||||
pass
|
||||
|
||||
|
||||
class ServerProxy(object):
|
||||
"JSON RPC Simple Client Service Proxy"
|
||||
|
||||
def __init__(self, uri, transport=None, encoding=None, verbose=0):
|
||||
self.location = uri # server location (url)
|
||||
self.trace = verbose # show debug messages
|
||||
self.exceptions = True # raise errors? (JSONRPCError)
|
||||
self.timeout = None
|
||||
self.json_request = self.json_response = ''
|
||||
|
||||
type, uri = urllib.splittype(uri)
|
||||
if type not in ("http", "https"):
|
||||
raise IOError, "unsupported JSON-RPC protocol"
|
||||
self.__host, self.__handler = urllib.splithost(uri)
|
||||
|
||||
if transport is None:
|
||||
if type == "https":
|
||||
transport = JSONSafeTransport()
|
||||
else:
|
||||
transport = JSONTransport()
|
||||
self.__transport = transport
|
||||
self.__encoding = encoding
|
||||
self.__verbose = verbose
|
||||
|
||||
def __getattr__(self, attr):
|
||||
"pseudo method that can be called"
|
||||
return lambda *args: self.call(attr, *args)
|
||||
|
||||
def call(self, method, *args):
|
||||
"JSON RPC communication (method invocation)"
|
||||
|
||||
# build data sent to the service
|
||||
request_id = random.randint(0, sys.maxint)
|
||||
data = {'id': request_id, 'method': method, 'params': args, }
|
||||
request = json.dumps(data)
|
||||
|
||||
# make HTTP request (retry if connection is lost)
|
||||
response = self.__transport.request(
|
||||
self.__host,
|
||||
self.__handler,
|
||||
request,
|
||||
verbose=self.__verbose
|
||||
)
|
||||
|
||||
# store plain request and response for further debugging
|
||||
self.json_request = request
|
||||
self.json_response = response
|
||||
|
||||
# parse json data coming from service
|
||||
# {'version': '1.1', 'id': id, 'result': result, 'error': None}
|
||||
response = json.loads(response)
|
||||
|
||||
if response['id'] != request_id:
|
||||
raise JSONRPCError(0, "JSON Request ID != Response ID")
|
||||
|
||||
self.error = response.get('error', {})
|
||||
if self.error and self.exceptions:
|
||||
raise JSONRPCError(self.error.get('code', 0), self.error.get('message', ''))
|
||||
|
||||
return response.get('result')
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
# basic tests:
|
||||
location = "http://www.web2py.com.ar/webservices/sample/call/jsonrpc"
|
||||
client = ServerProxy(location, verbose='--verbose' in sys.argv,)
|
||||
print client.add(1, 2)
|
||||
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/rpc"
|
||||
"net/rpc/jsonrpc"
|
||||
"fmt"
|
||||
//"time"
|
||||
)
|
||||
|
||||
|
||||
func main(){
|
||||
client, _ := rpc.DialHTTPPath("tcp", "localhost:2000", "/rpc")
|
||||
client, _ := jsonrpc.Dial("tcp", "localhost:5090")
|
||||
var reply string
|
||||
i:= 0
|
||||
for ; i < 5 * 10e3; i++ {
|
||||
|
||||
54
cmd/stresstest/stresstest.py
Normal file
54
cmd/stresstest/stresstest.py
Normal file
@@ -0,0 +1,54 @@
|
||||
# jsonclient.py
|
||||
# A simple JSONRPC client library, created to work with Go servers
|
||||
# Written by Stephen Day
|
||||
# Modified by Bruce Eckel to work with both Python 2 & 3
|
||||
import json, socket, itertools
|
||||
|
||||
class JSONClient(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=next(self._id_iter),
|
||||
params=list(params),
|
||||
method=name)
|
||||
|
||||
def call(self, name, *params):
|
||||
request = self._message(name, *params)
|
||||
id = request.get('id')
|
||||
msg = self._codec.dumps(request)
|
||||
self._socket.sendall(msg.encode())
|
||||
|
||||
# This will actually have to loop if resp is bigger
|
||||
response = self._socket.recv(4096)
|
||||
response = self._codec.loads(response.decode())
|
||||
|
||||
if response.get('id') != id:
|
||||
raise Exception("expected id=%s, received id=%s: %s"
|
||||
%(id, response.get('id'),
|
||||
response.get('error')))
|
||||
|
||||
if response.get('error') is not None:
|
||||
raise Exception(response.get('error'))
|
||||
|
||||
return response.get('result')
|
||||
|
||||
def close(self):
|
||||
self._socket.close()
|
||||
|
||||
|
||||
rpc =JSONClient(("127.0.0.1", 5090))
|
||||
|
||||
# alternative to the above
|
||||
s = socket.create_connection(("127.0.0.1", 5090))
|
||||
s.sendall(json.dumps(({"id": 1, "method": "Responder.Get", "params": ["test"]})))
|
||||
print s.recv(4096)
|
||||
|
||||
i = 0
|
||||
result = ""
|
||||
for i in xrange(5 * int(10e3) + 1):
|
||||
result = rpc.call("Responder.Get", "test")
|
||||
print i, result
|
||||
@@ -1,5 +0,0 @@
|
||||
import simplejsonrpc as jsonrpc
|
||||
|
||||
server = jsonrpc.Server("http://localhost:2000/rpc")
|
||||
print dir(server)
|
||||
#print server.Get("test")
|
||||
@@ -1,5 +0,0 @@
|
||||
from sj import *
|
||||
URL = "http://127.0.0.1:2000/rpc"
|
||||
service = ServerProxy(URL, verbose=True)
|
||||
print service.call("Responder.Get", "test")
|
||||
print service.ResponseGet("test")
|
||||
Reference in New Issue
Block a user