首页 > 其他分享 >cpp test write content speed to ssd and usual disk respectively 1M,10M,100M rows data,the fact has

cpp test write content speed to ssd and usual disk respectively 1M,10M,100M rows data,the fact has

时间:2023-06-05 23:57:14浏览次数:69  
标签:std now chrono write include time ssd disk start

#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <thread>
#include <uuid/uuid.h>

std::string get_time_now()
{
    auto now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);
    struct tm tm_info = *localtime(&raw_time);
    std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
    auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
    auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
    std::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S") << "_"
       << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000)
       << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000)
       << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000);
    return ss.str();
}
 
char *uuid_value = (char *)malloc(40);
char *get_uuid_value()
{
    uuid_t new_uuid;
    uuid_generate(new_uuid);
    uuid_unparse(new_uuid, uuid_value);
    return uuid_value;
}

void log_file(const std::string &file_name, const int &loops)
{
    std::fstream w_file(file_name, std::ios::app);
    if (!w_file.is_open())
    {
        std::cout << get_time_now() << ",create or open " << file_name << " failed!" << std::endl;
        return;
    }

    std::uint64_t num = 0;
    std::stringstream ss;

    for (int j = 0; j < loops; j++)
    {
        for (int i = 0; i < 1000000; i++)
        {
            ss << ++num << "," << get_uuid_value() << std::endl;
        }
        w_file << ss.str();
        ss = std::stringstream();
        std::cout << get_time_now() << "," << num << ",loop:" << j + 1 << std::endl;
    }

    w_file.close();
    std::cout << get_time_now() << ",finish in " << __FUNCTION__ << std::endl;
}
 
std::stringstream read_file_to_ss(const std::string &file_name)
{
    std::stringstream ss;
    std::fstream r_file(file_name, std::ios::in);
    if (!r_file.is_open())
    {
        std::cout << get_time_now() << " open " << file_name << " failed!" << std::endl;
        return ss;
    }

    std::string line;
    while (getline(r_file, line))
    {
        ss << line;
    }
    r_file.close();
    return ss;
}

void test_populate_ssd(const std::string &src_name, const int &len, const std::string &dest_file)
{
    // std::string file_name1="log1.txt",file_name10="log10.txt",file_name_100="log100.txt";
    // /media/fred/WD_BLACK/log1.txt
    std::stringstream ss = read_file_to_ss(src_name);
    std::fstream w_file(dest_file, std::ios::app);
    std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, _end_time;
    for (int i = 0; i < 10; i++)
    {
        _start_time = std::chrono::high_resolution_clock::now();
        w_file << ss.str();
        _end_time = std::chrono::high_resolution_clock::now();
        std::cout << get_time_now()<<",src file:"<<src_name << ",rows:" << len << ",loop: " << i + 1 << ",length:" << ss.str().length()<<std::endl;
        std::cout << ",Time cost:"
                  << std::chrono::duration_cast<std::chrono::seconds>(_end_time - _start_time).count() << " seconds,"
                  << std::chrono::duration_cast<std::chrono::milliseconds>(_end_time - _start_time).count() << " mills,"
                  << std::chrono::duration_cast<std::chrono::microseconds>(_end_time - _start_time).count() << " micros,"
                  << std::chrono::duration_cast<std::chrono::nanoseconds>(_end_time - _start_time).count() << " nanos!" << std::endl;
    }
    std::cout << get_time_now() << ",finish len:" << len << std::this_thread::get_id() << std::endl;
}

void test_populate_ssd_1_10_100()
{
    std::cout << "1000000" << std::endl;
    test_populate_ssd("log1.txt", 1000000, "/media/fred/WD_BLACK/log1.txt");
    std::cout << std::endl
              << std::endl;
    std::cout << "10000000" << std::endl;
    test_populate_ssd("log10.txt", 10000000, "/media/fred/WD_BLACK/log10.txt");
    std::cout << std::endl
              << std::endl;
    std::cout << "100000000" << std::endl;
    test_populate_ssd("log100.txt", 1000000, "/media/fred/WD_BLACK/log100.txt");
    std::cout << std::endl
              << std::endl;
              std::cout << get_time_now() << ",finish in:" << __FUNCTION__<<","<< std::this_thread::get_id() << std::endl;
}

int main(int args, char **argv)
{ 
    // log_file(argv[1], atoi(argv[2]));
    test_populate_ssd_1_10_100();
    std::cout << get_time_now() << ",finish in " << __FUNCTION__ << std::endl;
}

Compile

g++-12 -std=c++2a -I. *.cpp -o h1 -luuid

Run

nohup ./h1 >>write.log | tail -f write.log

 

 

 

 

The execute script as below

 

#include <chrono>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <thread>
#include <uuid/uuid.h>

std::string get_time_now()
{
    auto now = std::chrono::high_resolution_clock::now();
    time_t raw_time = std::chrono::high_resolution_clock::to_time_t(now);
    struct tm tm_info = *localtime(&raw_time);
    std::chrono::seconds seconds = std::chrono::duration_cast<std::chrono::seconds>(now.time_since_epoch());
    auto mills = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
    auto micros = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch());
    auto nanos = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch());
    std::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S") << "_"
       << std::setw(3) << std::setfill('0') << (mills.count() - seconds.count() * 1000)
       << std::setw(3) << std::setfill('0') << (micros.count() - mills.count() * 1000)
       << std::setw(3) << std::setfill('0') << (nanos.count() - micros.count() * 1000);
    return ss.str();
}
 
char *uuid_value = (char *)malloc(40);
char *get_uuid_value()
{
    uuid_t new_uuid;
    uuid_generate(new_uuid);
    uuid_unparse(new_uuid, uuid_value);
    return uuid_value;
}

void log_file(const std::string &file_name, const int &loops)
{
    std::fstream w_file(file_name, std::ios::app);
    if (!w_file.is_open())
    {
        std::cout << get_time_now() << ",create or open " << file_name << " failed!" << std::endl;
        return;
    }

    std::uint64_t num = 0;
    std::stringstream ss;

    for (int j = 0; j < loops; j++)
    {
        for (int i = 0; i < 1000000; i++)
        {
            ss << ++num << "," << get_uuid_value() << std::endl;
        }
        w_file << ss.str();
        ss = std::stringstream();
        std::cout << get_time_now() << "," << num << ",loop:" << j + 1 << std::endl;
    }

    w_file.close();
    std::cout << get_time_now() << ",finish in " << __FUNCTION__ << std::endl;
}
 
std::stringstream read_file_to_ss(const std::string &file_name)
{
    std::stringstream ss;
    std::fstream r_file(file_name, std::ios::in);
    if (!r_file.is_open())
    {
        std::cout << get_time_now() << " open " << file_name << " failed!" << std::endl;
        return ss;
    }

    std::string line;
    while (getline(r_file, line))
    {
        ss << line;
    }
    r_file.close();
    return ss;
}

void test_populate_ssd(const std::string &src_name, const int &len, const std::string &dest_file)
{
    // std::string file_name1="log1.txt",file_name10="log10.txt",file_name_100="log100.txt";
    // /media/fred/WD_BLACK/log1.txt
    std::stringstream ss = read_file_to_ss(src_name);
    std::fstream w_file(dest_file, std::ios::app);
    std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, _end_time;
    for (int i = 0; i < 10; i++)
    {
        _start_time = std::chrono::high_resolution_clock::now();
        w_file << ss.str();
        _end_time = std::chrono::high_resolution_clock::now();
        std::cout << get_time_now()<<",src file:"<<src_name << ",rows:" << len << ",loop: " << i + 1 << ",length:" << ss.str().length()<<std::endl;
        std::cout << ",Time cost:"
                  << std::chrono::duration_cast<std::chrono::seconds>(_end_time - _start_time).count() << " seconds,"
                  << std::chrono::duration_cast<std::chrono::milliseconds>(_end_time - _start_time).count() << " mills,"
                  << std::chrono::duration_cast<std::chrono::microseconds>(_end_time - _start_time).count() << " micros,"
                  << std::chrono::duration_cast<std::chrono::nanoseconds>(_end_time - _start_time).count() << " nanos!" << std::endl;
    }
    std::cout << get_time_now() << ",finish len:" << len << std::this_thread::get_id() << std::endl;
}

void test_populate_ssd_1_10_100()
{
    std::cout << "1000000" << std::endl;
    test_populate_ssd("log1.txt", 1000000, "/media/fred/WD_BLACK/log1.txt");
    std::cout << std::endl
              << std::endl;
    std::cout << "10000000" << std::endl;
    test_populate_ssd("log10.txt", 10000000, "/media/fred/WD_BLACK/log10.txt");
    std::cout << std::endl
              << std::endl;
    std::cout << "100000000" << std::endl;
    test_populate_ssd("log100.txt", 1000000, "/media/fred/WD_BLACK/log100.txt");
    std::cout << std::endl
              << std::endl;
              std::cout << get_time_now() << ",finish in:" << __FUNCTION__<<","<< std::this_thread::get_id() << std::endl;
}

void test_populate_disk_1_10_100()
{
    std::cout << "1000000" << std::endl;
    test_populate_ssd("log1.txt", 1000000, "/media/fred/Elements/log1.txt");
    std::cout << std::endl
              << std::endl;
    std::cout << "10000000" << std::endl;
    test_populate_ssd("log10.txt", 10000000, "/media/fred/Elements/log10.txt");
    std::cout << std::endl
              << std::endl;
    std::cout << "100000000" << std::endl;
    test_populate_ssd("log100.txt", 1000000, "/media/fred/Elements/log100.txt");
    std::cout << std::endl
              << std::endl;
              std::cout << get_time_now() << ",finish in:" << __FUNCTION__<<","<< std::this_thread::get_id() << std::endl;
}

int main(int args, char **argv)
{ 
    ///media/fred/Elements
    // log_file(argv[1], atoi(argv[2]));
    // test_populate_ssd_1_10_100();
    test_populate_disk_1_10_100();
    std::cout << get_time_now() << ",finish in " << __FUNCTION__ << std::endl;
}

 

 


 

标签:std,now,chrono,write,include,time,ssd,disk,start
From: https://www.cnblogs.com/Fred1987/p/17459330.html

相关文章

  • GaussDB数据库事务介绍
    前言随着大数据和互联网技术的不断发展,数据库管理系统的作用越来越重要,实现数据的快速读写以及保证数据的安全性和完整性成为企业在选择数据库技术时最为关注的问题之一。事务是保证数据一致性和完整性的关键机制之一,因此事务管理在数据库技术中占据了极为重要的位置。在这里我们将......
  • 再识华为云数据库——GaussDB
    前言华为云数据库GaussDB是一款拥有云上高可用,高可靠,高安全,弹性伸缩,一键部署,快速备份恢复,监控告警等关键能力,能为企业提供功能全面,稳定可靠,扩展性强,性能优越的企业级数据库服务。同时具有PB级海量数据存储、实时高效访问、自动化运维等特点,广泛应用于金融、电信、物流、电商、政体......
  • C#读写锁ReaderWriterLockSlim的使用
    读写锁的概念很简单,允许多个线程同时获取读锁,但同一时间只允许一个线程获得写锁,因此也称作共享-独占锁。在C#中,推荐使用ReaderWriterLockSlim类来完成读写锁的功能。某些场合下,对一个对象的读取次数远远大于修改次数,如果只是简单的用lock方式加锁,则会影响读取的效率。而如果采用读......
  • GaussDB存储过程介绍
    前言华为云数据库GaussDB是一款高性能、高安全性的云原生数据库,在数据库领域处于领先地位。而在GaussDB中,存储过程是一个不容忽视的重要功能。本文将深入介绍GaussDB存储过程的使用场景、使用优缺点、示例及示例解析、调用方法等方面,为读者提供全方位的指导与帮助。存储过程是一个......
  • GaussDB如何给世界一个更优选择?
    华为云CEO张平安11月7日,华为全联接大会2022第一天,华为云CEO张平安在主题演讲中,专门有一页PPT谈到了GaussDB信息量很大,不仅特别强调“GaussDB云原生交易数据库,给世界一个更优选择”,同时,还分享了2个案例和一些数据。华为云特别强调“云原生交易数据库”而非常规的“云原生数据......
  • GaussDB数据类型介绍
    GaussDB数据库GaussDB是华为基于openGauss自研生态推出的云化企业级分布式关系型数据库,它支持多种数据类型,包括数值、字符、日期等。在使用GaussDB时,可能需要进行数据类型转换,以满足不同的需求。本文将以示例的形式罗列并介绍一些常见的数据类型转换方法等。     数据类型......
  • write的奥秘
    write在Linux下我们在使用设备的时候,都会用到write这个函数,通过这个函数我们可以象使用文件那样向设备传送数据。可是为什么用户使用write函数就可以把数据写到设备里面去,这个过程到底是怎么实现的呢?这个奥秘就在于设备驱动程序的write实现中,这里我结......
  • 驱动开发:内核实现SSDT挂钩与摘钩
    在前面的文章《驱动开发:内核解析PE结构导出表》中我们封装了两个函数KernelMapFile()函数可用来读取内核文件,GetAddressFromFunction()函数可用来在导出表中寻找指定函数的导出地址,本章将以此为基础实现对特定SSDT函数的Hook挂钩操作,与《驱动开发:内核层InlineHook挂钩函数》所使用......
  • p4 FileReader 和 FileWriter
    FileReader和FileWriter一、FileReader和FileWriter介绍FileReader和FileWriter是字符流,即按照字符来操作io二、FileReader相关方法构造方法摘要ConstructorandDescriptionFileReader(Filefile)创建一个新的FileReader,给出File读取。FileR......
  • 2018WEB安全测试秋季预选赛WriteUp
    0x01input传送门:http://114.55.36.69:8003/题目上说前三道题目是容易的,于是就从容易的题目入手,为了拿到1血,手速飞快地点,emmm,一紧张忘了js输出语句怎么写了,百度后才发现,自己有多蠢alert啊!进入网址,发现一个输入框,查看源码,发现id="flag",后面有一段js代码<script>functionchec......