CodeSampleX

サンプル

github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9: metrics.GetOrRegisterTimer, metrics.NewTimer, metrics.Timer

検証済みサンプル — golang github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9: metrics.GetOrRegisterTimer, metrics.NewTimer, metrics.Timer. go 1.26 …

sha256:1dde5092bd282e5b430e57838ac8eb394e2fd1fe57cff6482a94a5fa0c6ecaf1

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 MIT-0

実行証拠

宣言された環境と署名済みの実行を分けてあります。このサンプルが何をどこで実行したかをそのまま確認できます。

証拠の基準
署名済みコントラクト合格
検証レシート
1
ビルドした署名鍵
1
宣言された環境 linux 24 · ubuntu · glibc 2.39 x64 go

検証実行環境

環境 コントラクト ステージ 実行日
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-02

ケース

HOW
ゴール
verify pkg:golang/github.com/rcrowley/go-metrics@v0.0.0-20250401214520-65e299d6c5c9
パッケージ
シンボル
  • metrics.GetOrRegisterTimer
  • metrics.NewTimer
  • metrics.Timer
作成日
2026-09-02T20:45:21Z

コントラクト

  1. metrics.NewTimer constructs a new timer instance with count 0
  2. Timer.Update records duration samples and increments count
  3. Timer.Time measures execution duration of a closure
  4. Timer.Snapshot captures an immutable point-in-time snapshot with statistical metrics
  5. Timer.Percentile and Percentiles calculate duration distribution percentiles
  6. metrics.GetOrRegisterTimer registers a new timer or returns an existing one from a registry
  7. Timer.Stop releases timer resources without altering accumulated counts

ファイル

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

ソースアーティファクトをダウンロード (tar.gz)

ソース

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify pkg:golang/github.com/rcrowley/go-metrics@v0.0.0-20250401214520-65e299d6c5c9
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/rcrowley/go-metrics@v0.0.0-20250401214520-65e299d6c5c9
Demonstrate these symbols/APIs:
  - metrics.GetOrRegisterTimer
  - metrics.NewTimer
  - metrics.Timer

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:43e6cc7d6c2d4d71b5bd52450ae474f01c9ae14c89d4a75a05804d0664b28186","contract":["metrics.NewTimer constructs a new timer instance with count 0","Timer.Update records duration samples and increments count","Timer.Time measures execution duration of a closure","Timer.Snapshot captures an immutable point-in-time snapshot with statistical metrics","Timer.Percentile and Percentiles calculate duration distribution percentiles","metrics.GetOrRegisterTimer registers a new timer or returns an existing one from a registry","Timer.Stop releases timer resources without altering accumulated counts"],"goal":"verify pkg:golang/github.com/rcrowley/go-metrics@v0.0.0-20250401214520-65e299d6c5c9","kind":"HOW","packages":["pkg:golang/github.com/rcrowley/go-metrics@v0.0.0-20250401214520-65e299d6c5c9"],"schemaVersion":1,"symbols":["metrics.GetOrRegisterTimer","metrics.NewTimer","metrics.Timer"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/rcrowley/go-metrics@v0.0.0-20250401214520-65e299d6c5c9"],"schemaVersion":1,"subject":"pkg:golang/github.com/rcrowley/go-metrics@v0.0.0-20250401214520-65e299d6c5c9","symbols":["metrics.GetOrRegisterTimer","metrics.NewTimer","metrics.Timer"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.22

require github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9
go.sum
github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9 h1:bsUq1dX0N8AOIL7EB/X911+m4EHsnWEHeJ0c+3TTBrg=
github.com/rcrowley/go-metrics v0.0.0-20250401214520-65e299d6c5c9/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
main.go
package main

import (
	"fmt"
	"time"

	"github.com/rcrowley/go-metrics"
)

func main() {
	// Create a metrics registry
	registry := metrics.NewRegistry()

	// Create a standalone timer
	timer := metrics.NewTimer()
	defer timer.Stop()

	// Record execution duration using Time closure helper
	timer.Time(func() {
		time.Sleep(10 * time.Millisecond)
	})

	// Record duration directly using Update
	timer.Update(25 * time.Millisecond)

	// Register or get a timer from the registry
	apiTimer := metrics.GetOrRegisterTimer("api.request_duration", registry)
	apiTimer.Update(15 * time.Millisecond)

	// Capture point-in-time snapshot
	snapshot := timer.Snapshot()
	fmt.Printf("Timer count: %d\n", snapshot.Count())
	fmt.Printf("Timer mean: %.2f ms\n", snapshot.Mean()/float64(time.Millisecond))
	fmt.Printf("Timer 95th percentile: %.2f ms\n", snapshot.Percentile(0.95)/float64(time.Millisecond))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/github.com/rcrowley/go-metrics@v0.0.0-20250401214520-65e299d6c5c9",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/rcrowley/go-metrics@v0.0.0-20250401214520-65e299d6c5c9"
  ],
  "symbols": [
    "metrics.GetOrRegisterTimer",
    "metrics.NewTimer",
    "metrics.Timer"
  ]
}
test/contract.go
package main

import (
	"fmt"
	"os"
	"time"

	"github.com/rcrowley/go-metrics"
)

func main() {
	// 1. metrics.NewTimer constructs a new timer instance with count 0
	tm := metrics.NewTimer()
	if tm == nil {
		fmt.Fprintf(os.Stderr, "metrics.NewTimer returned nil\n")
		os.Exit(1)
	}
	if tm.Count() != 0 {
		fmt.Fprintf(os.Stderr, "expected initial count 0, got %d\n", tm.Count())
		os.Exit(1)
	}
	if tm.Min() != 0 || tm.Max() != 0 {
		fmt.Fprintf(os.Stderr, "expected initial min/max 0, got min=%d max=%d\n", tm.Min(), tm.Max())
		os.Exit(1)
	}

	// 2. Timer.Update records duration samples and increments count
	tm.Update(50 * time.Millisecond)
	if tm.Count() != 1 {
		fmt.Fprintf(os.Stderr, "expected count 1 after first update, got %d\n", tm.Count())
		os.Exit(1)
	}
	tm.Update(150 * time.Millisecond)
	if tm.Count() != 2 {
		fmt.Fprintf(os.Stderr, "expected count 2 after second update, got %d\n", tm.Count())
		os.Exit(1)
	}
	if tm.Max() < int64(150*time.Millisecond) {
		fmt.Fprintf(os.Stderr, "expected max >= 150ms, got %d\n", tm.Max())
		os.Exit(1)
	}
	if tm.Min() > int64(50*time.Millisecond) || tm.Min() <= 0 {
		fmt.Fprintf(os.Stderr, "expected min <= 50ms and > 0, got %d\n", tm.Min())
		os.Exit(1)
	}

	// 3. Timer.Time measures execution duration of a closure
	tm.Time(func() {
		time.Sleep(20 * time.Millisecond)
	})
	if tm.Count() != 3 {
		fmt.Fprintf(os.Stderr, "expected count 3 after Time(), got %d\n", tm.Count())
		os.Exit(1)
	}

	// 4. Timer.Snapshot captures an immutable point-in-time snapshot with statistical metrics
	snap := tm.Snapshot()
	if snap == nil {
		fmt.Fprintf(os.Stderr, "Snapshot() returned nil\n")
		os.Exit(1)
	}
	if snap.Count() != 3 {
		fmt.Fprintf(os.Stderr, "expected snapshot count 3, got %d\n", snap.Count())
		os.Exit(1)
	}
	snapCount := snap.Count()
	tm.Update(300 * time.Millisecond)
	if snap.Count() != snapCount {
		fmt.Fprintf(os.Stderr, "expected snapshot count to remain %d, got %d\n", snapCount, snap.Count())
		os.Exit(1)
	}

	snapPanicked := false
	func() {
		defer func() {
			if recover() != nil {
				snapPanicked = true
			}
		}()
		snap.Update(10 * time.Millisecond)
	}()
	if !snapPanicked {
		fmt.Fprintf(os.Stderr, "expected snap.Update to panic on snapshot\n")
		os.Exit(1)
	}

	// 5. Timer.Percentile and Percentiles calculate duration distribution percentiles
	p95 := snap.Percentile(0.95)
	if p95 <= 0 {
		fmt.Fprintf(os.Stderr, "expected positive 95th percentile, got %f\n", p95)
		os.Exit(1)
	}
	percentiles := snap.Percentiles([]float64{0.5, 0.95, 0.99})
	if len(percentiles) != 3 {
		fmt.Fprintf(os.Stderr, "expected 3 percentiles, got %d\n", len(percentiles))
		os.Exit(1)
	}
	if percentiles[0] > percentiles[1] || percentiles[1] > percentiles[2] {
		fmt.Fprintf(os.Stderr, "expected non-decreasing percentiles: %v\n", percentiles)
		os.Exit(1)
	}

	// 6. metrics.GetOrRegisterTimer registers a new timer or returns an existing one from a registry
	r := metrics.NewRegistry()
	t1 := metrics.GetOrRegisterTimer("request_timer", r)
	if t1 == nil {
		fmt.Fprintf(os.Stderr, "GetOrRegisterTimer returned nil\n")
		os.Exit(1)
	}
	t1.Update(100 * time.Millisecond)

	t2 := metrics.GetOrRegisterTimer("request_timer", r)
	if t2.Count() != 1 {
		fmt.Fprintf(os.Stderr, "expected existing timer count 1, got %d\n", t2.Count())
		os.Exit(1)
	}
	t2.Update(200 * time.Millisecond)
	if t1.Count() != 2 {
		fmt.Fprintf(os.Stderr, "expected both references to see count 2, got %d\n", t1.Count())
		os.Exit(1)
	}

	// 7. Timer.Stop releases timer resources without altering accumulated counts
	countBeforeStop := tm.Count()
	tm.Stop()
	if tm.Count() != countBeforeStop {
		fmt.Fprintf(os.Stderr, "expected count %d after Stop(), got %d\n", countBeforeStop, tm.Count())
		os.Exit(1)
	}

	fmt.Println("All go-metrics Timer contract assertions passed.")
}

オリジンシーダー

匿名