Flutter Engine
The Flutter Engine
JSONBench.cpp
Go to the documentation of this file.
1/*
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
8#include "bench/Benchmark.h"
11#include "src/utils/SkJSON.h"
12
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
26
27 void onPerCanvasPreDraw(SkCanvas*) override {
29 if (!fData) {
30 SkDebugf("!! Could not open bench file: %s\n", kBenchFile);
31 }
32 }
33
34 void onPerCanvasPostDraw(SkCanvas*) override {
35 fData = nullptr;
36 }
37
38 void onDraw(int loops, SkCanvas*) override {
39 if (!fData) return;
40
41 for (int i = 0; i < loops; i++) {
42 skjson::DOM dom(static_cast<const char*>(fData->data()), fData->size());
43 if (dom.root().is<skjson::NullValue>()) {
44 SkDebugf("!! Parsing failed.\n");
45 return;
46 }
47 }
48 }
49
50private:
51 sk_sp<SkData> fData;
52
53 using INHERITED = Benchmark;
54};
55
56DEF_BENCH( return new JsonBench; )
57
58#if (0)
59
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 == Backend::kNonRendering; }
69
70 void onPerCanvasPreDraw(SkCanvas*) override {
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 == Backend::kNonRendering; }
121
122 void onPerCanvasPreDraw(SkCanvas*) override {
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
#define DEF_BENCH(code)
Definition: Benchmark.h:20
const char * backend
SkAssertResult(font.textToGlyphs("Hello", 5, SkTextEncoding::kUTF8, glyphs, std::size(glyphs))==count)
static constexpr const char * kBenchFile
Definition: JSONBench.cpp:16
#define SkASSERT(cond)
Definition: SkAssert.h:116
void SK_SPI SkDebugf(const char format[],...) SK_PRINTF_LIKE(1
#define INHERITED(method,...)
Definition: SkRecorder.cpp:128
virtual void onPerCanvasPostDraw(SkCanvas *)
Definition: Benchmark.h:103
virtual void onDraw(int loops, SkCanvas *)=0
virtual bool isSuitableFor(Backend backend)
Definition: Benchmark.h:55
virtual void onPerCanvasPreDraw(SkCanvas *)
Definition: Benchmark.h:102
virtual const char * onGetName()=0
bool isSuitableFor(Backend backend) override
Definition: JSONBench.cpp:25
const char * onGetName() override
Definition: JSONBench.cpp:23
void onPerCanvasPreDraw(SkCanvas *) override
Definition: JSONBench.cpp:27
void onPerCanvasPostDraw(SkCanvas *) override
Definition: JSONBench.cpp:34
void onDraw(int loops, SkCanvas *) override
Definition: JSONBench.cpp:38
static sk_sp< SkData > MakeUninitialized(size_t length)
Definition: SkData.cpp:116
const void * data() const
Definition: SkData.h:37
static sk_sp< SkData > MakeWithCopy(const void *data, size_t length)
Definition: SkData.cpp:111
static sk_sp< SkData > MakeFromFileName(const char path[])
Definition: SkData.cpp:148
size_t size() const
Definition: SkData.h:30
static std::unique_ptr< SkStreamAsset > MakeFromFile(const char path[])
Definition: SkStream.cpp:922
Definition: dom.py:1
std::shared_ptr< const fml::Mapping > data
Definition: texture_gles.cc:63