⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 statistic.cpp

📁 模拟P2P各种网络环境的,适合新手们的学习,不错的源码.
💻 CPP
字号:
#include "Statistic.h"
#include "SimEventScheduler.h"
#include "BT.h"
#include "Topology.h"
#include "BTPeer.h"
#include <string>
void Statistic::addFileDownLoadTime(string key, double mount){
	map<string, FileDownloadInof>::iterator aIt = mFileDownloadInfo.find(key);
	if( aIt != mFileDownloadInfo.end()){
		aIt->second.downloadTime += mount;
		aIt->second.downloadTimes++;
	}
	else{
		FileDownloadInof info;
		memset(&info, 0, sizeof(FileDownloadInof));
		info.downloadTimes++;
		info.downloadTime+=mount;
		mFileDownloadInfo.insert( map<string, FileDownloadInof>::value_type( key, info));
	}

}

void Statistic::addFileUploadNum(string key, unsigned int mount){
	map<string, FileDownloadInof>::iterator aIt = mFileDownloadInfo.find(key);
	if( aIt != mFileDownloadInfo.end()){
		aIt->second.uploadNum += mount;
		aIt->second.uploadTimes++;
	}
	else{
		FileDownloadInof info;
		memset(&info, 0, sizeof(FileDownloadInof));
		info.uploadTimes++;
		info.uploadNum+=mount;
		mFileDownloadInfo.insert( map<string, FileDownloadInof>::value_type( key, info));
	}

}

void Statistic::printFileDownloadInfo(void){
	map<string, FileDownloadInof>::iterator aIt = mFileDownloadInfo.begin();
	while( aIt != mFileDownloadInfo.end()){
		mTotalDownloadTime += aIt->second.downloadTime;
		mTotalDownloadTimes += aIt->second.downloadTimes;
		float avguploat = (float)(aIt->second.uploadNum)/(float)(aIt->second.uploadTimes);
		printf("file %s download avg time %f upload avg num %f %fM download times %d upload times%d\n", aIt->first.c_str(), (float)(aIt->second.downloadTime)/(float)(aIt->second.downloadTimes), avguploat, (float)(avguploat*16)/(float)1024, aIt->second.downloadTimes, aIt->second.uploadTimes);
		aIt++;
	}
	printf(" peer total download time %f download num %d avg download time %f\n", mTotalDownloadTime, mTotalDownloadTimes, (float)mTotalDownloadTime/(float)mTotalDownloadTimes );
	printf(" esp retire num %d esp recover num %d\n", mEspRetireNum, mEspRecoverNum);
}

void Statistic::printDownloadingInfo( void ){
	double usedmount = 0;
	double totalmount = 0;
	double nummount = 0;
	int 	activeFilenum = 0;
	int filenum = BTDocument::mDocumentListBykey.size();
	downloadingstatic  usedperfile[ filenum ];

	for(int j = 0; j< filenum; j++)
		memset( &usedperfile[j], 0, sizeof( downloadingstatic));

	Topology* topology = Topology::getInstance();
	totalmount += bt.mAgents[1]->getOutBandwidth();
	
	for( unsigned  int i = 2; i< bt.mAgents.size(); i++){
		if( ((BTPeer*)( bt.mAgents[i]))->mState == work ){
			int filename = atoi( ((BTPeer*)bt.mAgents[i])->mDocuments.begin()->second->mkey.c_str());
			usedperfile[filename].mount  +=  bt.mAgents[i]->getInBandwidth() - topology->getAvailableBandwidth( true, bt.mAgents[i] );
			usedperfile[filename].num ++;
			usedperfile[filename].peerTotalUpBandwidth += bt.mAgents[i]->getOutBandwidth();
			usedperfile[filename].peerTotalDownBandwidth += bt.mAgents[i]->getInBandwidth();
			
			totalmount += bt.mAgents[i]->getOutBandwidth();
			nummount++;
		}else if( ((BTPeer*)(bt.mAgents[i]))->ifUpload() ){
			totalmount += bt.mAgents[i]->getOutBandwidth();
			nummount++;
		}
	}

	for( int i = 0; i< filenum ; i++){
		if( usedperfile[i].mount ){
			usedmount += usedperfile[i].mount;
			activeFilenum++;
		}
	}	
	printf(" %f system inmount %f esp output %f  allupbandwidth %f, percent %f active peer num%f activefilenum %d\n", Scheduler.getCurrent(), usedmount, bt.getEspOutput(),  totalmount,  usedmount/totalmount, nummount, activeFilenum);

	char buf[10] = { 0 };
	for( int i = 0; i< filenum; i++){
		if( usedperfile[i].mount ){
			printf( buf, "%d", i);
			printf("system download info file %d esp file con %d output %f,  peernum %d total outbandwidth %f total inBandwidth %f\n", i, bt.getEspFileCon( string(buf) ), usedperfile[i].mount, usedperfile[i].num, usedperfile[i].peerTotalUpBandwidth, usedperfile[i].peerTotalDownBandwidth );
	      }
	}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -