1 | // |
---|---|
2 | // Benchmark.cpp |
3 | // |
4 | // This sample shows a benchmark of the JSON parser. |
5 | // |
6 | // Copyright (c) 2012, Applied Informatics Software Engineering GmbH. |
7 | // and Contributors. |
8 | // |
9 | // SPDX-License-Identifier: BSL-1.0 |
10 | // |
11 | |
12 | |
13 | #include "Poco/JSON/Parser.h" |
14 | #include "Poco/JSON/ParseHandler.h" |
15 | #include "Poco/JSON/JSONException.h" |
16 | #include "Poco/Environment.h" |
17 | #include "Poco/Path.h" |
18 | #include "Poco/File.h" |
19 | #include "Poco/FileStream.h" |
20 | #include "Poco/StreamCopier.h" |
21 | #include "Poco/Stopwatch.h" |
22 | #include <iostream> |
23 | #include <iomanip> |
24 | |
25 | |
26 | int main(int argc, char** argv) |
27 | { |
28 | Poco::Stopwatch sw; |
29 | |
30 | std::string dir = Poco::Environment::get("POCO_BASE") + "/JSON/samples/Benchmark/"; |
31 | Poco::Path filePath(dir, "input.big.json"); |
32 | |
33 | std::ostringstream ostr; |
34 | |
35 | if (filePath.isFile()) |
36 | { |
37 | Poco::File inputFile(filePath); |
38 | if ( inputFile.exists() ) |
39 | { |
40 | sw.start(); |
41 | Poco::FileInputStream fis(filePath.toString()); |
42 | Poco::StreamCopier::copyStream(fis, ostr); |
43 | sw.stop(); |
44 | } |
45 | else |
46 | { |
47 | std::cout << filePath.toString() << " doesn't exist!"<< std::endl; |
48 | return 1; |
49 | } |
50 | } |
51 | |
52 | std::cout << "JSON Benchmark"<< std::endl; |
53 | std::cout << "=============="<< std::endl; |
54 | |
55 | std::string jsonStr = ostr.str(); |
56 | std::cout << "Total of "<< jsonStr.size() << " bytes,"<< std::endl << "loaded in "<< sw.elapsed() << " [us],"<< std::endl; |
57 | |
58 | std::cout << std::endl << "POCO JSON barebone parse"<< std::endl; |
59 | Poco::JSON::Parser sparser(0); |
60 | sw.restart(); |
61 | sparser.parse(jsonStr); |
62 | sw.stop(); |
63 | std::cout << "---------------------------------"<< std::endl; |
64 | std::cout << "[std::string] parsed in "<< sw.elapsed() << " [us]"<< std::endl; |
65 | std::cout << "---------------------------------"<< std::endl; |
66 | |
67 | Poco::JSON::Parser iparser(0); |
68 | std::istringstream istr(jsonStr); |
69 | sw.restart(); |
70 | iparser.parse(istr); |
71 | sw.stop(); |
72 | std::cout << "----------------------------------------"<< std::endl; |
73 | std::cout << "[std::istringstream] parsed in "<< sw.elapsed() << " [us]"<< std::endl; |
74 | std::cout << "----------------------------------------"<< std::endl; |
75 | |
76 | std::cout << std::endl << "POCO JSON Handle/Stringify"<< std::endl; |
77 | try |
78 | { |
79 | Poco::JSON::Parser sparser; |
80 | sw.restart(); |
81 | sparser.parse(jsonStr); |
82 | Poco::DynamicAny result = sparser.result(); |
83 | sw.stop(); |
84 | std::cout << "-----------------------------------------"<< std::endl; |
85 | std::cout << "[std::string] parsed/handled in "<< sw.elapsed() << " [us]"<< std::endl; |
86 | std::cout << "-----------------------------------------"<< std::endl; |
87 | |
88 | Poco::JSON::Parser isparser; |
89 | std::istringstream istr(jsonStr); |
90 | sw.restart(); |
91 | isparser.parse(istr); |
92 | result = isparser.result(); |
93 | sw.stop(); |
94 | std::cout << "------------------------------------------------"<< std::endl; |
95 | std::cout << "[std::istringstream] parsed/handled in "<< sw.elapsed() << " [us]"<< std::endl; |
96 | std::cout << "------------------------------------------------"<< std::endl; |
97 | |
98 | //Serialize to string |
99 | Poco::JSON::Object::Ptr obj; |
100 | if (result.type() == typeid(Poco::JSON::Object::Ptr)) |
101 | obj = result.extract<Poco::JSON::Object::Ptr>(); |
102 | |
103 | std::ostringstream out; |
104 | sw.restart(); |
105 | obj->stringify(out); |
106 | sw.stop(); |
107 | std::cout << "-----------------------------------"<< std::endl; |
108 | std::cout << "stringified in "<< sw.elapsed() << " [us]"<< std::endl; |
109 | std::cout << "-----------------------------------"<< std::endl; |
110 | std::cout << std::endl; |
111 | } |
112 | catch(Poco::JSON::JSONException jsone) |
113 | { |
114 | std::cout << jsone.message() << std::endl; |
115 | } |
116 | |
117 | return 0; |
118 | } |
119 |