github.com/igggame/nebulas-go@v2.1.0+incompatible/nbre/test/jit/test_jit_driver.cpp (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 // GNU General
    14  // 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  #include "common/address.h"
    22  #include "common/byte.h"
    23  #include "jit/jit_driver.h"
    24  #include <fstream>
    25  #include <random>
    26  #include <sstream>
    27  
    28  void randomly_gen_irs(const std::string &ir_path,
    29                        std::vector<nbre::NBREIR> &irs) {
    30    std::string name = std::to_string(std::rand());
    31    uint64_t version = std::rand();
    32    uint64_t height = std::rand();
    33  
    34    std::ifstream ifs;
    35    ifs.open(ir_path.c_str(), std::ios::in | std::ios::binary);
    36    ifs.seekg(0, ifs.end);
    37    std::ifstream::pos_type size = ifs.tellg();
    38  
    39    neb::bytes buf(size);
    40  
    41    ifs.seekg(0, ifs.beg);
    42    ifs.read((char *)buf.value(), buf.size());
    43  
    44    nbre::NBREIR ir;
    45    ir.set_name(name);
    46    ir.set_version(version);
    47    ir.set_height(height);
    48    ir.set_ir(neb::byte_to_string(buf));
    49  
    50    irs.push_back(ir);
    51  }
    52  
    53  int main(int argc, char *argv[]) {
    54  
    55    std::string ir_path = argv[1];
    56    std::string func_name = argv[2];
    57  
    58    int32_t n;
    59    std::cin >> n;
    60  
    61    while (n--) {
    62      std::vector<nbre::NBREIR> irs;
    63      randomly_gen_irs(ir_path, irs);
    64  
    65      auto &ele = irs.front();
    66      std::stringstream ss;
    67      ss << ele.name() << ele.version();
    68  
    69      auto &jd = neb::jit_driver::instance();
    70      jd.run<std::string>(ss.str(), irs, func_name, ss.str());
    71  
    72      if (!(n % 100)) {
    73        std::cout << n << std::endl;
    74      }
    75    }
    76  
    77    return 0;
    78  }