poco/JSON/samples/Benchmark/src/Benchmark.cpp

141 lines
4.7 KiB
C++
Raw Normal View History

2012-11-11 09:57:01 +01:00
//
// Benchmark.cpp
//
// $Id$
//
// This sample shows a benchmark of the JSON parser.
//
// Copyright (c) 2012, Applied Informatics Software Engineering GmbH.
// and Contributors.
//
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
//
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
#include "Poco/JSON/Parser.h"
#include "Poco/JSON/ParseHandler.h"
2012-11-11 09:57:01 +01:00
#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"
2013-03-18 20:59:29 -05:00
#include "Poco/Stopwatch.h"
2012-11-11 09:57:01 +01:00
#include <iostream>
#include <iomanip>
int main(int argc, char** argv)
{
2013-03-18 20:59:29 -05:00
Poco::Stopwatch sw;
2012-11-11 09:57:01 +01:00
std::string dir = Poco::Environment::get("POCO_BASE") + "/JSON/samples/Benchmark/";
Poco::Path filePath(dir, "input.big.json");
2012-11-11 09:57:01 +01:00
std::ostringstream ostr;
2013-03-18 20:59:29 -05:00
if (filePath.isFile())
2012-11-11 09:57:01 +01:00
{
Poco::File inputFile(filePath);
if ( inputFile.exists() )
{
2013-03-18 20:59:29 -05:00
sw.start();
2012-11-11 09:57:01 +01:00
Poco::FileInputStream fis(filePath.toString());
Poco::StreamCopier::copyStream(fis, ostr);
2013-03-18 20:59:29 -05:00
sw.stop();
2012-11-11 09:57:01 +01:00
}
else
{
std::cout << filePath.toString() << " doesn't exist!" << std::endl;
return 1;
}
}
2013-03-18 20:59:29 -05:00
std::cout << "JSON Benchmark" << std::endl;
std::cout << "==============" << std::endl;
2012-11-11 09:57:01 +01:00
std::string jsonStr = ostr.str();
2013-03-18 20:59:29 -05:00
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);
2013-03-18 20:59:29 -05:00
sw.restart();
sparser.parse(jsonStr);
2013-03-18 20:59:29 -05:00
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;
2013-03-18 20:59:29 -05:00
std::cout << std::endl << "POCO JSON Handle/Stringify" << std::endl;
2012-11-11 09:57:01 +01:00
try
{
Poco::JSON::Parser sparser;
sw.restart();
sparser.parse(jsonStr);
Poco::DynamicAny 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);
2013-03-18 20:59:29 -05:00
sw.restart();
isparser.parse(istr);
result = isparser.result();
2013-03-18 20:59:29 -05:00
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;
2012-11-11 09:57:01 +01:00
if ( result.type() == typeid(Poco::JSON::Object::Ptr) )
obj = result.extract<Poco::JSON::Object::Ptr>();
std::ostringstream out;
2013-03-18 20:59:29 -05:00
sw.restart();
2012-11-11 09:57:01 +01:00
obj->stringify(out);
2013-03-18 20:59:29 -05:00
sw.stop();
std::cout << "-----------------------------------" << std::endl;
std::cout << "stringified in " << sw.elapsed() << " [us]" << std::endl;
std::cout << "-----------------------------------" << std::endl;
2012-11-11 09:57:01 +01:00
std::cout << std::endl;
}
catch(Poco::JSON::JSONException jsone)
{
std::cout << jsone.message() << std::endl;
}
return 0;
}