blob: 061316df20a380681ad67680938dc49753f2aa84 [file] [log] [blame]
Florin Malita7796f002018-06-08 12:25:38 -04001/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "bench/Benchmark.h"
9#include "include/core/SkData.h"
10#include "include/core/SkStream.h"
11#include "src/utils/SkJSON.h"
Florin Malita7796f002018-06-08 12:25:38 -040012
13#if defined(SK_BUILD_FOR_ANDROID)
14static constexpr const char* kBenchFile = "/data/local/tmp/bench.json";
15#else
16static constexpr const char* kBenchFile = "/tmp/bench.json";
17#endif
18
19class JsonBench : public Benchmark {
20public:
21
22protected:
23 const char* onGetName() override { return "json_skjson"; }
24
25 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
26
27 void onPerCanvasPreDraw(SkCanvas*) override {
Florin Malitafedfd542018-06-14 15:03:21 -040028 fData = SkData::MakeFromFileName(kBenchFile);
29 if (!fData) {
Florin Malita7796f002018-06-08 12:25:38 -040030 SkDebugf("!! Could not open bench file: %s\n", kBenchFile);
31 }
32 }
33
34 void onPerCanvasPostDraw(SkCanvas*) override {
Florin Malitafedfd542018-06-14 15:03:21 -040035 fData = nullptr;
Florin Malita7796f002018-06-08 12:25:38 -040036 }
37
38 void onDraw(int loops, SkCanvas*) override {
Florin Malitafedfd542018-06-14 15:03:21 -040039 if (!fData) return;
Florin Malita7796f002018-06-08 12:25:38 -040040
41 for (int i = 0; i < loops; i++) {
Florin Malitafedfd542018-06-14 15:03:21 -040042 skjson::DOM dom(static_cast<const char*>(fData->data()), fData->size());
Florin Malita7796f002018-06-08 12:25:38 -040043 if (dom.root().is<skjson::NullValue>()) {
44 SkDebugf("!! Parsing failed.\n");
45 return;
46 }
47 }
48 }
49
50private:
Florin Malitafedfd542018-06-14 15:03:21 -040051 sk_sp<SkData> fData;
Florin Malita7796f002018-06-08 12:25:38 -040052
53 using INHERITED = Benchmark;
54};
55
56DEF_BENCH( return new JsonBench; )
57
Florin Malita7d42c442018-06-14 16:16:01 -040058#if (0)
Florin Malita7796f002018-06-08 12:25:38 -040059
60#include "rapidjson/document.h"
61
62class RapidJsonBench : public Benchmark {
63public:
64
65protected:
66 const char* onGetName() override { return "json_rapidjson"; }
67
68 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
69
70 void onPerCanvasPreDraw(SkCanvas*) override {
71 if (auto stream = SkStream::MakeFromFile(kBenchFile)) {
72 SkASSERT(stream->hasLength());
73 fCStringData = SkData::MakeUninitialized(stream->getLength() + 1);
74 auto* data8 = reinterpret_cast<uint8_t*>(fCStringData->writable_data());
75 SkAssertResult(stream->read(data8, stream->getLength()) == stream->getLength());
76 data8[stream->getLength()] = '\0';
77
78 } else {
79 SkDebugf("!! Could not open bench file: %s\n", kBenchFile);
80 }
81 }
82
83 void onPerCanvasPostDraw(SkCanvas*) override {
84 fCStringData = nullptr;
85 }
86
87 void onDraw(int loops, SkCanvas*) override {
88 if (!fCStringData) return;
89
90 for (int i = 0; i < loops; i++) {
91 rapidjson::Document doc;
92 doc.Parse(static_cast<const char*>(fCStringData->data()));
93 if (doc.HasParseError()) {
94 SkDebugf("!! Parsing failed.\n");
95 return;
96 }
97 }
98 }
99
100private:
101 sk_sp<SkData> fCStringData;
102
103 using INHERITED = Benchmark;
104};
105
106DEF_BENCH( return new RapidJsonBench; )
107
108#endif
109
110#if (0)
111
112#include "pjson.h"
113
114class PJsonBench : public Benchmark {
115public:
116
117protected:
118 const char* onGetName() override { return "json_pjson"; }
119
120 bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; }
121
122 void onPerCanvasPreDraw(SkCanvas*) override {
123 if (auto stream = SkStream::MakeFromFile(kBenchFile)) {
124 SkASSERT(stream->hasLength());
125 fCStringData = SkData::MakeUninitialized(stream->getLength() + 1);
126 auto* data8 = reinterpret_cast<uint8_t*>(fCStringData->writable_data());
127 SkAssertResult(stream->read(data8, stream->getLength()) == stream->getLength());
128 data8[stream->getLength()] = '\0';
129
130 } else {
131 SkDebugf("!! Could not open bench file: %s\n", kBenchFile);
132 }
133 }
134
135 void onPerCanvasPostDraw(SkCanvas*) override {
136 fCStringData = nullptr;
137 }
138
139 void onDraw(int loops, SkCanvas*) override {
140 if (!fCStringData) return;
141
142 for (int i = 0; i < loops; i++) {
143 // Copy needed for in-place operation.
144 auto data = SkData::MakeWithCopy(fCStringData->data(), fCStringData->size());
145 pjson::document doc;
146 if (!doc.deserialize_in_place(static_cast<char*>(data->writable_data()))) {
147 SkDebugf("!! Parsing failed.\n");
148 return;
149 }
150 }
151 }
152
153private:
154 sk_sp<SkData> fCStringData;
155
156 using INHERITED = Benchmark;
157};
158
159DEF_BENCH( return new PJsonBench; )
160
161#endif