-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f2a023f
commit c944c1f
Showing
3 changed files
with
85 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package evaluate | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
// MetricKey is a description for a numerical metric. | ||
type MetricKey string | ||
|
||
// allMetricKeys holds all metric keys. | ||
var allMetricKeys []MetricKey | ||
|
||
func registerMetricKey(key string) MetricKey { | ||
metric := MetricKey(key) | ||
allMetricKeys = append(allMetricKeys, metric) | ||
|
||
return metric | ||
} | ||
|
||
var ( | ||
// MetricKeyTotal is the total number of benchmarking candidates. | ||
MetricKeyTotal = registerMetricKey("total") | ||
|
||
// MetricKeyExecuted is the number of benchmarking candidates with successful execution. | ||
MetricKeyExecuted = registerMetricKey("executed") | ||
) | ||
|
||
// Metrics holds numerical benchmarking metrics. | ||
type Metrics map[MetricKey]uint | ||
|
||
// Metrics holds numerical percentage-based benchmarking metrics. | ||
type MetricsPercentual map[MetricKey]float64 | ||
|
||
// Add sums two metrics objects. | ||
func (m Metrics) Add(o Metrics) Metrics { | ||
metrics := map[MetricKey]uint{} | ||
|
||
for _, k := range allMetricKeys { | ||
metrics[k] = m[k] + o[k] | ||
} | ||
|
||
return Metrics(metrics) | ||
} | ||
|
||
// Percentual converts the metrics into percentage-based metrics. | ||
// If the total key "MetricsKeyTotal" is not present "nil" is returned instead. | ||
func (m Metrics) Percentual() MetricsPercentual { | ||
total := float64(m[MetricKeyTotal]) | ||
if total == 0.0 { | ||
return nil | ||
} | ||
metrics := map[MetricKey]float64{} | ||
|
||
for _, k := range allMetricKeys { | ||
if k == MetricKeyTotal { | ||
continue | ||
} | ||
metrics[k] = (float64(m[k]) / total) * 100.0 | ||
} | ||
|
||
return MetricsPercentual(metrics) | ||
} | ||
|
||
// String returns a string representation of the metrics. | ||
func (m Metrics) String() string { | ||
return fmt.Sprintf("#executed=%d/%d", m[MetricKeyExecuted], m[MetricKeyTotal]) | ||
} | ||
|
||
// String returns a string representation of the metrics. | ||
func (m MetricsPercentual) String() string { | ||
return fmt.Sprintf("#executed=%3.0f%%", m[MetricKeyExecuted]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters