C++ mysql libmysqlcppconn-dev insert into table
#include <chrono> #include <ctime> #include <fstream> #include <iomanip> #include <iostream> #include <map> #include <mutex> #include <random> #include <sstream> #include <thread> #include <uuid/uuid.h> #include <vector> #include <cppconn/driver.h> #include <cppconn/connection.h> #include <cppconn/metadata.h> #include <cppconn/prepared_statement.h> #include <cppconn/resultset.h> #include <cppconn/resultset_metadata.h> 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(); } sql::Connection *get_conn() { sql::Driver *driver = get_driver_instance(); sql::ConnectOptionsMap connection_properties; connection_properties["hostName"] = "localhost"; connection_properties["userName"] = "rumination"; connection_properties["password"] = "Rumination0001!"; connection_properties["schema"] = "db"; connection_properties["port"] = 3306; // connection_properties["OPT_RECONNECT"] = true; sql::Connection *conn = driver->connect(connection_properties); return conn; } uint32_t rand32() { return ((rand() & 0x3) << 30) | ((rand() & 0x7fff) << 15) | (rand() & 0x7fff); } char uuid_value_arr[37]; char *gen_uuid4() { int n = snprintf(uuid_value_arr, sizeof(uuid_value_arr), "%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_arr; } void insert_into_mysql(const int &loops) { sql::Connection *conn = get_conn(); std::cout << std::boolalpha << conn->isValid() << std::endl; sql::Statement *stmt = conn->createStatement(); srand(time(NULL)); std::uint64_t num = 0; std::stringstream ss; std::string sql_str; std::chrono::time_point<std::chrono::high_resolution_clock> _start_time, _end_time; for (int i = 0; i < loops; i++) { _start_time = std::chrono::high_resolution_clock::now(); ss = std::stringstream(); ss << "insert into t1(name,topic,author,isbn) values "; for (int j = 0; j < 1000000; j++) { ss << "('" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "','" << gen_uuid4() << "'),"; ++num; } sql_str = ss.str(); int last_comma_idx = sql_str.find_last_of(","); sql_str = sql_str.substr(0, last_comma_idx); bool is_inserted = stmt->execute(sql_str); _end_time = std::chrono::high_resolution_clock::now(); std::cout << std::boolalpha << get_time_now() << ",is_inserted:" << is_inserted << ",num:" << num << ",loops:" << i + 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; } conn->close(); std::cout << std::boolalpha << conn->isClosed() << std::endl; std::cout << get_time_now() << ",finished in thread id:" << std::this_thread::get_id() << ",line:" << __LINE__ << " of " << __FUNCTION__ << std::endl; } int main(int args, char **argv) { // g++-13 -std=c++23 -I. main.cpp -lpthread -luuid -lmysqlcppconn -o h1; // nohup ./h1 100 >>insert.log|tail -f insert.log; insert_into_mysql(atoi(argv[1])); std::cout << get_time_now() << ",thread id:" << std::this_thread::get_id() << ", finished in line " << __LINE__ << " of " << __FUNCTION__ << std::endl; }
g++-13 -std=c++23 -I. main.cpp -lpthread -luuid -lmysqlcppconn -o h1;
nohup ./h1 100 >>insert.log|tail -f insert.log;