poco/JSON/samples/Benchmark/src/Benchmark.cpp
Matej Kenda f24547cdcf enh(Poco): Mark deprecated functionality with C++ attributes and resolve internal usage of deprecated functions (#4551)
* enh(poco): Replace deprecated comments with C++ deprecated attribute.

* enh(Poco): Replace some deprecated functionality in Poco sources. (#4426)

* enh(Poco): Replace more deprecated functionality in Poco sources. (#4426)

* fix(CMake): Variable BUILD_SHARED_LIBS must be defined properly to create valid binaries.

* enh: Code improvements done while resolving deprecated functionality (#4426)

* Un-deprecate LocalDateTme (#4426)

* enh(Poco): Replace usage of deprecated functionality with other functions/classes (#4426)

* chore(SSL): temporarily un-deprecate SSL-related functionality (#4426)

* chore(SSL): temporarily un-deprecate old MongoDB protocol functionality (#4426)

* enh(Poco): Minor Hash improvements (#4426)

* enh(Foundation): Compile deprecated hash tests only when POCO_TEST_DEPRECATED is enabled (#4426)

* enh(Net): Compile deprecated Socket::select functionality only when POCO_TEST_DEPRECATED is enabled (#4426)

* enh(Bonjour): Replace deprecated Socket::select with PollSet (#4426)

* enh(Poco): Introduce POCO_DEPRECATED macro to have the ability to disable deprecation warnings in applications (#4426)

* test(ODBC): add few asserts to testStoredProcedureDynamicVar

* fix(ODBC): rename DynamicAny -> DynamicVar in tests

* fix(ODBC): make Dignostics static members inline to prevent explicit instantiation warnings on windows

---------

Co-authored-by: Alex Fabijanic <alex@pocoproject.org>
2024-07-29 08:37:35 +02:00

119 lines
3.4 KiB
C++

//
// Benchmark.cpp
//
// This sample shows a benchmark of the JSON parser.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// SPDX-License-Identifier: BSL-1.0
//
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/JSONException.h"
#include "Poco/Environment.h"
#include "Poco/Path.h"
#include "Poco/File.h"
#include "Poco/FileStream.h"
#include "Poco/StreamCopier.h"
#include "Poco/Stopwatch.h"
#include <iostream>
#include <sstream>
int main(int argc, char** argv)
{
Poco::Stopwatch sw;
std::string dir = Poco::Environment::get("POCO_BASE") + "/JSON/samples/Benchmark/";
Poco::Path filePath(dir, "input.big.json");
std::ostringstream ostr;
if (filePath.isFile())
{
Poco::File inputFile(filePath);
if ( inputFile.exists() )
{
sw.start();
Poco::FileInputStream fis(filePath.toString());
Poco::StreamCopier::copyStream(fis, ostr);
sw.stop();
}
else
{
std::cout << filePath.toString() << " doesn't exist!" << std::endl;
return 1;
}
}
std::cout << "JSON Benchmark" << std::endl;
std::cout << "==============" << std::endl;
std::string jsonStr = ostr.str();
std::cout << "Total of " << jsonStr.size() << " bytes," << std::endl << "loaded in " << sw.elapsed() << " [us]," << std::endl;
std::cout << std::endl << "POCO JSON barebone parse" << std::endl;
Poco::JSON::Parser sparser(0);
sw.restart();
sparser.parse(jsonStr);
sw.stop();
std::cout << "---------------------------------" << std::endl;
std::cout << "[std::string] parsed in " << sw.elapsed() << " [us]" << std::endl;
std::cout << "---------------------------------" << std::endl;
Poco::JSON::Parser iparser(0);
std::istringstream istr(jsonStr);
sw.restart();
iparser.parse(istr);
sw.stop();
std::cout << "----------------------------------------" << std::endl;
std::cout << "[std::istringstream] parsed in " << sw.elapsed() << " [us]" << std::endl;
std::cout << "----------------------------------------" << std::endl;
std::cout << std::endl << "POCO JSON Handle/Stringify" << std::endl;
try
{
Poco::JSON::Parser sparser;
sw.restart();
sparser.parse(jsonStr);
Poco::Dynamic::Var result = sparser.result();
sw.stop();
std::cout << "-----------------------------------------" << std::endl;
std::cout << "[std::string] parsed/handled in " << sw.elapsed() << " [us]" << std::endl;
std::cout << "-----------------------------------------" << std::endl;
Poco::JSON::Parser isparser;
std::istringstream istr(jsonStr);
sw.restart();
isparser.parse(istr);
result = isparser.result();
sw.stop();
std::cout << "------------------------------------------------" << std::endl;
std::cout << "[std::istringstream] parsed/handled in " << sw.elapsed() << " [us]" << std::endl;
std::cout << "------------------------------------------------" << std::endl;
//Serialize to string
Poco::JSON::Object::Ptr obj;
if ( result.type() == typeid(Poco::JSON::Object::Ptr) )
obj = result.extract<Poco::JSON::Object::Ptr>();
std::ostringstream out;
sw.restart();
obj->stringify(out);
sw.stop();
std::cout << "-----------------------------------" << std::endl;
std::cout << "stringified in " << sw.elapsed() << " [us]" << std::endl;
std::cout << "-----------------------------------" << std::endl;
std::cout << std::endl;
}
catch(Poco::JSON::JSONException& jsone)
{
std::cout << jsone.message() << std::endl;
}
return 0;
}