forked from envoyproxy/nighthawk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscoring_function_impl.h
87 lines (74 loc) · 3.16 KB
/
scoring_function_impl.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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
83
84
85
86
87
// Implementations of ScoringFunction plugins and corresponding factories.
#pragma once
#include "envoy/registry/registry.h"
#include "nighthawk/adaptive_load/scoring_function.h"
#include "api/adaptive_load/scoring_function_impl.pb.h"
namespace Nighthawk {
/**
* ScoringFunction that returns 1.0 when a metric is within thresholds and -1.0 otherwise.
* Supports upper or lower threshold or both.
*/
class BinaryScoringFunction : public ScoringFunction {
public:
/**
* Constructs a BinaryScoringFunction, taking thresholds from the config proto.
*/
explicit BinaryScoringFunction(
const nighthawk::adaptive_load::BinaryScoringFunctionConfig& config);
double EvaluateMetric(double value) const override;
private:
// Upper threshold for the metric.
const double upper_threshold_;
// Lower threshold for the metric.
const double lower_threshold_;
};
/**
* Factory that creates a BinaryScoringFunction from a BinaryScoringFunctionConfig proto.
* Registered as an Envoy plugin.
*/
class BinaryScoringFunctionConfigFactory : public ScoringFunctionConfigFactory {
public:
std::string name() const override;
Envoy::ProtobufTypes::MessagePtr createEmptyConfigProto() override;
ScoringFunctionPtr createScoringFunction(const Envoy::Protobuf::Message& message) override;
absl::Status ValidateConfig(const Envoy::Protobuf::Message& message) const override;
};
// This factory is activated through LoadScoringFunctionPlugin in plugin_util.h.
DECLARE_FACTORY(BinaryScoringFunctionConfigFactory);
/**
* ScoringFunction that calculates a metric score as k * (threshold - value), where k is a scaling
* constant. The score is 0.0 when the value exactly equals the threshold, positive below the
* threshold (meaning load should increase), and negative above the threshold. The score is
* proportional to the difference from the threshold.
*/
class LinearScoringFunction : public ScoringFunction {
public:
/**
* Constructs a LinearScoringFunction, taking threshold and scaling parameter from the config
* proto.
*/
explicit LinearScoringFunction(
const nighthawk::adaptive_load::LinearScoringFunctionConfig& config);
double EvaluateMetric(double value) const override;
private:
// The target value of the metric.
const double threshold_;
// Scaling constant: score = scaling_constant_ * (threshold - value). Use this in combination
// with step controller constants to produce reasonable load increments for reasonable
// differences from the threshold.
const double scaling_constant_;
};
/**
* Factory that creates a LinearScoringFunction from a LinearScoringFunctionConfig proto.
* Registered as an Envoy plugin.
*/
class LinearScoringFunctionConfigFactory : public ScoringFunctionConfigFactory {
public:
std::string name() const override;
Envoy::ProtobufTypes::MessagePtr createEmptyConfigProto() override;
ScoringFunctionPtr createScoringFunction(const Envoy::Protobuf::Message& message) override;
absl::Status ValidateConfig(const Envoy::Protobuf::Message& message) const override;
};
// This factory is activated through LoadScoringFunctionPlugin in plugin_util.h.
DECLARE_FACTORY(LinearScoringFunctionConfigFactory);
} // namespace Nighthawk