#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. Ruby系列文章

    安装Ruby.多版本Ruby共存.Ruby安装慢问题 Ruby语言的一些杂项 Ruby中的常量:引号.%符号和heredoc Ruby中的数值 Ruby字符串(1):String基本用法 Ruby字符 ...

  2. Linux服务器使用Docker部署.net Core项目

    发布ASP.NET Core项目 和普通的项目发布一样,将项目发布到目标文件夹中 构建Dockerfile文件 在目标文件根目录新建Dockerfile文件(没有后缀) FROM microsoft/ ...

  3. QDomDocument 读取和编辑xml文件

    Qt中几种操作xml的方式 流方式 sax方式 dom方式 初学时,我常常采用流方式读取xml,该方式简单直观,容易理解.之后遇到了需要修改xml并重新写回的情况,流方式就显得捉襟见肘了. sax方式 ...

  4. LinkedList源码分析:JDK源码分析系列

    如果本文中有不正确的地方请指出由于没有留言可以在公众号添加我的好友共同讨论. 1.介绍 LinkedList 是线程不安全的,允许元素为null的双向链表. 2.继承结构 我们来看一下LinkedLi ...

  5. c++学习书籍推荐《The C++ Programming Language第四版》下载

    百度云及其他网盘下载地址:点我 作者简介 Bjarne Stroustrup is the designer and original implementer of C++, the author o ...

  6. asp.net core系列 68 Filter管道过滤器

    一.概述 本篇详细了解一下asp.net core filters,filter叫"筛选器"也叫"过滤器",是请求处理管道中的特定阶段之前或之后运行代码.fil ...

  7. springboot不加载mapper文件问题解析

    1. 场景描述 启动的时候报"springboot available: expected at least 1 bean which qualifies as autowire candi ...

  8. MyBatis从入门到精通:各个实体类

    SysUser类: package tk.mybatis.simple.model; import java.util.Date; public class SysUser { public Long ...

  9. MYSQL数据库的安装,配置文件,登入

    07.13自我总结 MYSQL数据库 一.MYQL数据库的安装 可以去mysql官网下载mysql压缩包 运行程序:在bin文件夹中,其中客户端运行文件是mysql.exe,服务端运行文件为mysql ...

  10. C#7.2 新增功能

    连载目录    [已更新最新开发文章,点击查看详细] C# 7.2 又是一个单点版本,它增添了大量有用的功能. 此版本的一项主要功能是避免不必要的复制或分配,进而更有效地处理值类型. C# 7.2 使 ...