Commit 0f3247dc authored by Paul Asmuth's avatar Paul Asmuth
Browse files

diskbackend-test

parent e0c5d244
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -215,9 +215,9 @@ add_executable(tests/test-statsd
    stage/src/fnordmetric/metricdb/statsd_test.cc)
target_link_libraries(tests/test-statsd fnord-static)

#add_executable(tests/test-metric
#    stage/src/fnordmetric/metricdb/metric_test.cc)
#target_link_libraries(tests/test-metric fnord-static)
add_executable(tests/test-disk-backend
    stage/src/fnordmetric/metricdb/backends/disk/diskbackend_test.cc)
target_link_libraries(tests/test-disk-backend fnord-static)

#add_executable(tests/test-metricrepository
#    stage/src/fnordmetric/metricdb/metricrepository_test.cc)
+1 −1
Original line number Diff line number Diff line
@@ -146,7 +146,7 @@ void FileUtil::rm(const std::string& filename) {

void FileUtil::truncate(const std::string& filename, size_t new_size) {
  if (::truncate(filename.c_str(), new_size) < 0) {
    RAISE_ERRNO(kIOError, "ftruncate(%s) failed", filename.c_str());
    RAISE_ERRNO(kIOError, "truncate(%s) failed", filename.c_str());
  }
}

+84 −0
Original line number Diff line number Diff line
/**
 * This file is part of the "FnordMetric" project
 *   Copyright (c) 2014 Paul Asmuth, Google Inc.
 *
 * FnordMetric is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License v3.0. You should have received a
 * copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */
#include <fnordmetric/environment.h>
#include <fnordmetric/io/fileutil.h>
#include <fnordmetric/metricdb/backends/disk/metric.h>
#include <fnordmetric/util/unittest.h>
#include <fnordmetric/util/wallclock.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>

using namespace fnordmetric::metricdb::disk_backend;
using namespace fnordmetric::metricdb;
using namespace fnord::io;

UNIT_TEST(DiskBackendTest);

const char kTestRepoPath[] = "/tmp/__fnordmetric_test_metricrepo2";

using LabelListType = std::vector<std::pair<std::string, std::string>>;

TEST_CASE(DiskBackendTest, TestCreateNewMetric, [] () {
  io::FileUtil::mkdir_p(kTestRepoPath);
  FileRepository file_repo(kTestRepoPath);
  file_repo.deleteAllFiles();

  Metric metric("myfirstmetric", &file_repo);
  metric.setLiveTableMaxSize(4 * 2 << 19); /* 4MB */

  int n = 0;
  metric.scanSamples(
      util::DateTime::epoch(),
      util::DateTime::now(),
      [&n] (Sample* sample) -> bool {
        n++;
        return true;
      });

  EXPECT_EQ(n, 0);

  std::vector<std::string> labels;
  for (int i = 0; i < 1000; ++i) {
    labels.emplace_back(fnord::util::Random::alphanumericString(16));
  }

  auto seq1 = [] (double x) {
    return fmod((x + 1) * 23.5f, 4200.0f);
  };

  int num_saples = 100000;
  for (int i = 0; i < num_saples; ++i) {
    LabelListType smpl_labels;
    smpl_labels.emplace_back(labels[i % labels.size()], "myvalue");
    metric.insertSample(seq1(i), smpl_labels);
  }

  size_t total_bytes = metric.totalBytes();
  metric.compact();
  EXPECT_EQ(metric.totalBytes(), total_bytes);

  n = 0;
  metric.scanSamples(
      util::DateTime::epoch(),
      util::DateTime::now(),
      [&n, &seq1] (Sample* sample) -> bool {
        EXPECT_EQ(sample->value(), seq1(n));
        n++;
        return true;
      });

  EXPECT_EQ(n, num_saples);
});



+3 −2
Original line number Diff line number Diff line
@@ -17,7 +17,7 @@
#include <string.h>
#include <math.h>

using namespace fnordmetric::metricdb;
using namespace fnordmetric::metricdb::disk_backend;
using namespace fnord::io;

UNIT_TEST(MetricTest);
@@ -42,6 +42,7 @@ TEST_CASE(MetricTest, TestCreateNewMetric, [] () {
      });

  EXPECT_EQ(n, 0);
  metric.compact();

  std::vector<std::string> labels;
  for (int i = 0; i < 1000; ++i) {
@@ -52,7 +53,7 @@ TEST_CASE(MetricTest, TestCreateNewMetric, [] () {
    return fmod((x + 1) * 23.5f, 4200.0f);
  };

  int num_saples = 100000;
  int num_saples = 1000000;
  for (int i = 0; i < num_saples; ++i) {
    Sample<double> sample;
    sample.value = seq1(i);
+7 −4
Original line number Diff line number Diff line
@@ -20,10 +20,13 @@ void IMetric::insertSample(
    double value,
    const std::vector<std::pair<std::string, std::string>>& labels) {
  // FIXPAUL slow slow slow!
  for (const auto& l1 : labels) {
    for (const auto& l2 : labels) {
      if (l1.first == l2.first) {
        RAISE(kIllegalArgumentError, "duplicate label: %s", l1.first.c_str());
  for (int i1 = 0; i1 < labels.size(); ++i1) {
    for (int i2 = 0; i2 < labels.size(); ++i2) {
      if (i1 != i1 && labels[i1].first == labels[i2].first) {
        RAISE(
            kIllegalArgumentError,
            "duplicate label: %s",
            labels[i1].first.c_str());
      }
    }
  }
Loading