#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 //获取当前时间
double getCurrentTime(){
struct timeval tv;
gettimeofday(&tv,NULL);
//return tv.tv_sec*1000+tv.tv_usec/1000;//毫秒
return tv.tv_sec*+tv.tv_usec/+tv.tv_usec%*0.001;//毫秒带小数
}
//计算数组内平均值
double getAvgDiff(double *diffs){
double sum = 0.0;
for(int i=;i<NUM;i++){
sum +=*(diffs+i);
}
return sum/NUM;
} //总处理函数
void handle(){
//输出到文件
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;//记录本次最开始时间
double last_time;//记录本次最后一次时间
double avg_diff;//平均响应时间
double diffs[NUM] ={0.0};
for(int i=;i<NUM;i++){ SumServiceImplPortBindingProxy sumWebservice; K8S1__getSum sumRequest;
K8S1__getSumResponse res;
int a,b;
//两个1-100的随机数
a= rand() % ;
b= rand() % ;
sumRequest.arg0 = a;
sumRequest.arg1 = b; std::cout<<"a="<<a<<",b="<<b<<std::endl; time_t tt = time(NULL);//这句返回的只是一个时间戳 精确到秒
//clock_t start_time,end_time; //这个会精确到毫秒
//start_time = clock();
double start_time,end_time;
start_time = getCurrentTime();
if(i==){
first_time = start_time;//记录第一次时间
}
fout<<"Current timestamp(k8s_sum) request(ms)="<<std::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+==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::init(argc, argv, "k8s_sum_node");
pid_t pid;
//创建子进程
for(int i=;i<FORK_NUM;i++){
pid = fork();
//子进程退出循环,不再创建子进程,全部由主进程创建子进程 ,这里是关键所在
if(pid == || pid == -){
break;
}
}
if(pid == -){
ROS_INFO("Fail to Fork!");
exit();
}
else if(pid == ){
//子进程处理逻辑
handle();
sleep();
exit();
}
else{
//主进程处理逻辑
handle();
exit();
} ros::spin();
return ;
}

采用了ROS 的编程方式

Web Service 模拟了一个 加法运算,计算求和

Fork 多进程 模拟并行访问web service获取响应时间差的更多相关文章

  1. SharePoint 2013 APP 开发示例 (六)服务端跨域访问 Web Service (REST API)

    上个示例(SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API))是基于JavaScript,运行在web browser内去访问REST AP ...

  2. SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API)

          虽然 JQuery 也能通过授权header实现跨域, 但SharePoint 提供了更简单的方法,它被实现在SP.RequestExecutor里 .它能访问跨域的服务包括REST AP ...

  3. 通过Places API Web Service获取兴趣点数据

    实验将爬取新加坡地区的银行POI数据 数据库采用mongodb,请自行安装,同时申请google的key 直接上代码 #coding=utf-8 import urllib import json i ...

  4. Calling the Web Service dynamically (.NET 动态访问Web Service)

    针对.NET平台下的WebService访问,为达到不添加引用的情况下,动态调用外部服务. 主体方法: public class WebServiceHelper { //Calling the We ...

  5. Exchange Web Service 获取邮件的附件并保存到本地的示例代码

    private static void DownLoadMailAttachments(ExchangeService service, ItemId itemId) { EmailMessage m ...

  6. C++模拟Http/Https访问web站点

    一.概述 1.Http与Https的区别与联系 在OSI参考模型中Http与Https均属于应用层协议.Http即Hypertext Transfer Protocol,超文本传输协议:而Https为 ...

  7. [转载] 学会使用Web Service上(服务器端访问)~~~

    http://www.360doc.com/content/10/0712/12/541242_38461863.shtml# 关于什么是Web Service,相信在很多地方都会有介绍.简单的讲,W ...

  8. 测试Remoting三种信道Http,Tcp,Ipc和Web Service的访问速度 (转)

    Remoting和Web Service是.net中的重要技术,都可用来实现分布式系统开发,如果是不同的平台就只能选择Web Service,但如果是同一平台,就都可以选择了.到底选择那种,当然还有访 ...

  9. .NET Remoting三种信道Http,Tcp,IPC和Web Service的访问速度比较(转)

    Remoting和Web Service是.net中的重要技术,都可用来实现分布式系统开发,如果是不同的平台就只能选择Web Service,但如果是同一平台,就都可以选择了.到底选择那种,当然还有访 ...

随机推荐

  1. 用ASP.NET Core重写了一款网络考试培训的免费软件

    在IT圈混迹了近十年,今已正当而立之年却仍一事无成,心中倍感惶恐惭愧.面对竟争如此激列的环境,该如何应对?却也知不能让自已闲着,得转起来,动起来.于是,便想着利用最新技术栈将自已原来的收费产品重写一次 ...

  2. throw 与 throws的比较

    说实话,今天在公司的实习,确确实实编号被严重打脸了,说真的,自己的基础功不扎实,希望慢慢弥补吧! 抛出异常有三种形式,一是throw,一个throws,还有一种系统自动抛异常,下面它们之间的异同. 一 ...

  3. scrapy基础知识之 Logging:

    修改配置文件settings.py,任意位置添加 LOG_FILE = "XxSpider.log" LOG_LEVEL = "INFO" Log levels ...

  4. linux服务器无telnet等测试工具,测试http+json服务连通性

    1. 问题描述: 1.公司内部服务器需要通过http接口方式访问另一公司内部接口服务器. 2.申请信息安全开通访问权限,但是只能开通到服务器+端口号,例如:192.168.1:8080,无ping权限 ...

  5. [POI2007]洪水pow 题解

    [POI2007]洪水pow 时间限制: 5 Sec  内存限制: 128 MB 题目描述 AKD市处在一个四面环山的谷地里.最近一场大暴雨引发了洪水,AKD市全被水淹没了.Blue Mary,AKD ...

  6. @ImportResource导入的xml配置里的Bean能够使用@PropertySource导入的属性值吗?

    每篇一句 大师都是偏执的,偏执才能产生力量,妥协是没有力量的.你对全世界妥协了你就是空气.所以若没有偏见,哪来的大师呢 相关阅读 [小家Spring]详解PropertyPlaceholderConf ...

  7. 如果通过脚本来关闭程序-linux

    正常情况下在linux关闭一个程序最好是走正常的关闭流程,不要直接杀死进程,这样程序的稳定性确实会收到影响,但是如果想通过脚本来关闭程序正常情况下比较困难的,我便采取了这种暴力的方法-直接杀死进程. ...

  8. UVA1103 古代象形符号 Ancient Messages 题解

    题目链接: https://www.luogu.org/problemnew/show/UVA1103 题目分析: 我们可以先进行矩阵的还原 for(int k=1;k<=4;k++) { a[ ...

  9. JWT(JSON WEB TOKEN)实例

    JWT的工具类 加密解密工具 package top.wintp.crud.util; import com.auth0.jwt.JWTSigner; import com.auth0.jwt.JWT ...

  10. c语言进阶11-经典算法代码

    重要算法一览 #include "stdio.h" #include "stdio.h" void main() { int a,b,c,i,n; int x, ...