clamp checkpoint token delta overflow · Entire

clamp checkpoint token delta overflow

5c246c4·

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
378 unmodified lines

448
449
450
451
452
453
454
450
451
455
456
457
458
459
2 unmodified lines

462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
463
486
487
465
488
489
467
490
491
492
493
2 unmodified lines

496
497
498
476
499
500
478
501
502
503
504
125 unmodified lines

630
631
632
610
633
634
635
636

59 unmodified lines

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

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

func newCheckpointTokensCmd() *cobra.Command {
378 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

2395 unmodified lines

2396
2397
2398
2399
2399
2400
2401
2402
102 unmodified lines

2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533

2395 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()