首页 > 数据库 >pgsql create table,cpp fill psql table via the third party library pqxx

pgsql create table,cpp fill psql table via the third party library pqxx

时间:2023-10-24 23:35:41浏览次数:38  
标签:psql std now via chrono null time table include

//create table t1;
create table t1(id bigserial not null primary key,author varchar(40) not null,comment varchar(40) not null,content varchar(40) not null,header varchar(40) not null,isbn varchar(40) not null,object varchar(40) not null,summary varchar(40) not null);

 

//main.cpp
#include <algorithm>
#include <barrier>
#include <chrono>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <memory>
#include <map>
#include <queue>
#include <random>
#include <sstream>
#include <thread>
#include <time.h>
#include <uuid/uuid.h>
#include <vector>
#include <pqxx/pqxx>

std::string get_time_now(bool is_exact = true)
{
    std::chrono::time_point<std::chrono::high_resolution_clock> 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::stringstream ss;
    ss << std::put_time(&tm_info, "%Y%m%d%H%M%S");
    if (is_exact)
    {
        auto 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());
        ss << "_"
           << 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[37];
uint32_t rand32()
{
    return ((rand() & 0x3) << 30) | ((rand() & 0x7fff) << 15) | (rand() & 0x7fff);
}

char *gen_uuid4(size_t len)
{
    int n = snprintf(uuid_value, len, "%08x-%04x-%04x-%04x-%04x%08x",
                     rand32(),                       // Generates a 32-bit Hex number
                     rand32() & 0xffff,              // Generates a 16-bit Hex number
                     ((rand32() & 0x0fff) | 0x4000), // Generates a 16-bit Hex number of the form 4xxx (4 indicates the UUID version)
                     (rand32() & 0x3fff) + 0x8000,   // Generates a 16-bit Hex number in the range [0x8000, 0xbfff]
                     rand32() & 0xffff, rand32());   // Generates a 48-bit Hex number
    // return n >= 0 && n < len;             // Success only when snprintf result is a positive number and the provided buffer was large enough.
    return uuid_value;
}

void insert_into_pqsql_t1(const int &loops)
{
    try
    {
        std::uint64_t num = 0;
        std::stringstream ss;
        std::string insert_sql;
        int last_comma_idx = -1;
        std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, _end_time;
        for (int loop = 0; loop < loops; ++loop)
        {
            pqxx::connection conn("dbname=db user=fred password=Fred0001!");
            pqxx::work trans(conn);
            ss = std::stringstream();
            srand(time(NULL));
            _start_time = std::chrono::high_resolution_clock::now();
            ss << "insert into t1(author,comment,content,header,isbn,object,summary) values ";
            for (int i = 0; i < 3000000; i++)
            {
                ++num;
                ss << "('" << gen_uuid4(37) << "','" << gen_uuid4(37) << "','" << gen_uuid4(37)
                   << "','" << gen_uuid4(37) << "','" << gen_uuid4(37) << "','" << gen_uuid4(37) << "','" << gen_uuid4(37) << "'),";
            }
            last_comma_idx = ss.str().find_last_of(",");
            if (last_comma_idx > 0)
            {
                std::cout << typeid(last_comma_idx).name() << " " << last_comma_idx << std::endl;
                insert_sql = ss.str();
                ss = std::stringstream();
                insert_sql = insert_sql.substr(0, last_comma_idx);
                // std::cout << insert_sql << std::endl;
                pqxx::result res = trans.exec(insert_sql);
                trans.commit();
                _end_time = std::chrono::high_resolution_clock::now();
                std::cout << get_time_now() << ",Num:" << num << ",loops:" << loop + 1 << ",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() << ",finished thread id:" << std::this_thread::get_id() << " line: " << __LINE__ << " of " << __FUNCTION__ << std::endl;
    }
    catch (const std::exception &e)
    {
        std::cerr << e.what() << '\n';
    }
}

void thread_insert_into_t1(const int &loops)
{
    std::thread t1(insert_into_pqsql_t1, std::cref(loops));
    t1.join();
}

int main(int args, char **argv)
{
    thread_insert_into_t1(atoi(argv[1]));
    std::cout << get_time_now() << ",finished in " << __LINE__ << " of " << __FUNCTION__ << std::endl;
}
//compile 
g++-13 -std=c++23 -I. main.cpp -luuid -lpthread -libpqxx -o h1;
//run 
nohup ./h1 10000 >>write.log |tail -f write.log

 

 

 

select * from t1 order by id desc limit 1;

 

 

 

 

标签:psql,std,now,via,chrono,null,time,table,include
From: https://www.cnblogs.com/Fred1987/p/17786009.html

相关文章

  • Ultrabook™ and Tablet Windows* 8 Sensors Development Guide
    MicrosoftWindows*8Desktop  Sensors  Laptop  Tablet  MicrosoftWindows*8IntroductionThisguidegivesdevelopersanoverviewoftheMicrosoftWindows8.1sensorsapplicationprogramminginterfaces(APIs)forWindows8.1DesktopandWindowsStore......
  • list集合,dataTable 转json null转空字符串,时间格式
    usingNewtonsoft.Json;usingSystem;usingSystem.Collections.Generic;usingSystem.Data;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceUtils{publicclassNewtonsoftHelper{publicstaticstringToJson<......
  • vue实现用Element Table 展现数据T图
    vue实现用ElementTable展现数据T图,废话不多少,直接上干货<template> <div> <el-table:data="resultTable"style="width:100%"> <el-table-columnprop="id"label="日期"width="180"> </el-table......
  • Element-UI的Table表头合并
    一、效果图1、原图效果2、目标效果二、实现思路1、表头第一行的第一列占零格,表头第一行的第二列占两格2、表头第一行的第一列隐藏三、完整代码<el-table:header-cell-style="headerStyle">headerStyle({row,column,rowIndex,columnIndex}){if(rowIndex===0){row......
  • iptables 初始化(docker)
    iptables-Fiptables-Xiptables-ZiptablesFORWARD-PACCEPTiptables-tnat-Fiptables-tnat-Xiptables-tnat-Zyuminstalliptables-services serviceiptablessave......
  • 使用 DDPO 在 TRL 中微调 Stable Diffusion 模型
    引言扩散模型(如DALL-E2、StableDiffusion)是一类文生图模型,在生成图像(尤其是有照片级真实感的图像)方面取得了广泛成功。然而,这些模型生成的图像可能并不总是符合人类偏好或人类意图。因此出现了对齐问题,即如何确保模型的输出与人类偏好(如“质感”)一致,或者与那种难......
  • 学会 CompletableFuture:让你的代码免受阻塞之苦!
    来源:https://juejin.cn/post/6844904024332828685写在前面通过阅读本篇文章你将了解到:CompletableFuture的使用CompletableFure异步和同步的性能测试已经有了Future为什么仍需要在JDK1.8中引入CompletableFutureCompletableFuture的应用场景对CompletableFuture的使用优化......
  • C. Colorful Table
    C.ColorfulTable设p1为最左边的a[p1]>=i,p2为最右边的a[p2]>=i,则i的面积大小为行的p1-p2,列的p1-p2,大小为2*(p2-p1+1)但是如果暴力的去求每个点的左右端点,肯定会超时,有没有办法优化呢?1.我们想到,大的数一定包含小的数:如果大的数算出来了,那么比他小的数一定也满足条件,可以递推2.......
  • iptables
    iptables是Linux系统下的防火墙工具,可以用于配置和管理网络访问规则。以下是iptables的50条常用命令:查看当前防火墙规则:iptables-L清空所有防火墙规则:iptables-F允许所有本地回环接口的访问:iptables-AINPUT-ilo-jACCEPT允许已建立的连接进入:iptables-AINPUT-mstate......
  • 【大揭秘】美团面试题:ConcurrentHashMap和Hashtable有什么区别?一文解析!
    正文亲爱的小伙伴们,大家好!我是小米,一个热爱技术分享的程序员,今天我为大家带来了一篇有关美团面试题的热门话题:ConcurrentHashMap和Hashtable有什么区别。这个问题在Java面试中常常被拿来考察对多线程编程的理解,所以务必认真学习,不仅仅是为了通过面试,更是为了提高自己在多线程编......