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,但如果是同一平台,就都可以选择了.到底选择那种,当然还有访 ...
随机推荐
- 学习体验centos7 下根目录扩容操作(步骤详细!!!)
转自 苗尼玛乔 感谢你!
- 使用SpringSecurity搭建授权认证服务(1) -- 基本demo认证原理
使用SpringSecurity搭建授权认证服务(1) -- 基本demo 登录认证是做后台开发的最基本的能力,初学就知道一个interceptor或者filter拦截所有请求,然后判断参数是否合理, ...
- java虚拟机-GC-新生代的GC、老年代的GC
名词解释: GC:垃圾收集器 Minor GC:新生代GC,指发生在新生代的垃圾收集动作,所有的Minor GC都会触发全世界的暂停(stop-the-world),停止应用程序的线程,不过这个过程非 ...
- 常用的方法论-SMART
- Codeforces 758C:Unfair Poll(思维+模拟)
http://codeforces.com/problemset/problem/758/C 题意:教室里有n列m排,老师上课点名从第一列第一排开始往后点,直到点到第一列第m排,就从第二列第一排开始点 ...
- [Python学习]错误篇二:切换当前工作目录时出错——FileNotFoundError: [WinError 3] 系统找不到指定的路径
REFERENCE:<Head First Python> ID:我的第二篇[Python学习] BIRTHDAY:2019.7.13 EXPERIENCE_SHARING:解决切换当前工 ...
- lleetcode 1 two sum c++
Problem describe:https://leetcode.com/problems/two-sum/ Given an array of integers, return indices o ...
- Python 爬虫从入门到进阶之路(十八)
在之前的文章我们通过 scrapy 框架 及 scrapy.Spider 类做了一个<糗事百科>的糗百爬虫,本章我们再来看一下相较于 scrapy.Spider 类更为强大的 CrawlS ...
- 1.为什么会有Servlet?它解决了什么问题?
1. 为什么会出现Servlet? 因为web服务器(tomcat.Weblogic.iis.apache)没有处理动态资源请求的能力(即该请求需要计算),只能处理静态资源的请求(如果浏览器请求某个h ...
- RSYNC部署
1 rsync简介 1.1 什么是rsync rsync: - a fast, versatile, remote (and local) file-copying toolrsync:是一种快速,多 ...