github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/benchmark/benchmark_instances.h (about)

     1  // Copyright (C) 2018 go-nebulas authors
     2  //
     3  // This file is part of the go-nebulas library.
     4  //
     5  // the go-nebulas library is free software: you can redistribute it and/or
     6  // modify
     7  // it under the terms of the GNU General Public License as published by
     8  // the Free Software Foundation, either version 3 of the License, or
     9  // (at your option) any later version.
    10  //
    11  // the go-nebulas library is distributed in the hope that it will be useful,
    12  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  // GNU General Public License for more details.
    15  //
    16  // You should have received a copy of the GNU General Public License
    17  // along with the go-nebulas library.  If not, see
    18  // <http://www.gnu.org/licenses/>.
    19  //
    20  
    21  #pragma once
    22  #include "common/common.h"
    23  #include "util/singleton.h"
    24  #include <unordered_set>
    25  
    26  namespace neb {
    27  class benchmark_instance_base {
    28  public:
    29    virtual std::string get_fixture_name() = 0;
    30    virtual std::string get_instance_name() = 0;
    31    virtual void run() const = 0;
    32  }; // end class benchmark_instance_base
    33  
    34  typedef std::shared_ptr<benchmark_instance_base> benchmark_instance_base_ptr;
    35  
    36  class benchmark_instances : public util::singleton<benchmark_instances> {
    37  public:
    38    void init_benchmark_instances(int argc, char *argv[]);
    39  
    40    virtual ~benchmark_instances();
    41  
    42    int run_all_benchmarks();
    43  
    44    size_t register_benchmark(const benchmark_instance_base_ptr &b);
    45  
    46  protected:
    47    void show_all_benchmarks();
    48    void parse_all_enabled_fixtures(const std::string &fixture_name);
    49  
    50  protected:
    51    std::vector<benchmark_instance_base_ptr> m_all_instances;
    52    size_t m_eval_count;
    53    std::string m_output_fp;
    54    std::unordered_set<std::string> m_enabled_fixtures;
    55  }; // end class benchmark_instances
    56  }
    57  
    58  #define GEN_NAME_VAR(name) _##name##_nouse
    59  #define BENCHMARK(fixture, name)                                               \
    60    class name : public neb::benchmark_instance_base {                           \
    61    public:                                                                      \
    62      virtual std::string get_fixture_name() { return #fixture; }                \
    63      virtual std::string get_instance_name() { return #name; }                  \
    64      virtual void run() const;                                                  \
    65    };                                                                           \
    66    static int GEN_NAME_VAR(name) =                                              \
    67        neb::benchmark_instances::instance().register_benchmark(                 \
    68            std::static_pointer_cast<neb::benchmark_instance_base>(              \
    69                std::make_shared<name>()));                                      \
    70    void name::run() const
    71