cleanup: remove artifact .class and .go test files from defects/scala and defects/victoria-metrics
This commit is contained in:
parent
25c2bafdee
commit
390ce56a83
2 changed files with 0 additions and 180 deletions
Binary file not shown.
|
|
@ -1,180 +0,0 @@
|
|||
package unit
|
||||
|
||||
// CWE-407 unit test for victoria-metrics-0002
|
||||
// Tests that RemoveTagsOn/RemoveTagsIgnoring/SetTags with map-based lookup produce
|
||||
// the same results as the linear-scan original, and measures speedup.
|
||||
//
|
||||
// Run: go test -v -run TestVictoriaMetrics0002 ./...
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
// Simulate the old O(T×I) linear scan approach
|
||||
func hasTagLinear(tags []string, key string) bool {
|
||||
for _, t := range tags {
|
||||
if t == key {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// Simulate the new O(1) map-based approach
|
||||
func buildTagSet(tags []string) map[string]struct{} {
|
||||
m := make(map[string]struct{}, len(tags))
|
||||
for _, t := range tags {
|
||||
m[t] = struct{}{}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// simulateRemoveTagsOnLinear simulates old behavior: O(T×I) per call
|
||||
func simulateRemoveTagsOnLinear(metricTags []string, onTags []string) []string {
|
||||
var result []string
|
||||
for _, tag := range metricTags {
|
||||
if hasTagLinear(onTags, tag) {
|
||||
result = append(result, tag)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// simulateRemoveTagsOnMap simulates new behavior: build map once, O(T) lookup
|
||||
func simulateRemoveTagsOnMap(metricTags []string, onTags []string) []string {
|
||||
onSet := buildTagSet(onTags)
|
||||
var result []string
|
||||
for _, tag := range metricTags {
|
||||
if _, ok := onSet[tag]; ok {
|
||||
result = append(result, tag)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// simulateRemoveTagsIgnoringLinear simulates old behavior
|
||||
func simulateRemoveTagsIgnoringLinear(metricTags []string, ignoringTags []string) []string {
|
||||
var result []string
|
||||
for _, tag := range metricTags {
|
||||
if !hasTagLinear(ignoringTags, tag) {
|
||||
result = append(result, tag)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// simulateRemoveTagsIgnoringMap simulates new behavior
|
||||
func simulateRemoveTagsIgnoringMap(metricTags []string, ignoringTags []string) []string {
|
||||
ignoreSet := buildTagSet(ignoringTags)
|
||||
var result []string
|
||||
for _, tag := range metricTags {
|
||||
if _, ok := ignoreSet[tag]; !ok {
|
||||
result = append(result, tag)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func TestVictoriaMetrics0002_RemoveTagsOn_Correctness(t *testing.T) {
|
||||
metricTags := []string{"job", "instance", "cluster", "region", "env", "namespace", "pod", "container", "app", "team"}
|
||||
onTags := []string{"job", "cluster", "region"}
|
||||
|
||||
linear := simulateRemoveTagsOnLinear(metricTags, onTags)
|
||||
mapped := simulateRemoveTagsOnMap(metricTags, onTags)
|
||||
|
||||
if len(linear) != len(mapped) {
|
||||
t.Fatalf("RemoveTagsOn: linear=%v mapped=%v", linear, mapped)
|
||||
}
|
||||
for i := range linear {
|
||||
if linear[i] != mapped[i] {
|
||||
t.Fatalf("RemoveTagsOn mismatch at %d: linear=%s mapped=%s", i, linear[i], mapped[i])
|
||||
}
|
||||
}
|
||||
if len(linear) != 3 {
|
||||
t.Fatalf("Expected 3 tags kept, got %d: %v", len(linear), linear)
|
||||
}
|
||||
t.Logf("RemoveTagsOn: kept %v (PASS)", linear)
|
||||
}
|
||||
|
||||
func TestVictoriaMetrics0002_RemoveTagsIgnoring_Correctness(t *testing.T) {
|
||||
metricTags := []string{"job", "instance", "cluster", "region", "env", "namespace", "pod", "container", "app", "team"}
|
||||
ignoringTags := []string{"instance", "pod", "container"}
|
||||
|
||||
linear := simulateRemoveTagsIgnoringLinear(metricTags, ignoringTags)
|
||||
mapped := simulateRemoveTagsIgnoringMap(metricTags, ignoringTags)
|
||||
|
||||
if len(linear) != len(mapped) {
|
||||
t.Fatalf("RemoveTagsIgnoring: linear=%v mapped=%v", linear, mapped)
|
||||
}
|
||||
for i := range linear {
|
||||
if linear[i] != mapped[i] {
|
||||
t.Fatalf("RemoveTagsIgnoring mismatch at %d: linear=%s mapped=%s", i, linear[i], mapped[i])
|
||||
}
|
||||
}
|
||||
if len(linear) != 7 {
|
||||
t.Fatalf("Expected 7 tags kept, got %d: %v", len(linear), linear)
|
||||
}
|
||||
t.Logf("RemoveTagsIgnoring: kept %v (PASS)", linear)
|
||||
}
|
||||
|
||||
func TestVictoriaMetrics0002_SpeedupRatio(t *testing.T) {
|
||||
// Simulate N=10000 series, T=20 tags each, I=10 ignoring-labels
|
||||
const N = 10000
|
||||
metricTags := []string{
|
||||
"job", "instance", "cluster", "region", "env",
|
||||
"namespace", "pod", "container", "app", "team",
|
||||
"dc", "zone", "tier", "svc", "owner",
|
||||
"version", "release", "build", "repo", "branch",
|
||||
}
|
||||
ignoringTags := []string{"instance", "pod", "container", "build", "release", "dc", "zone", "owner", "branch", "repo"}
|
||||
|
||||
var linearOps, mapOps int
|
||||
|
||||
// Count operations for linear approach
|
||||
for n := 0; n < N; n++ {
|
||||
for _, tag := range metricTags {
|
||||
for _, itag := range ignoringTags {
|
||||
linearOps++
|
||||
if tag == itag {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Count operations for map approach: build map once, then N×T lookups
|
||||
mapOps = len(ignoringTags) // build map
|
||||
for n := 0; n < N; n++ {
|
||||
mapOps += len(metricTags) // O(1) map lookup per tag
|
||||
_ = n
|
||||
}
|
||||
|
||||
ratio := float64(linearOps) / float64(mapOps)
|
||||
t.Logf("N=%d series, T=%d tags, I=%d ignoring-labels", N, len(metricTags), len(ignoringTags))
|
||||
t.Logf("Linear ops: %d", linearOps)
|
||||
t.Logf("Map ops: %d", mapOps)
|
||||
t.Logf("Ratio: %.1fx", ratio)
|
||||
|
||||
if ratio < 3.0 {
|
||||
t.Errorf("Expected speedup ratio >= 3.0x, got %.1fx", ratio)
|
||||
}
|
||||
t.Logf("PASS: speedup ratio %.1fx >= 3.0x", ratio)
|
||||
}
|
||||
|
||||
func BenchmarkVictoriaMetrics0002_Linear(b *testing.B) {
|
||||
metricTags := []string{"job", "instance", "cluster", "region", "env", "namespace", "pod", "container", "app", "team", "dc", "zone", "tier", "svc", "owner", "version", "release", "build", "repo", "branch"}
|
||||
ignoringTags := []string{"instance", "pod", "container", "build", "release", "dc", "zone", "owner", "branch", "repo"}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = simulateRemoveTagsIgnoringLinear(metricTags, ignoringTags)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkVictoriaMetrics0002_Map(b *testing.B) {
|
||||
metricTags := []string{"job", "instance", "cluster", "region", "env", "namespace", "pod", "container", "app", "team", "dc", "zone", "tier", "svc", "owner", "version", "release", "build", "repo", "branch"}
|
||||
ignoringTags := []string{"instance", "pod", "container", "build", "release", "dc", "zone", "owner", "branch", "repo"}
|
||||
b.ResetTimer()
|
||||
for i := 0; i < b.N; i++ {
|
||||
_ = simulateRemoveTagsIgnoringMap(metricTags, ignoringTags)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue