⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 perf_server_example.cc

📁 C++的CORBA程序源代码,isporb.1.1.1.tar.gz
💻 CC
字号:
//// Copyright (c) 1996 by ISP Russia.// All rights reserved.//#ifdef _IN_ORBIX20#include "perf.hh"#define CORBA2 CORBA// Wow ... !#define string_dup string_dupl#include <iostream.h>#include <stdlib.h>#else#include "perf_server_t.hh"#endif // _IN_ORBIX20#include <sys/types.h>#include <sys/time.h>#include <signal.h>/* added by dkv */#include <stdio.h>#define EXCEPTION_CHECKING(str)			\{						\    if (env.exception () != 0)			\      {						\        print_exception (env.exception (), str);\      }						\                                                  }#ifndef _IN_ORBIX20typedef _tie_TestOneway<TestOneway_templated> tem_TestOneway;typedef _tie_TestReqReply<TestReqReply_templated> tem_TestReqReply;NamingContext_ptr nc_ptr;#endif // _IN_ORBIX20TestOneway_ptr get_local_TestOneway();TestReqReply_ptr get_local_TestReqReply();TestOneway_var TestOneway_i;#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;extern int	seq_size ;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; // !! not structArray !!        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);}/* added by dkv */FILE *TestOneway_f, *TestReqReply_f;/* moved from main() */CORBA2::ORB_ptr	orb_ptr;intmain(int argc, char *const *argv){  CORBA2::Environment env;#ifndef _IN_ORBIX20    orb_ptr = CORBA2::ORB_init(argc, argv, "ISP_ORB", env);  TCP_OA_ptr	oa_ptr = TCP_OA::init (orb_ptr, argc, argv, 0, env);      NamingContext_var nc =    NamingContext::_narrow (orb_ptr->resolve_initial_references((CORBA::String)"NameService", env));    nc_ptr=nc;#endif // _IN_ORBIX20    /* dkv: let's work via IORs saved in file but not via repository */    char TestOneway_IOR_file[512], TestReqReply_IOR_file[512];    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, "w")) == NULL) {      printf ("Can not open file %s for writing\n", TestOneway_IOR_file);      exit (0);    }        strcpy (TestReqReply_IOR_file, base_dir);    strcat (TestReqReply_IOR_file, "/src/examples/perf/orbix20/IORs/TestReqReply");        if ((TestReqReply_f = fopen (TestReqReply_IOR_file, "w")) == NULL) {      printf ("Can not open file %s for writing\n", TestReqReply_IOR_file);      exit (0);    }    /* end of added by dkv */        TestReqReply_var TestReqReply_i= get_local_TestReqReply();    TestOneway_i= get_local_TestOneway();    TestOneway_var oneway =TestOneway_i;     TestReqReply_var obj=TestReqReply_i;  signal(SIGINT, (void (*) (int)) sighandler);  int c;  while ((c = getopt(argc, argv, "n:s:?")) != EOF) {    switch (c) {    case 'n':      num_samples = atoi(optarg);      break;    case 's':      seq_size = atoi(optarg);      break;    case '?':    default:      cout << "Usage: perfclnt [-n num_samples] "           << "[-s seq_size]" << endl;      return 0;    }  }  	if ( CORBA2::is_nil(oneway) ){          cout << "testOneway: Unable to connect to server. "               << endl;          return 0;	}        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);  #ifndef _IN_ORBIX20        oa_ptr->impl_is_ready (env);#endif // _IN_ORBIX20      return(0);}TestOneway_ptr get_local_TestOneway(){  CORBA2::Environment env;  TestOneway_templated * e1_t = new TestOneway_templated();  tem_TestOneway * TestOneway_p = new tem_TestOneway;    TestOneway_p->bind(*e1_t);  TestOneway_ptr ret = TestOneway_p->get_objref ();    /* dkv: let's work via IORs saved in file but not via repository */  CORBA2::String_var TestOneway_IOR = orb_ptr->object_to_string (ret);  fputs (TestOneway_IOR, TestOneway_f);  fclose (TestOneway_f);  /* nc_ptr->bind ((CORBA::String)"TestOneway", ret, env); */  return ret;};TestReqReply_ptr get_local_TestReqReply(){  CORBA2::Environment env;  TestReqReply_templated * e1_t = new TestReqReply_templated();  tem_TestReqReply * TestReqReply_p = new tem_TestReqReply;    TestReqReply_p->bind(*e1_t);  TestReqReply_ptr ret = TestReqReply_p->get_objref ();    /* dkv: let's work via IORs saved in file but not via repository */  CORBA2::String_var TestReqReply_IOR = orb_ptr->object_to_string (ret);  fputs (TestReqReply_IOR, TestReqReply_f);  fclose (TestReqReply_f);  /*nc_ptr->bind ((CORBA::String)"TestReqReply", ret, env);*/  return ret;};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -