sdk_test.cc 2.5 KB
Newer Older
S
Superjom 已提交
1 2 3 4 5 6 7
#include "visualdl/logic/sdk.h"

#include <gtest/gtest.h>

namespace visualdl {

struct ScalarTestHelper {
S
superjom 已提交
8 9 10 11
  ImHelper _rim;
  ImHelper _wim;
  ImHelper rim = _rim.AsMode("train");
  ImHelper wim = _wim.AsMode("train");
12
  const std::string dir = "./tmp/sdk_test.test";
S
Superjom 已提交
13 14 15 16 17

  void operator()(std::function<void()> read, std::function<void()> write) {
    wim.StartWriteSerice(dir, 200);
    write();

18 19 20 21
    // should wait for the write service create log's path
    std::this_thread::sleep_for(std::chrono::milliseconds(400));
    rim.StartReadService(dir, 100);
    // should wait for the read service to load "tag0" tablet into memory
S
Superjom 已提交
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    std::this_thread::sleep_for(std::chrono::milliseconds(600));
    read();
  }
};

TEST(Scalar, set_caption) {
  ScalarTestHelper helper;

  const std::vector<std::string> captions({"train", "test"});

  auto write = [&] {
    auto tablet = helper.wim.AddTablet("tag0", -1);
    components::ScalarHelper<float> scalar(tablet, &helper.wim.handler());

    scalar.SetCaptions(captions);
  };

  auto read = [&] {
    auto mytablet = helper.rim.tablet("tag0");
    components::ScalarHelper<float> myscalar(mytablet, &helper.rim.handler());
    auto mycaptions = myscalar.GetCaptions();

    ASSERT_EQ(captions, mycaptions);
  };

  helper(read, write);
}

TEST(Scalar, add_records) {
  ScalarTestHelper helper;

  const std::vector<std::string> captions({"train", "test"});

  const size_t nsteps = 100;

  auto write = [&] {
    auto tablet = helper.wim.AddTablet("tag0", -1);
    components::ScalarHelper<float> scalar(tablet, &helper.wim.handler());

    scalar.SetCaptions(captions);

    for (int i = 0; i < nsteps; i++) {
      scalar.AddRecord(i * 10, std::vector<float>({(float)i, (float)i + 1}));
    }
  };

  auto read = [&] {
    auto mytablet = helper.rim.tablet("tag0");
    components::ScalarHelper<float> myscalar(mytablet, &helper.rim.handler());

    auto records = myscalar.GetRecords();
    ASSERT_EQ(records.size(), nsteps);

    for (int i = 0; i < nsteps; i++) {
      ASSERT_EQ(records[i], std::vector<float>({(float)i, (float)i + 1}));
    }
  };

  helper(read, write);
}

S
superjom 已提交
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
TEST(Scalar, mode) {
  ScalarTestHelper helper;
  auto train_wim = helper.wim.AsMode("train");

  auto write = [&] {
    auto tablet = train_wim.AddTablet("tag1", -1);
    components::ScalarHelper<float> scalar(tablet, &train_wim.handler());

    scalar.SetCaptions(std::vector<std::string>({"train"}));
    scalar.AddRecord(10, std::vector<float>({0.1}));
  };

  auto reader = [&] {};
}

S
Superjom 已提交
98
}  // namespace visualdl