这里有三个文件组成,一个是测试文件,另外两个是对MongoDB操作的封装
测试文件
#include "MongoDBWrapper.h"
#include "sperformance.h"
#include <iostream>
using namespace std;
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string.hpp>
kagula::CMongoDBWrapper& mongoDB = kagula::CMongoDBWrapper::Instance();
//测试一天内插入多次。
//测试不同日期插入。
void testInsert()
{
std::string cable_id("00d34f28-8bb9-4f22-a032-b46468d2176a");
const int type_id = 2;
std::string channel_id("c5e5ef1d-b31a-443b-a044-d70c64ac3354");
int pos = 0;
float fInterval = .0f;
float value = .1f;
mongoDB.insertOrUpdate(cable_id, type_id, channel_id, pos, fInterval, value);
//100条记录,corei3-2120,用了23s+,虽然console output拖了后腿,这个更新速度也是无法接受的。
//决定新建个方法,专门进行批处理更新。
kagula::PerformanceTest pt;
pt.start();
for (int i = 0; i < 100;i++)
{
value += .01f;
mongoDB.insertOrUpdate(cable_id, type_id, channel_id, pos, fInterval, value);
}
pt.stop();
std::cout << "100 records update elapsed time:" << pt.toString() << std::endl << std::endl;
}
void testInsertBatch()
{
std::string cable_id("00d34f28-8bb9-4f22-a032-b46468d2176a");
const int type_id = 2;
std::string channel_id("c5e5ef1d-b31a-443b-a044-d70c64ac3354");
int pos = 0;
float fInterval = .0f;
float value = .2f;
std::vector<double> vecInsert;
for (int i = 0; i < 10 * 1000; i++)
{
vecInsert.push_back(i + value);
}
cout << "testInsertBatch 10k test begin." << endl;
kagula::PerformanceTest pt;
pt.start();
mongoDB.insertOrUpdate_batch(cable_id, type_id, channel_id, fInterval, vecInsert);
pt.stop();
//10k条记录,corei3-2120,新建记录用了1,760ms - 3,655ms
//10k条记录,corei3-2120,更新记录用了304,432ms
cout << "testInsertBatch 10k elapsed time:" << pt.toString() << endl;
}
void testSplit()
{
string strBuffer;
char buf[32] = { 0 };
kagula::PerformanceTest pt;
cout << "准备测试数据" << endl;
for (int i = 0; i < 100*1000; i++)
{
sprintf_s(buf, "%d,", i);
strBuffer += buf;
}
cout << "测试100k split性能!" << endl;
pt.start();
std::vector<std::string> vecRec;
boost::split(vecRec, strBuffer, boost::is_any_of(","));
pt.stop();
//100k个点,corei3-2120 用了26,228ms+
cout << "elapsed time:" << pt.toString();
}
void testLargeArrayCopy()
{
std::vector<double> vecSrc,vecDest;
cout << "void testLargeArrayCopy() 准备测试数据" << endl;
unsigned nCount = 100 * 1000;
for (int i = 0; i < nCount;i++)
{
vecSrc.push_back(i);
}
kagula::PerformanceTest pt;
pt.start();
vecDest.resize(vecSrc.size());
for (int i = 0; i < nCount; i++)
{
vecDest[i] = vecSrc[i];
}
pt.stop();
//100k个点,corei3-2120 用了29ms+
cout << "elapsed time:" << pt.toString() << endl;
}
//测试查询指定时间位置的[cable+channel]
void testQuery()
{
kagula::PerformanceTest pt;
pt.start();
std::string cable_id("00d34f28-8bb9-4f22-a032-b46468d2176a");
std::string channel_id("c5e5ef1d-b31a-443b-a044-d70c64ac3354");
int pos = 0;
std::string outJSON;
mongoDB.query(cable_id, channel_id, pos, outJSON);
pt.stop();
cout << "Query测试:" << pt.toString() << std::endl;
cout << endl << "outJSON=>" << outJSON << endl;
}
int main(int argc, char *argv[])
{
if (mongoDB.initDB() != 0)
{
cout << "Initialize mongoDB failed!" << endl;
return -1;
}
//testSplit();
//testLargeArrayCopy();
//测试新建或更新document(记录)
//testInsert();
testInsertBatch();
//测试返回查询结果
//testQuery();
cout << "input any key to close." << endl;
cin.get();
return 0;
}
用到的头文件
#ifndef _CMongoDBWrapper_H_
#define _CMongoDBWrapper_H_
#pragma once
#include <string>
#include <vector>
/*
Title: MongoDB Access
Author: kagula
Date: 2017-09-06
Environment:
[1]VS2013 Update5
[2]mongo-c-driver-1.7.0
*/
namespace kagula
{
class CMongoDBWrapper
{
public:
static CMongoDBWrapper& Instance