#include #include #include #include #include #include #include #include #include #include "trlitool.h" using namespace std; enum TOTO {TOTO_A,TOTO_B}; static bool verbose = false; struct HANDLE_INFO: public ARRAY_OBJ{ REQUEST_INFO req; }; // Class to show non optimal way to unserialise strings struct OBJ_NOOPTIM{ string a,b,c,d; void clear(){ a.clear(); b.clear(); c.clear(); d.clear(); } void add (REQUEST &req) const{ req.append_val (a); req.append_val (b); req.append_val (c); req.append_val (d); } int getarg(REQUEST &req){ int ret = -1; if (req.getarg(NULL,a)!=-1 && req.getarg(NULL,b)!=-1 && req.getarg(NULL,c)!=-1 && req.getarg(NULL,d)!=-1){ ret = 0; } return ret; } }; #define OBJ_NOOPTIM_receive OBJ_NOOPTIM #include "proto/test.protoh" #include "proto/test.protoch" #include "proto/webapi.protoch" static void print_duration (const char *title, long long start) { long long end = fdpass_getnow(); long long duration = end - start; fprintf (stderr,"%s: duration %Ld.%06Ld\n",title,duration/1000000,duration%1000000); } /* Compare performance of optimized object (_receive) vs one using std::string to receive results */ static void testproto_optim(CONNECT_INFO &con, unsigned size_res) { printf ("size_res=%u\n",size_res); long long start = fdpass_getnow(); for (int i=0; i<1000; i++){ (con,size_res,"this is a"); if (verbose){ printf ("test_optim internal_error=%d res=%d resb=\n",internal_error,res); for (auto &o:resb){ printf ("\ta=%s b=%s c=%s d=%s\n",o.a,o.b ,o.c,o.d); } } } print_duration ("test_optim",start); start = fdpass_getnow(); for (int i=0; i<1000; i++){ (con,size_res,"this is a"); if (verbose){ printf ("test_optim internal_error=%d res=%d resb=\n",internal_error,res); for (auto &o:resb){ printf ("\ta=%s b=%s c=%s d=%s\n",o.a.c_str(),o.b.c_str() ,o.c.c_str(),o.d.c_str()); } } } print_duration ("test_nooption",start); } int main (int argc, char *argv[]) { glocal int ret = -1; glocal bool server = false; glocal bool test1 = false; glocal bool test_optim = false; glocal bool test_webapi = false; glocal bool test_quit = false; glocal unsigned nbrep = 1000; glocal unsigned nbproc = 1; glocal const char *testhost = "testlies"; glocal const char *testport = "80"; glocal bool use_ssl = false; glocal unsigned close_after=30; glocal.ret = (argc,argv); setproginfo ("testproto","0.0","Test the serialization protocol"); setarg ('s',"server","Test server side",glocal.server,false); setarg ('v',"verbose","Show some results",verbose,false); setgrouparg ("Client tests"); setarg ('t',"test1","Various tests",glocal.test1,false); setarg ('o',"optim","Compare obj with and without _receive",glocal.test_optim,false); setarg ('q',"quit","Ask the server to end",glocal.test_quit,false); setarg ('w',"webapi","Test the webapi using http",glocal.test_webapi,false); setgrouparg ("Misc."); setarg (' ',"nbrep","Number of iteration for some tests",glocal.nbrep,false); setarg (' ',"nbproc","Number of process for some tests",glocal.nbproc,false); setarg (' ',"testhost","Test host for option -w",glocal.testhost,false); setarg (' ',"testport","Test port for option -w",glocal.testport,false); setarg (' ',"use_ssl","Use SSL for option -w",glocal.use_ssl,false); setarg (' ',"close_after","Close SSL connection after some request",glocal.close_after,false); int ret = -1; if (glocal.server){ ("unix:/tmp/test.sock",5); info.data = new HANDLE_INFO; debug_printf ("Newclient\n"); debug_printf ("received %d\n",info.linelen); HANDLE_INFO *n = (HANDLE_INFO*)info.data; (this,n->req,line,info.linelen,endserver,endclient,no,n); printf ("testa a=%s b=%u\n",a,b); rep_testa(TOTO_B); printf ("testb\n"); rep_testb(2); printf ("testc a=%s b=",a); for (auto n:b) printf ("%u ",n); printf ("\n"); vector res2; res2.push_back(TOTO_A); res2.push_back(TOTO_B); rep_testc(3,res2); printf ("testd a=%s b=%s %s %s %s\n",a,b.a,b.b,b.c,b.d); OBJ res; res.a = "answer a"; res.b = "answer b"; res.c = "answer c"; res.d = "answer d"; rep_testd (4,res); printf ("teste a=%s b=[%s %s %s %s] nb=%u\n",a,b.a,b.b,b.c,b.d,nb); vector ress; for (unsigned i=0; i // a b:U{OBJV} nb:u = res:u resb:U{OBJV}v msg printf ("testf a=%s nb=%u\n",a,nb); printf ("\tb.a=%s b.b=%s b.d=%s\n",b.a,b.b,b.d); for (auto &s:b.c) printf ("\t\t%s\n",s); vector r; for (unsigned i=0; i // a b:U{OBJTYPE} nb:u = res:u resb:U{OBJTYPE} msg printf ("testg a=%s b.a=%d b.b=%u b.c=%u b.d=%s nb=%u\n",a,b.a,b.b,b.c,b.d,nb); OBJTYPE o; o.a = 10; o.b = 20; o.c = true; o.d = "this is d"; rep_testg (10,o,"answer"); if (verbose) printf ("test_optim nb=%u a=%s\n",nb,a); vector ress(nb); for (unsigned i=0; i if (verbose) printf ("test_nooptim nb=%u a=%s\n",nb,a); vector ress(nb); for (unsigned i=0; i rep_quit("Ok"); endserver = true; tlmp_error ("Invalid request: %s\n",line); o.setrawmode(true); o.loop(); }else{ CONNECT_INFO con; con.port = "/tmp/test.sock"; if (glocal.test1){ (con,"this is a",TOTO_A); printf ("testa internal_error=%d res=%u\n",internal_error,res); (con); printf ("testb internal_error=%d res=%u\n",internal_error,res); vector v; v.push_back(TOTO_A); v.push_back(TOTO_B); (con,"this is a",v); printf ("testc internal_error=%d res=%d resb=",internal_error,res); for (auto n:resb) printf ("%u ",n); printf ("\n"); OBJ o; o.a = "hello"; o.b = "this"; o.c = "is"; o.d = "test"; (con,"this is a",o); printf ("testd internal_error=%d res=%d resb=",internal_error,res); printf ("a=%s b=%s c=%s d=%s\n",resb.a,resb.b,resb.c,resb.d); (con,"this is a",o,0); printf ("teste internal_error=%d res=%d msg=%s resb=\n",internal_error,res,msg); for (auto &o:resb){ printf ("\ta=%s b=%s c=%s d=%s\n",o.a,o.b,o.c,o.d); } (con,"this is a",o,10); printf ("teste internal_error=%d res=%d msg=%s resb=\n",internal_error,res,msg); for (auto &o:resb){ printf ("\ta=%s b=%s c=%s d=%s\n",o.a,o.b,o.c,o.d); } OBJV ov; ov.a = "this is a"; ov.b = "this is b"; ov.c.push_back("First c"); ov.d = "this is d"; for (int i=0; i<20; i+= 10){ (con,"this is a",ov,i); printf ("testf internal_error=%d res=%d msg=%s resb=\n",internal_error,res,msg); for (auto &o:resb){ printf ("\to a=%s b=%s d=%s\n",o.a,o.b,o.d); for (auto &s:o.c) printf ("\t\t%s\n",s); OBJV oo = o; printf ("\too a=%s b=%s d=%s\n",oo.a.c_str(),oo.b.c_str(),oo.d.c_str()); for (auto &s:oo.c) printf ("\t\t%s\n",s.c_str()); } } OBJTYPE ot; ot.a = 11; ot.b = 22; ot.c = true; ot.d = "this is d"; (con,"this is a",ot,10); // a b:U{OBJTYPE} nb:u = res:u resb:U{OBJTYPE} msg printf ("testg res=%u resb={%d %u %u %s} msg=%s\n",res ,resb.a,resb.b,resb.c,resb.d,msg); }else if (glocal.test_optim){ testproto_optim (con,1000); testproto_optim (con,1); }else if (glocal.test_quit){ (con); printf ("done=%s\n",done); }else if (glocal.test_webapi){ REQUEST_JSON req; { int vara = 10; const char *varb = "hel\"\\{lo"; vector varc = {1,2,3}; bool vard = true; vector> vare; for (unsigned i=0; i<4; i++){ vector v; for (unsigned j=0; j varc; bool vard=false; vector> vare; REQUEST_JSON rep; int ok = rep.addpart(buf); if (ok!=1){ tlmp_error ("addpart ok=%d\n",ok); }else if (rep.getarg("vara",vara)==-1){ tlmp_error ("Can't retrieve vara\n"); }else if (rep.getarg("varb",varb)==-1){ tlmp_error ("Can't retrieve varb\n"); }else if (rep.getarg("varc",varc)==-1){ tlmp_error ("Can't retrieve varc\n"); }else if (rep.getarg("vard",vard)==-1){ tlmp_error ("Can't retrieve vard\n"); }else if (rep.getarg("vare",vare)==-1){ tlmp_error ("Can't retrieve vare\n"); }else if (!rep.is_all_read()){ tlmp_error ("is_all_read failed\n"); } printf ("vara=%d varb=%s varc=[",vara,varb); const char *sep = ""; for (auto n:varc){ printf ("%s%d",sep,n); sep = ","; } printf ("] vard=%d vare=[",vard); sep=""; for (auto &v:vare){ printf ("%s[",sep); const char *sep1 = ""; for (auto n:v){ printf ("%s%d",sep1,n); sep1 = ","; } printf ("]"); sep = ","; } printf ("]\n"); } CONNECT_HTTP_INFO hcon; hcon.host = glocal.testhost; hcon.port = glocal.testport; hcon.use_ssl = glocal.use_ssl; long long start = fdpass_getnow(); for (unsigned p=0; p(hcon,i); if (verbose) printf ("internal_error=%d aplus1=%u\n",internal_error,aplus1); if (aplus1 != glocal.expect) tlmp_error ("aplus1=%u expect %u\n",aplus1,glocal.expect); nbreq++; if (nbreq >= glocal.close_after){ hcon.close(); nbreq=0; } } _exit (0); }else if (pid == (pid_t)-1){ tlmp_error ("Can't fork (%s)\n",strerror(errno)); exit (-1); } } int st; while (wait(&st)!=-1); print_duration ("test JSON",start); TESTOBJ q; q.a = "this is a"; q.b = "this is b"; q.c = "this is c"; (hcon,q); printf ("internal_error=%d aplus1={%s,%s,%s}\n",internal_error,aplus1.a,aplus1.b,aplus1.c); vector qs; for (int i=0; i<20; i++) qs.push_back(q); (hcon,qs); printf ("internal_error=%d\n",internal_error); for (auto &a:aplus1){ printf ("\t{%s,%s,%s}\n",a.a,a.b,a.c); } char buf[40000]; for (unsigned i=0; i(hcon,bob); printf ("internal_error=%d msg=%s\n",internal_error,msg); int len = content.getsize(); printf ("content size=%zu :%*.*s:\n",content.getsize(),len,len,(const char *)content.getbuffer()); } }else{ usage(); } } return ret; return glocal.ret; }