Florin Malita | 7796f00 | 2018-06-08 12:25:38 -0400 | [diff] [blame^] | 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 "Benchmark.h" |
| 9 | #include "SkData.h" |
| 10 | #include "SkJSON.h" |
| 11 | #include "SkStream.h" |
| 12 | |
| 13 | #if defined(SK_BUILD_FOR_ANDROID) |
| 14 | static constexpr const char* kBenchFile = "/data/local/tmp/bench.json"; |
| 15 | #else |
| 16 | static constexpr const char* kBenchFile = "/tmp/bench.json"; |
| 17 | #endif |
| 18 | |
| 19 | class JsonBench : public Benchmark { |
| 20 | public: |
| 21 | |
| 22 | protected: |
| 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 { |
| 28 | if (auto stream = SkStream::MakeFromFile(kBenchFile)) { |
| 29 | SkASSERT(stream->hasLength()); |
| 30 | fCStringData = SkData::MakeUninitialized(stream->getLength() + 1); |
| 31 | auto* data8 = reinterpret_cast<uint8_t*>(fCStringData->writable_data()); |
| 32 | SkAssertResult(stream->read(data8, stream->getLength()) == stream->getLength()); |
| 33 | data8[stream->getLength()] = '\0'; |
| 34 | |
| 35 | } else { |
| 36 | SkDebugf("!! Could not open bench file: %s\n", kBenchFile); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | void onPerCanvasPostDraw(SkCanvas*) override { |
| 41 | fCStringData = nullptr; |
| 42 | } |
| 43 | |
| 44 | void onDraw(int loops, SkCanvas*) override { |
| 45 | if (!fCStringData) return; |
| 46 | |
| 47 | for (int i = 0; i < loops; i++) { |
| 48 | skjson::DOM dom(static_cast<const char*>(fCStringData->data())); |
| 49 | if (dom.root().is<skjson::NullValue>()) { |
| 50 | SkDebugf("!! Parsing failed.\n"); |
| 51 | return; |
| 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | private: |
| 57 | sk_sp<SkData> fCStringData; |
| 58 | |
| 59 | using INHERITED = Benchmark; |
| 60 | }; |
| 61 | |
| 62 | DEF_BENCH( return new JsonBench; ) |
| 63 | |
| 64 | #if !defined(SK_BUILD_FOR_ANDROID_FRAMEWORK) |
| 65 | |
| 66 | #include "rapidjson/document.h" |
| 67 | |
| 68 | class RapidJsonBench : public Benchmark { |
| 69 | public: |
| 70 | |
| 71 | protected: |
| 72 | const char* onGetName() override { return "json_rapidjson"; } |
| 73 | |
| 74 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 75 | |
| 76 | void onPerCanvasPreDraw(SkCanvas*) override { |
| 77 | if (auto stream = SkStream::MakeFromFile(kBenchFile)) { |
| 78 | SkASSERT(stream->hasLength()); |
| 79 | fCStringData = SkData::MakeUninitialized(stream->getLength() + 1); |
| 80 | auto* data8 = reinterpret_cast<uint8_t*>(fCStringData->writable_data()); |
| 81 | SkAssertResult(stream->read(data8, stream->getLength()) == stream->getLength()); |
| 82 | data8[stream->getLength()] = '\0'; |
| 83 | |
| 84 | } else { |
| 85 | SkDebugf("!! Could not open bench file: %s\n", kBenchFile); |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | void onPerCanvasPostDraw(SkCanvas*) override { |
| 90 | fCStringData = nullptr; |
| 91 | } |
| 92 | |
| 93 | void onDraw(int loops, SkCanvas*) override { |
| 94 | if (!fCStringData) return; |
| 95 | |
| 96 | for (int i = 0; i < loops; i++) { |
| 97 | rapidjson::Document doc; |
| 98 | doc.Parse(static_cast<const char*>(fCStringData->data())); |
| 99 | if (doc.HasParseError()) { |
| 100 | SkDebugf("!! Parsing failed.\n"); |
| 101 | return; |
| 102 | } |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | private: |
| 107 | sk_sp<SkData> fCStringData; |
| 108 | |
| 109 | using INHERITED = Benchmark; |
| 110 | }; |
| 111 | |
| 112 | DEF_BENCH( return new RapidJsonBench; ) |
| 113 | |
| 114 | #endif |
| 115 | |
| 116 | #if (0) |
| 117 | |
| 118 | #include "pjson.h" |
| 119 | |
| 120 | class PJsonBench : public Benchmark { |
| 121 | public: |
| 122 | |
| 123 | protected: |
| 124 | const char* onGetName() override { return "json_pjson"; } |
| 125 | |
| 126 | bool isSuitableFor(Backend backend) override { return backend == kNonRendering_Backend; } |
| 127 | |
| 128 | void onPerCanvasPreDraw(SkCanvas*) override { |
| 129 | if (auto stream = SkStream::MakeFromFile(kBenchFile)) { |
| 130 | SkASSERT(stream->hasLength()); |
| 131 | fCStringData = SkData::MakeUninitialized(stream->getLength() + 1); |
| 132 | auto* data8 = reinterpret_cast<uint8_t*>(fCStringData->writable_data()); |
| 133 | SkAssertResult(stream->read(data8, stream->getLength()) == stream->getLength()); |
| 134 | data8[stream->getLength()] = '\0'; |
| 135 | |
| 136 | } else { |
| 137 | SkDebugf("!! Could not open bench file: %s\n", kBenchFile); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void onPerCanvasPostDraw(SkCanvas*) override { |
| 142 | fCStringData = nullptr; |
| 143 | } |
| 144 | |
| 145 | void onDraw(int loops, SkCanvas*) override { |
| 146 | if (!fCStringData) return; |
| 147 | |
| 148 | for (int i = 0; i < loops; i++) { |
| 149 | // Copy needed for in-place operation. |
| 150 | auto data = SkData::MakeWithCopy(fCStringData->data(), fCStringData->size()); |
| 151 | pjson::document doc; |
| 152 | if (!doc.deserialize_in_place(static_cast<char*>(data->writable_data()))) { |
| 153 | SkDebugf("!! Parsing failed.\n"); |
| 154 | return; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | private: |
| 160 | sk_sp<SkData> fCStringData; |
| 161 | |
| 162 | using INHERITED = Benchmark; |
| 163 | }; |
| 164 | |
| 165 | DEF_BENCH( return new PJsonBench; ) |
| 166 | |
| 167 | #endif |