📄 perf_client_example.cc
字号:
//// Copyright (c) 1996 by ISP Russia.// All rights reserved.//#ifdef _IN_ORBIX20#include "perf.hh"#define EXCP(excpt)#define CORBA2 CORBA// Wow ... !#define string_dup string_dupl#include <iostream.h>#include <stdlib.h>#else#include "perf_client.hh"#define EXCP(excpt) excpt#endif // _IN_ORBIX20#include <sys/types.h>#include <sys/time.h>#include <signal.h>#define EXCEPTION_CHECKING(str) \{ \ if (env.exception () != 0) \ { \ print_exception (env.exception (), str);\ } \ }#ifndef _IN_ORBIX20NamingContext_ptr nc_ptr;#endif // _IN_ORBIX20//extern "C" int gettimeofday(struct timeval *, struct timezone *);#define GETTIME(x) gettimeofday(x, (struct timezone *)NULL)#define SIZE_STRUCT (sizeof(short) + sizeof(long) + sizeof(float) + \ sizeof(double) + sizeof(char) + strlen("String") )#define NUM_ELEMENTS 100struct timeval starttime;struct timeval endtime;int num_samples = 100;int seq_size = 100;double total_time = 0.0;void INIT_TIME(){ total_time = 0;}void START_TIME(){ GETTIME(&starttime);}void ADD_TIME(){ GETTIME(&endtime); total_time += ((endtime.tv_sec - starttime.tv_sec) * 1000.0) + ((endtime.tv_usec - starttime.tv_usec)/ 1000.0);}void print_stats(const char *func_name, int num_bytes_per_sample) { cout << "\n\n----------- " << func_name << " ----------------" << endl; cout << "Average time in millisecs: " << (total_time / num_samples) << endl; if ( num_bytes_per_sample <= 0) return; double total_num_bytes = num_bytes_per_sample * num_samples; cout << "Total num of bytes sent: " << total_num_bytes << endl; cout << "Average num of bytes per sec: " << (long)(total_num_bytes * 1000.0 / total_time) << endl;}void test_no_param(TestOneway_ptr oneway){ int i; CORBA2::Environment env; INIT_TIME(); START_TIME(); for (i=0; i< num_samples; i++) oneway->test_no_param(env); ADD_TIME(); print_stats("Oneway::test_no_param", 0);}void test_prim_args(TestOneway_ptr oneway){ CORBA2::Environment env; CORBA2::Short shortVal = 0; CORBA2::Long longVal = 0; CORBA2::Float floatVal = 0.0; CORBA2::Double doubleVal = 0.0; CORBA2::Char charVal = 0; CORBA2::String_var stringVal(CORBA2::string_dup("String")); INIT_TIME(); START_TIME(); for (int i=0; i< num_samples; i++) oneway->test_prim_args(shortVal, longVal, floatVal, doubleVal, charVal, stringVal, env); ADD_TIME(); int num_bytes = sizeof(short) + sizeof(long) + sizeof(float) + sizeof(double) + sizeof(char) + strlen("String"); print_stats("Oneway::test_prim_args", num_bytes);}void test_struct(TestOneway_ptr oneway){ CORBA2::Environment env; PerfStruct structVal; INIT_TIME(); START_TIME(); for (int i=0; i< num_samples; i++) oneway->test_struct(structVal,env); ADD_TIME(); print_stats("Oneway::test_struct", SIZE_STRUCT);} void test_prim_seq(TestOneway_ptr oneway){ CORBA2::Environment env; int i; shortSeq shortVal; longSeq longVal; floatSeq floatVal; doubleSeq doubleVal; charSeq charVal; stringSeq stringVal; shortVal.length(seq_size); longVal.length(seq_size); floatVal.length(seq_size); doubleVal.length(seq_size); charVal.length(seq_size); stringVal.length(seq_size); for (i=0; i< seq_size; i++) { shortVal[i]=0; longVal[i]=0; floatVal[i]=0; doubleVal[i]=0; charVal[i]=0; stringVal[i]=CORBA2::string_dup("String"); } INIT_TIME(); START_TIME(); for (i=0; i< num_samples; i++) oneway->test_prim_seq(shortVal, longVal, floatVal, doubleVal, charVal, stringVal,env); ADD_TIME(); int num_bytes = (sizeof(short) + sizeof(long) + sizeof(float) + sizeof(double) + sizeof(char) + strlen("String")) * seq_size; print_stats("Oneway::test_prim_seq", num_bytes);} void test_struct_seq(TestOneway_ptr oneway){ CORBA2::Environment env; structSeq structVal; structVal.length( seq_size); int i; for (i=0; i< seq_size; i++) { PerfStruct *val = new PerfStruct; val->stringVal =CORBA2::string_dup("String"); structVal[i]=*val; } INIT_TIME(); START_TIME(); for (i=0; i< num_samples; i++) oneway->test_struct_seq(structVal,env); ADD_TIME(); int num_bytes = SIZE_STRUCT * seq_size; print_stats("Oneway::test_struct_seq", num_bytes); } void test_struct_array(TestOneway_ptr oneway){ CORBA2::Environment env; structArray structVal; int i; for (i=0; i< NUM_ELEMENTS; i++) structVal[i].stringVal =CORBA2::string_dup("String"); INIT_TIME(); START_TIME(); for (i=0; i< num_samples; i++) oneway->test_struct_array(structVal,env); ADD_TIME(); int num_bytes = SIZE_STRUCT * NUM_ELEMENTS; print_stats("Oneway::test_struct_array", num_bytes);}void test_prim_args(TestReqReply_ptr obj){ CORBA2::Environment env; CORBA2::Short in_short=0; CORBA2::Long in_long=0; CORBA2::Float in_float = 0.0; CORBA2::Double in_double = 0.0; CORBA2::Char in_char = 0; CORBA2::String_var in_string(CORBA2::string_dup("String")); CORBA2::Short inout_short=0; CORBA2::Long inout_long=0; CORBA2::Float inout_float = 0.0; CORBA2::Double inout_double = 0.0; CORBA2::Char inout_char = 0; CORBA2::String_var inout_string = CORBA2::string_dup("String"); // char * inout_string(inout_string); CORBA2::Short out_short = 0; CORBA2::Long out_long = 0; CORBA2::Float out_float = 0; CORBA2::Double out_double = 0; CORBA2::Char out_char = 0; char *out_string; CORBA2::Long ret; INIT_TIME(); int i; for (i=0; i< num_samples; i++) { START_TIME(); ret = obj->test_prim_args(in_short, in_long, in_float, in_double, in_char, in_string, inout_short, inout_long, inout_float, inout_double, inout_char, inout_string, out_short, out_long, out_float, out_double, out_char, out_string,env); ADD_TIME(); CORBA2::string_free(out_string); } // Num of bytes sent/received int num_bytes = 4 * (sizeof(short) + sizeof(long) + sizeof(float) + sizeof(double) + sizeof(char) + strlen("String")); print_stats("TestReqReply::test_prim_args", num_bytes);}void test_struct_args(TestReqReply_ptr obj){ CORBA2::Environment env; struct PerfStruct in_struct; struct PerfStruct *out_struct = NULL; struct PerfStruct inout_struct; in_struct.stringVal = CORBA2::string_dup("String"); inout_struct.stringVal = CORBA2::string_dup("String"); INIT_TIME(); for (int i=0; i< num_samples; i++) { START_TIME(); obj->test_struct_args(in_struct, inout_struct, out_struct,env); ADD_TIME(); delete out_struct; } // Num of bytes sent/received int num_bytes = 4 * SIZE_STRUCT; print_stats("TestReqReply::test_struct_args", num_bytes);}void test_prim_seq(TestReqReply_ptr obj){ CORBA2::Environment env; shortSeq in_short; longSeq in_long; floatSeq in_float; doubleSeq in_double; charSeq in_char; stringSeq in_string; shortSeq inout_short; longSeq inout_long; floatSeq inout_float; doubleSeq inout_double; charSeq inout_char; stringSeq inout_string; shortSeq *out_short = NULL; longSeq *out_long = NULL; floatSeq *out_float = NULL; doubleSeq *out_double = NULL; charSeq *out_char = NULL; stringSeq *out_string = NULL; int i; in_short.length(seq_size); in_long.length(seq_size); in_float.length(seq_size); in_double.length(seq_size); in_char.length(seq_size); in_string.length(seq_size); inout_short.length(seq_size); inout_long.length(seq_size); inout_float.length(seq_size); inout_double.length(seq_size); inout_char.length(seq_size); inout_string.length(seq_size); for (i=0; i < seq_size; i++) { in_short[i]=0; in_long[i]=0; in_float[i]=0; in_double[i]=0; in_char[i]=0; in_string[i]=CORBA2::string_dup("String"); inout_short[i]=0; inout_long[i]=0; inout_float[i]=0; inout_double[i]=0; inout_char[i]=0; inout_string[i]=CORBA2::string_dup("String"); } CORBA2::Long ret; INIT_TIME(); for (i=0; i< num_samples; i++) { START_TIME(); ret = obj->test_prim_seq(in_short, in_long, in_float, in_double, in_char, in_string, inout_short, inout_long, inout_float, inout_double, inout_char, inout_string, out_short, out_long, out_float, out_double, out_char, out_string,env); ADD_TIME(); delete out_short; delete out_long; delete out_float; delete out_double; delete out_char; delete out_string; } // Num of bytes sent/received int num_bytes = 4 * seq_size * (sizeof(short) + sizeof(long) + sizeof(float) + sizeof(double) + sizeof(char) + strlen("String")); print_stats("TestReqReply::test_prim_seq", num_bytes);}void test_struct_seq(TestReqReply_ptr obj){ CORBA2::Environment env; structSeq in_struct; structSeq inout_struct; structSeq *out_struct = NULL; int i; in_struct.length(seq_size); inout_struct.length(seq_size); for (i=0; i< seq_size; i++) { PerfStruct *in = new PerfStruct; PerfStruct *inout = new PerfStruct; in->stringVal =CORBA2::string_dup("String"); inout->stringVal =CORBA2::string_dup( "String"); in_struct[i]=*in; inout_struct[i]=*inout; } CORBA2::Long ret; INIT_TIME(); for (i=0; i< num_samples; i++) { START_TIME(); ret = obj->test_struct_seq(in_struct, inout_struct, out_struct,env); ADD_TIME(); delete out_struct; } // Num of bytes sent/received int num_bytes = 4 * seq_size * SIZE_STRUCT; print_stats("TestReqReply::test_struct_seq", num_bytes);}void test_struct_array(TestReqReply_ptr obj){ CORBA2::Environment env; structArray in_struct; structArray inout_struct; structArray_slice *out_struct; int i; for (i=0; i< NUM_ELEMENTS; i++) { in_struct[i].stringVal =CORBA2::string_dup( "String"); inout_struct[i].stringVal =CORBA2::string_dup( "String"); } CORBA2::Long ret; INIT_TIME(); for (i=0; i< num_samples; i++) { START_TIME(); ret = obj->test_struct_array(in_struct, inout_struct, out_struct,env); ADD_TIME(); structArray_free(out_struct); } // Num of bytes sent/received int num_bytes = 4 * NUM_ELEMENTS * SIZE_STRUCT; print_stats("TestReqReply::test_struct_array", num_bytes);}void sighandler(int, ...){ exit(0);} intmain(int argc, char *const *argv){ CORBA2::Environment env;#ifdef _IN_ORBIX20 char * hostname;#endif // _IN_ORBIX20 signal(SIGINT, (void (*) (int)) sighandler); int c; while ((c = getopt(argc, argv, "n:s:?h:")) != EOF) { switch (c) { case 'n': num_samples = atoi(optarg); break; case 's': seq_size = atoi(optarg); break; case 'h':#ifdef _IN_ORBIX20 hostname = (char *) malloc(strlen(optarg)+1); strcpy(hostname,optarg);#else cerr << "This option is apllicable only for Orbix application" << endl;#endif // _IN_ORBIX20 break; case '?': default: cout << "Usage: perfclnt [-n num_samples] " << "[-s seq_size]" << endl; return 0; } }#ifndef _IN_ORBIX20CORBA2::ORB_ptr orb_ptr = CORBA2::ORB_init(argc, argv, "ISP_ORB", env);NamingContext_var nc =NamingContext::_narrow (orb_ptr->resolve_initial_references((CORBA::String)"NameService", env)); nc_ptr=nc; /* dkv: let's work via IORs saved in file but not via repository */ FILE *TestOneway_f, *TestReqReply_f; char TestOneway_IOR_file[512], TestReqReply_IOR_file[512]; char TestOneway_IOR[2048], TestReqReply_IOR[2048]; char *base_dir = getenv ("ISPORB_BASE_DIR"); if (base_dir == NULL) { printf (" Set ISPORB_BASE_DIR variable first (see \"INSTALL\").\n"); exit (0); } strcpy (TestOneway_IOR_file, base_dir); strcat (TestOneway_IOR_file, "/src/examples/perf/orbix20/IORs/TestOneway"); if ((TestOneway_f = fopen (TestOneway_IOR_file, "r")) == NULL) { printf ("Can not find file %s, check whether server TestOneway is running\n", TestOneway_IOR_file); exit (0); } fgets (TestOneway_IOR, 2048, TestOneway_f); fclose (TestOneway_f); CORBA2::Object_var ov1= orb_ptr->string_to_object (TestOneway_IOR); strcpy (TestReqReply_IOR_file, base_dir); strcat (TestReqReply_IOR_file, "/src/examples/perf/orbix20/IORs/TestReqReply"); if ((TestReqReply_f = fopen (TestReqReply_IOR_file, "r")) == NULL) { printf ("Can not find file %s, check whether server TestReqReply is running\n", TestReqReply_IOR_file); exit (0); } fgets (TestReqReply_IOR, 2048, TestReqReply_f); fclose (TestReqReply_f); CORBA2::Object_var ov2= orb_ptr->string_to_object (TestReqReply_IOR); /*CORBA2::Object_var ov1= nc->resolve((CORBA::String)"TestOneway", env);*/ TestOneway_var oneway = TestOneway::_narrow(ov1); /*CORBA2::Object_var ov2= nc->resolve((CORBA::String)"TestReqReply", env);*/ TestReqReply_var obj = TestReqReply::_narrow(ov2);#else // _IN_ORBIX20 TestOneway_var oneway = TestOneway::_bind("", hostname); TestReqReply_var obj = TestReqReply::_bind("", hostname);#endif // _IN_ORBIX20 if(CORBA2::is_nil(oneway)){cout<<"not Connect to oneway"<<endl; exit(-1);} if(CORBA2::is_nil(obj)){cout<<"not Connect to ReqReplay"<<endl; exit(-1);} if ( CORBA2::is_nil(oneway) ){ cout << "testOneway: Unable to connect to server. " << endl; return 0; } /* the next line is added by dkv: to not include init time to test time */ oneway->test_no_param(); test_no_param(oneway); test_prim_args(oneway); test_struct(oneway); test_prim_seq(oneway); test_struct_seq(oneway); test_struct_array(oneway);// oneway->_release(); if ( CORBA2::is_nil(obj) ) { cout << "TestReqReply: Unable to connect to server. " << endl; return 0; } test_prim_args(obj); test_struct_args(obj); test_prim_seq(obj); test_struct_seq(obj); test_struct_array(obj); return(0);};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -