Skip to content

Instantly share code, notes, and snippets.

@Laro88
Created January 27, 2021 12:34
Show Gist options
  • Save Laro88/ec2cd952723bcbb829afc63658eb03e8 to your computer and use it in GitHub Desktop.
Save Laro88/ec2cd952723bcbb829afc63658eb03e8 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
#include <pqxx/pqxx>
std::basic_string<std::byte> randomizer(int size)
{
std::basic_string<std::byte> n;
for(int j = 0; j < size; ++j)
{
std::byte b;
b = (std::byte)(std::rand() % 255);
n.push_back(b);
};
return n;
}
int main()
{
const std::string connstr = "user=postgres dbname=octopus password=postgres";
try
{
{
pqxx::connection c(connstr);
pqxx::work w(c);
w.exec("DROP TABLE iF EXISTS test_streamto");
w.exec("CREATE TABLE test_streamto ( "
"idx serial primary KEY, "
"strx character varying(5) not NULL, "
"datax BYTEA"
");");
w.commit();
std::cout << "recreated table" << std::endl;
}
//10K rows with 10K bytes gives a table size of 119 megabytes in ~20 seconds on a 2.4GHz I7 9th gen in debug mode (4 seconds in release mode)
const int LOOPCOUNT = 10000;
{
pqxx::connection c(connstr);
pqxx::work w(c);
pqxx::stream_to stream_to{ w
, "test_streamto"
, std::vector<std::string>{"strx", "datax"} };
for(int j = 0; j < LOOPCOUNT; ++j)
{
std::string text = "t" + std::to_string(j);
std::tuple<std::string, std::basic_string<std::byte>> data(text, randomizer(10000));
stream_to << data;
}
stream_to.complete();
w.commit();
}
{
pqxx::connection c(connstr);
pqxx::work w(c);
pqxx::result r = w.exec("SELECT COUNT (idx) AS cnt FROM test_streamto");
w.commit();
int cnt = r[0]["cnt"].as<int>();
if(LOOPCOUNT != cnt)
{
throw "damn stream_to thingomobob";
}
}
}
catch(const std::exception& e)
{
std::cout << e.what();
return -1;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment