Home | History | Annotate | Download | only in bench
      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         fData = SkData::MakeFromFileName(kBenchFile);
     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 
     50 private:
     51     sk_sp<SkData> fData;
     52 
     53     using INHERITED = Benchmark;
     54 };
     55 
     56 DEF_BENCH( return new JsonBench; )
     57 
     58 #if (0)
     59 
     60 #include "rapidjson/document.h"
     61 
     62 class RapidJsonBench : public Benchmark {
     63 public:
     64 
     65 protected:
     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 
    100 private:
    101     sk_sp<SkData> fCStringData;
    102 
    103     using INHERITED = Benchmark;
    104 };
    105 
    106 DEF_BENCH( return new RapidJsonBench; )
    107 
    108 #endif
    109 
    110 #if (0)
    111 
    112 #include "pjson.h"
    113 
    114 class PJsonBench : public Benchmark {
    115 public:
    116 
    117 protected:
    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 
    153 private:
    154     sk_sp<SkData> fCStringData;
    155 
    156     using INHERITED = Benchmark;
    157 };
    158 
    159 DEF_BENCH( return new PJsonBench; )
    160 
    161 #endif
    162