目录
前言
我们在开发中,使用传统的关系型数据库,当数据量过大时,查询会变得很慢,甚至会崩溃卡死,我们今天来测试下C/C++项目中调用ProcessDB数据库,查询大数据时的速度
一、历史采样接口测试
1.条件准备
我们这里选用大家都有的PUBLIC库下的SYS表的PDB_OS_CPU_USAGE数据点,数据量为852932条数据
2.测试历史采样接口代码
// ProcessDB_API_TEST.cpp : ProcessDB数据库示例
//
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include "processdb_v2.h"
using namespace std;
int main(int argc, char* argv[])
{
float val = 0;
EBASE_RES result = NULL;
int res = 0;
EBASE ebase = { 0 };
/* 初始化连接控制块 */
res = ebase2_init(&ebase);
if (0 != res)
{
cout << "ebase2_init failed !" << endl;
getchar();
exit(1);
}
/*设置超时时间*/
int timeout = 30;
ebase2_set_options(&ebase, EBASE_CONNECTION_TIMEOUT, &timeout);
/* 建立连接 */
res = ebase2_connect(&ebase, "127.0.0.1", 8301, "root", "root");
if (0 != res)
{
cout << "login failed, \n";
getchar();
exit(1);
}
/* 查询历史数据的采样信息 */
char fmt[] = "%Y-%m-%d %H:%M:%S";
char startTimeStr[] = "2022-11-01 00:00:00";
char stopTimeStr[] = "2022-11-24 14:00:00";
struct tm tb = { 0 };
time_t startTime;
time_t stopTime;
if (ebase2_strptime(startTimeStr, fmt, &tb))
{
startTime = mktime(&tb);
if (startTime == -1)
{
printf("From time format error, should be like \"2011-01-01 0:0:0\"\n");
}
}
if (ebase2_strptime(stopTimeStr, fmt, &tb))
{
stopTime = mktime(&tb);
if (stopTime == -1)
{
printf("From time format error, should be like \"2011-01-01 0:0:0\"\n");
}
}
/*采样间隔*/
int sample_interval = 0;
long begin, end;
long dt;
#ifdef __EBASE_WIN32
begin = GetTickCount();
#elif defined __EBASE_POSIX
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
begin = (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
#endif
/* 查找历史采样数据 */
EBASE_RES ebase2_res;
TimeRegion timeRegion = { 0 };
timeRegion.tmBegin = startTime;
timeRegion.tmEnd = stopTime;
printf("History simple query ---------------begin-----------------\n");
res = ebase2_exec_his_sample_query(&ebase, "PUBLIC.SYS.PDB_OS_CPU_USAGE", &timeRegion, sample_interval, &ebase2_res);
if (0 != res)
{
printf("ebase2_exec_his_sample_query failed, error code: %d\n", res);
getchar();
exit(1);
}
#ifdef __EBASE_WIN32
end = GetTickCount();
#elif defined __EBASE_POSIX
clock_gettime(CLOCK_MONOTONIC, &ts);
end = (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
#endif
dt = end - begin;
int count = 0;
MemSampleRecord memSampleRecord = { 0 };
ebase2_get_result_count(ebase2_res, &count);
printf("History simple query ---------------end-------------------\n");
printf("His simple record count: %d, used: %dms\n", count, dt);
ebase2_free_result(ebase2_res);
/* 关闭连接 */
ebase2_close(&ebase);
return 0;
}
3.速度测试结果
我们可以看到,852932条数据,查询出来只需要328ms
History simple query ---------------begin-----------------
History simple query ---------------end-------------------
His simple record count: 852932, used: 328ms
二、历史统计接口测试
1.条件准备
我们这里选用大家都有的PUBLIC库下的SYS表的PDB_OS_CPU_USAGE数据点,数据量为852932条数据
2.测试历史统计接口代码
// ProcessDB_API_TEST.cpp : ProcessDB数据库示例
//
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <windows.h>
#include "processdb_v2.h"
using namespace std;
int main(int argc, char* argv[])
{
float val = 0;
EBASE_RES result = NULL;
int res = 0;
EBASE ebase = { 0 };
/* 初始化连接控制块 */
res = ebase2_init(&ebase);
if (0 != res)
{
cout << "ebase2_init failed !" << endl;
getchar();
exit(1);
}
/*设置超时时间*/
int timeout = 30;
ebase2_set_options(&ebase, EBASE_CONNECTION_TIMEOUT, &timeout);
/* 建立连接 */
res = ebase2_connect(&ebase, "127.0.0.1", 8301, "root", "root");
if (0 != res)
{
cout << "login failed, \n";
getchar();
exit(1);
}
/* 查询历史数据的采样信息 */
char fmt[] = "%Y-%m-%d %H:%M:%S";
char startTimeStr[] = "2022-11-01 00:00:00";
char stopTimeStr[] = "2022-11-24 14:00:00";
struct tm tb = { 0 };
time_t startTime;
time_t stopTime;
if (ebase2_strptime(startTimeStr, fmt, &tb))
{
startTime = mktime(&tb);
if (startTime == -1)
{
printf("From time format error, should be like \"2011-01-01 0:0:0\"\n");
}
}
if (ebase2_strptime(stopTimeStr, fmt, &tb))
{
stopTime = mktime(&tb);
if (stopTime == -1)
{
printf("From time format error, should be like \"2011-01-01 0:0:0\"\n");
}
}
/* 查询历史数据的统计信息 */
TimeRegion timeRegion = { 0 };
timeRegion.tmBegin = startTime;
timeRegion.tmEnd = stopTime;
float val_flow = 0;
float val_max = 0;
float val_min = 0;
float val_avg = 0;
int val_seconds = 0;
int static_interval = 300;
/* 测试查询损耗时间--开始 */
long begin, end;
long dt;
EBASE_RES ebase2_res;
printf("History stat query ---------------begin-----------------\n");
#ifdef __EBASE_WIN32
begin = GetTickCount();
#elif defined __EBASE_POSIX
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
begin = (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
#endif
/* 查询历史的统计信息 */
res = ebase2_exec_his_stat_query(&ebase, "PUBLIC.SYS.PDB_OS_CPU_USAGE", &timeRegion, static_interval, &ebase2_res);
if (0 != res)
{
printf("ebase2_exec_his_stat_query failed, error code: %d\n", res);
getchar();
exit(1);
}
/* 测试查询损耗时间--结束 */
#ifdef __EBASE_WIN32
end = GetTickCount();
#elif defined __EBASE_POSIX
clock_gettime(CLOCK_MONOTONIC, &ts);
end = (ts.tv_sec * 1000 + ts.tv_nsec / 1000000);
#endif
/* 测试查询损耗时间 */
dt = end - begin;
if (res != 0)
{
printf("ebase2_exec_his_stat_query failed, error code: %d\n", res);
getchar();
return 0;
}
int count = 0;
ebase2_get_result_count(ebase2_res, &count);
MemStatRecord memStatRecord = { 0 };
ebase2_free_result(ebase2_res);
printf("History stat query ---------------end-------------------\n");
printf("His stat record count: %d, used: %dms\n", count, dt);
/* 关闭连接 */
ebase2_close(&ebase);
return 0;
}
3.速度测试结果
我们可以看到,统计852932条数据,60秒一个统计间隔,只需94ms
History stat query ---------------begin-----------------
History stat query ---------------end-------------------
His stat record count: 33414, used: 94ms