clamp checkpoint token delta overflow · Entire

clamp checkpoint token delta overflow

7da0d50·

peyton-alt·3w ago·2 files·+55 added/-9 removed

Sessions

70741de008a8View transcript

Changes

2

59 unmodified lines

60
61
62
63
64
65
66
67
68
69
366 unmodified lines

436
437
438
439
440
441
442
438
439
443
444
445
446
447
2 unmodified lines

450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
451
474
475
453
476
477
455
478
479
480
481
2 unmodified lines

484
485
486
464
487
488
466
489
490
491
492
125 unmodified lines

618
619
620
598
621
622
623
624

59 unmodified lines

checkpointComparisonStatusObservedReduction = "observed_reduction"
    checkpointComparisonStatusObservedIncrease  = "observed_increase"
    checkpointComparisonStatusObservedNoChange  = "observed_no_change"

checkpointDeltaDirectionDown      = "down"
    checkpointDeltaDirectionUp        = "up"
    checkpointDeltaDirectionUnchanged = "unchanged"
)

func newCheckpointTokensCmd() *cobra.Command {
366 unmodified lines

}

func buildCheckpointMetricDelta(baseline, current int) *checkpointTokensMetricDelta {
    change := saturatingIntSub(current, baseline)
    delta := &checkpointTokensMetricDelta{
        Baseline:  baseline,
        Current:   current,
        Change:    current - baseline,
        Direction: checkpointDeltaDirection(current - baseline),
        Change:    change,
        Direction: checkpointDeltaDirection(change),
    }
    if baseline != 0 {
        percent := float64(delta.Change) * 100 / float64(baseline)
2 unmodified lines

return delta
}

func saturatingIntSub(a, b int) int {
    if b < 0 && a > maxInt()+b {
        return maxInt()
    }
    if b > 0 && a < minInt()+b {
        return minInt()
    }
    return a - b
}

func maxInt() int {
    return int(^uint(0) >> 1)
}

func minInt() int {
    return -maxInt() - 1
}

func checkpointDeltaDirection(change int) string {
    switch {
    case change < 0:
        return "down"
        return checkpointDeltaDirectionDown
    case change > 0:
        return "up"
        return checkpointDeltaDirectionUp
    default:
        return "unchanged"
        return checkpointDeltaDirectionUnchanged
    }
}

2 unmodified lines

return checkpointComparisonStatusUnavailable
}
switch total.Direction {
case "down":
case checkpointDeltaDirectionDown:
    return checkpointComparisonStatusObservedReduction
case "up":
case checkpointDeltaDirectionUp:
    return checkpointComparisonStatusObservedIncrease
default:
    return checkpointComparisonStatusObservedNoChange
}
125 unmodified lines

}
from := formatValue(delta.Baseline)
to := formatValue(delta.Current)
if delta.Direction == "unchanged" {
if delta.Direction == checkpointDeltaDirectionUnchanged {
    return fmt.Sprintf("unchanged (%%s -> %%s)", from, to)
}
if delta.ChangePercent == nil || *result.Comparison.Total.ChangePercent != 60 {

Mcmd/entire/cli/checkpoint_tokens.go+31/-8

2329 unmodified lines

2330
2331
2332
2333
2333
2334
2335
2336
102 unmodified lines

2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467

2329 unmodified lines

if result.Comparison.Total.Change != 330 {
        t.Fatalf("expected total change 330, got %+v", result.Comparison.Total)
    }
    if result.Comparison.Total.Direction != "up" {
        if result.Comparison.Total.Direction != checkpointDeltaDirectionUp {
        t.Fatalf("expected total direction up, got %+v", result.Comparison.Total)
    }
        if result.Comparison.Total.ChangePercent == nil || *result.Comparison.Total.ChangePercent != 60 {
102 unmodified lines

}
}

func TestBuildCheckpointMetricDeltaClampsChangeOverflow(t *testing.T) {
    t.Parallel()

maxInt := int(^uint(0) >> 1)
    minInt := -maxInt - 1

up := buildCheckpointMetricDelta(minInt, maxInt)
    if up.Change != maxInt {
        t.Fatalf("upward overflow change = %%d, want %%d", up.Change, maxInt)
    }
    if up.Direction != checkpointDeltaDirectionUp {
        t.Fatalf("upward overflow direction = %%q, want up", up.Direction)
    }

down := buildCheckpointMetricDelta(maxInt, minInt)
    if down.Change != minInt {
        t.Fatalf("downward overflow change = %%d, want %%d", down.Change, minInt)
    }
    if down.Direction != checkpointDeltaDirectionDown {
        t.Fatalf("downward overflow direction = %%q, want down", down.Direction)
    }
}

func TestCheckpointTokensCmd_ComparisonUnavailableWhenBaselineTokenDataMissing(t *testing.T) {
    repo, _ := runExplainAutoTestRepo(t)
    ctx := context.Background()