![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include <ros/ros.h> #include <iostream> #include <string> #include <cstring> // 名称空间映射表 #include "k8s_sum/SumServiceImplPortBinding.nsmap" #include "k8s_sum/soapSumServiceImplPortBindingProxy.h" //using namespace std; #include <stdio.h> #include <iostream> #include <unistd.h> #include <fstream> #include <time.h> #include <sys/types.h> #define NUM 1000 #define FORK_NUM 100 //Get the current time Double getCurrentTime () { struct timeval TV; the gettimeofday ( & TV, NULL); // return tv.tv_sec * 1000 + tv.tv_usec / 1000; // ms return tv.tv_sec * 1000 + tv.tv_usec / 1000 tv.tv_usec% + 1000 * from 0.001 ; // ms decimal } // calculates an average value within the array Double getAvgDiff ( Double * diffs) { Double SUM = 0.0 ; for ( int I = 0 ; I <NUM; I ++ ) { SUM + = * (diffs + I); } return SUM / NUM; } // total handler void handle () { // output to a file STD :: ofstream FOUT; int PID = getpid (); STD :: String fileName = " / tmp / rosSumfile_ " + std :: to_string (pid) + " .dat " ; fout.open (fileName, std :: ios :: App); Double FIRST_TIME; // record the beginning of this time Double Last_time; / / record The last time Double avg_diff; //The average response time Double diffs [NUM] = { 0.0 }; for ( int I = 0 ; I <NUM; I ++ ) { SumServiceImplPortBindingProxy sumWebservice; K8S1__getSum sumRequest; K8S1__getSumResponse RES; int A, B; // random two 1-100 number of A = RAND ()% 100 ; B = RAND ()% 100 ; sumRequest.arg0 = A; sumRequest.arg1 = B; STD :: COUT << " A = " << << A ", B = " << << B STD :: endl; time_t TT = Time (NULL); // phrase just returns a timestamp to the nearest second // of clock_t START_TIME, END_TIME; // This precision of milliseconds / / START_TIME = Clock (); Double START_TIME, END_TIME; START_TIME = getCurrentTime (); IF (I == 0 ) { FIRST_TIME = START_TIME; // log the first time } FOUT << " Current timestamp (k8s_sum) Request (MS ) = " << STD :: the to_string (START_TIME); int result = sumWebservice.getSum(&sumRequest, res); //sleep(2); 暂停2秒 if(SOAP_OK == result) { int sum_value = res.return_; //time_t tt1 = time(NULL); end_time = getCurrentTime(); if(i+1==NUM){ last_time = end_time; } fout<<",response(ms)="<<std::to_string(end_time); fout<<",the diff(ms)="<<end_time-start_time<<std::endl; diffs[i]=end_time-start_time; }else{ fout<<",request is error!"<<std::endl; } } //计算平均响应时间 avg_diff = getAvgDiff(diffs); fout<<"Count firstTime="<<std::to_string(first_time)<<",lastTime="<<std::to_string(last_time)<<",avgDiff="<<avg_diff<<std::endl; fout.close(); } int main ( int argc, char ** argv) { ROS :: the init (argc, argv, " k8s_sum_node " ); pid_t pid; // create a child process for ( int i = 0 ; i <FORK_NUM; i ++ ) { pid = fork (); // child process exits the loop, no longer creates child processes, all child processes created by the primary process, here is the key IF (pid == 0 || pid == - 1 ) { BREAK ; } } IF ( == PID - . 1 ) { ROS_INFO ( "Fork to the Fail! " ); Exit ( . 1 ); } the else IF (PID == 0 ) { // child process processing logic handle (); SLEEP ( . 5 ); Exit ( 0 ); } the else { // the main processing process logic handle (); Exit ( 0 ); } ROS :: Spin (); return 0 ; }
Programmatically using the ROS
It simulates a Web Service adder, summation