User Tools

Site Tools


Json memory usage

Without json:

  PID USERNAME     THR PRI NICE   SIZE    RES STATE   C   TIME    WCPU COMMAND
10693  ziggi         1  35    0 18640K 14116K nanslp  0   0:00   0.59% json2

With json collector in memory:

  PID USERNAME     THR PRI NICE   SIZE    RES STATE   C   TIME    WCPU COMMAND
 9905  ziggi         1  52    0 30976K 25376K nanslp  2   0:01   1.37% json2

Size of pure result:

$ du -sch out.json
2.4M	out.json

Code

json2.cpp
/*
 * json2.cpp
 *
 * Copyright 2019 Oleg Borodin  <borodin@unix7.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 *
 *
 */
 
#include <iostream>
#include <fstream>
#include <pqxx/pqxx>
#include <nlohmann/json.hpp>
#include <unistd.h>
 
int main(int, char *argv[]) {
 
    try {
        pqxx::connection conn("host=localhost dbname=wcm user=wcm");
        pqxx::work txn(conn);
 
        pqxx::result res = txn.exec(
            "select id, name from customers order by id limit 100000"
        );
        std::cout << "# res size " << res.size() << "\n";
 
        nlohmann::json j;
        j["id"] = "qwerty";
 
        for (auto row: res) {
            j["result"].push_back({
                row.at("id").as<int>(),
                row.at("name").c_str(),
            });
        }
 
        std::ofstream out("out.json");
        out << j;
 
        sleep(1000);
 
    } catch (const pqxx::sql_error &e) {
        std::cerr << "# sql error: " << e.what() << std::endl;
        std::cerr << "# query was: " << e.query() << std::endl;
        return 2;
    } catch (const std::exception &e) {
        std::cerr << "# error: " << e.what() << std::endl;
        return 1;
    }
}

First PagePrevious PageBack to overviewNext PageLast Page