Fork 多进程 模拟并行访问web service获取响应时间差
#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获取响应时间差的更多相关文章
- SharePoint 2013 APP 开发示例 (六)服务端跨域访问 Web Service (REST API)
上个示例(SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API))是基于JavaScript,运行在web browser内去访问REST AP ...
- SharePoint 2013 APP 开发示例 (五)跨域访问 Web Service (REST API)
虽然 JQuery 也能通过授权header实现跨域, 但SharePoint 提供了更简单的方法,它被实现在SP.RequestExecutor里 .它能访问跨域的服务包括REST AP ...
- 通过Places API Web Service获取兴趣点数据
实验将爬取新加坡地区的银行POI数据 数据库采用mongodb,请自行安装,同时申请google的key 直接上代码 #coding=utf-8 import urllib import json i ...
- Calling the Web Service dynamically (.NET 动态访问Web Service)
针对.NET平台下的WebService访问,为达到不添加引用的情况下,动态调用外部服务. 主体方法: public class WebServiceHelper { //Calling the We ...
- Exchange Web Service 获取邮件的附件并保存到本地的示例代码
private static void DownLoadMailAttachments(ExchangeService service, ItemId itemId) { EmailMessage m ...
- C++模拟Http/Https访问web站点
一.概述 1.Http与Https的区别与联系 在OSI参考模型中Http与Https均属于应用层协议.Http即Hypertext Transfer Protocol,超文本传输协议:而Https为 ...
- [转载] 学会使用Web Service上(服务器端访问)~~~
http://www.360doc.com/content/10/0712/12/541242_38461863.shtml# 关于什么是Web Service,相信在很多地方都会有介绍.简单的讲,W ...
- 测试Remoting三种信道Http,Tcp,Ipc和Web Service的访问速度 (转)
Remoting和Web Service是.net中的重要技术,都可用来实现分布式系统开发,如果是不同的平台就只能选择Web Service,但如果是同一平台,就都可以选择了.到底选择那种,当然还有访 ...
- .NET Remoting三种信道Http,Tcp,IPC和Web Service的访问速度比较(转)
Remoting和Web Service是.net中的重要技术,都可用来实现分布式系统开发,如果是不同的平台就只能选择Web Service,但如果是同一平台,就都可以选择了.到底选择那种,当然还有访 ...
随机推荐
- Programming In Lua 第一章
1,Lua可以嵌入其他应用程序(如CGILua或IUPLua). 2,lua代码的语句,分号是可以省略的.同一行可以有多条lua语句,最好用分号隔开(当然也可以不隔开) 3,外壳与lua解释器的区别. ...
- VIM技巧, .vimrc文件
将 .vimrc文件上传到用户家目录下 文件内容如下: autocmd BufNewFile *.py,*.cc,*.sh,*.java exec ":call SetTitle()&quo ...
- C++ 洛谷P1230 智力大冲浪
题目描述 小伟报名参加中央电视台的智力大冲浪节目.本次挑战赛吸引了众多参赛者,主持人为了表彰大家的勇气,先奖励每个参赛者m元.先不要太高兴!因为这些钱还不一定都是你的?!接下来主持人宣布了比赛规则: ...
- FireFox下Canvas使用图像合成绘制SVG的Bug
本文适合适合对canvas绘制.图形学.前端可视化感兴趣的读者阅读. 楔子 所有的事情都会有一个起因.最近产品上需要做一个这样的功能:给一些图形进行染色处理.想想这还不是顺手拈来的事情,早就研究过图形 ...
- SCUT 125 :笔芯回文(DP)
https://scut.online/p/125 125. 笔芯回文 题目描述 bxbx有一个长度一个字符串SS,bxbx可以对其进行若干次操作. 每次操作可以删掉一个长度为k(1 \leq k \ ...
- HttpUtility.UrlEncode讲解
hello 大家好,今天讲讲HttpUtility.UrlEncode编码 HttpUtility.UrlEncode方法有4个重载分别如下 我们有这么一个字符串 string str = " ...
- Java第二次作业——数组和String类
Java第二次作业--数组和String类 学习总结 1.学习使用Eclipse关联jdk源代码,查看String类的equals()方法,截图,并学习其实现方法.举例说明equals方法和==的区别 ...
- [译]试用新的System.Text.Json API
译注 可能有的小伙伴已经知道了,在.NET Core 3.0中微软加入了对JSON的内置支持. 一直以来.NET开发者们已经习惯使用Json.NET这个强大的库来处理JSON. 那么.NET为什么要增 ...
- Hash的应用2
代码: #include <stdio.h> #define OFFSET 500000//偏移量 ];//记录每个数是否出现,出现为1,不出现为0 int main(){ int n,m ...
- 20140115-URL编码与解码
UrlEncode()方法,有两个类都有这个方法即HttpUtility.UrlEncode和Server.UrlEncode 区别: 1.HttpUtility.UrlEncode,HttpUtil ...