Esempio n. 1
0
TEST(TEma, SimpleTest1) {
	try {
		TSignalProc::TEma ema(TSignalProc::TEmaType::etPreviousPoint, 0, 2000);
				
		ema.Update(1.0, 1000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(1, ema.GetValue());

		ema.Update(1.0, 2000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(1, ema.GetValue());

		ema.Update(1.0, 3000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(1, ema.GetValue());

		ema.Update(2.0, 4000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(1, ema.GetValue());
		
		ema.Update(2.0, 5000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(1.393469, ema.GetValue());

		ema.Update(3.0, 6000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(1.632121, ema.GetValue());

		ema.Update(3.0, 7000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(2.170339, ema.GetValue());

		ema.Update(3.0, 8000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(2.496785, ema.GetValue());

		ema.Update(4.0, 10000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(2.496785, ema.GetValue());

		ema.Update(5.0, 30000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(2.496785, ema.GetValue());

		ema.Update(5.0, 31000);
		printf("ema: %f\n", ema.GetValue());
		EXPECT_EQ(2.496785, ema.GetValue());

	} catch (PExcept& Except) {
		printf("Error: %s", Except->GetStr());
		throw Except;
	}
}
Esempio n. 2
0
Try<Nothing> EMAFilter::consume(const ResourceUsage& in) {
  ResourceUsage product;

  for (ResourceUsage_Executor inExec : in.executors()) {
    if (!inExec.has_executor_info()) {
      SERENITY_LOG(ERROR) << "Executor <unknown>"
                 << " does not include executor_info";
      // Filter out these executors.
      continue;
    }
    if (!inExec.has_statistics()) {
      SERENITY_LOG(ERROR) << "Executor "
                 << inExec.executor_info().executor_id().value()
                 << " does not include statistics.";
      // Filter out these executors.
      continue;
    }

    // Check if EMA for given executor exists.
    auto emaSample = this->emaSamples->find(inExec.executor_info());
    if (emaSample == this->emaSamples->end()) {
      SERENITY_LOG(ERROR) << "First EMA iteration for: "
                          << WID(inExec.executor_info()).toString();
      // If not - insert new one.
      ExponentialMovingAverage ema(EMA_REGULAR_SERIES, this->alpha);
      emaSamples->insert(std::pair<ExecutorInfo, ExponentialMovingAverage>(
          inExec.executor_info(), ema));

    } else {
      // Get proper value.
      Try<double_t> value = this->valueGetFunction(inExec);
      if (value.isError()) {
        SERENITY_LOG(ERROR) << value.error();
        continue;
      }

      // Perform EMA filtering.
      double_t emaValue =
        (emaSample->second).calculateEMA(
            value.get(),
            inExec.statistics().perf().timestamp());

      // Store EMA value.
      ResourceUsage_Executor* outExec = new ResourceUsage_Executor(inExec);
      Try<Nothing> result = this->valueSetFunction(emaValue, outExec);
      if (result.isError()) {
        SERENITY_LOG(ERROR) << result.error();
        delete outExec;
        continue;
      }

      // Add an executor only when there was no error.
      product.mutable_executors()->AddAllocated(outExec);
    }
  }

  if (0 != product.executors_size()) {
    SERENITY_LOG(INFO) << "Continuing with "
                       << product.executors_size() << " executor(s).";
    // Continue pipeline.
    // Copy total agent's capacity.
    product.mutable_total()->CopyFrom(in.total());
    produce(product);
  }

  return Nothing();
}