|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "os/signal" |
| 9 | + "runtime" |
| 10 | + "sync" |
| 11 | + "sync/atomic" |
| 12 | + "syscall" |
| 13 | + "time" |
| 14 | + |
| 15 | + openfeature "github.com/open-feature/go-sdk/openfeature" |
| 16 | + confidence "github.com/spotify/confidence-resolver-rust/openfeature-provider/go/confidence" |
| 17 | + "google.golang.org/grpc" |
| 18 | + "google.golang.org/grpc/credentials/insecure" |
| 19 | +) |
| 20 | + |
| 21 | +type stats struct { |
| 22 | + completed uint64 |
| 23 | + errors uint64 |
| 24 | +} |
| 25 | + |
| 26 | +func main() { |
| 27 | + var ( |
| 28 | + mockGRPCAddr string |
| 29 | + durationSeconds int |
| 30 | + warmupSeconds int |
| 31 | + threads int |
| 32 | + gomaxprocs int |
| 33 | + flagKey string |
| 34 | + clientSecret string |
| 35 | + apiClientID string |
| 36 | + apiClientSecret string |
| 37 | + pollInterval int |
| 38 | + ) |
| 39 | + |
| 40 | + flag.StringVar(&mockGRPCAddr, "mock-grpc", "localhost:8081", "mock support server gRPC address host:port") |
| 41 | + flag.IntVar(&durationSeconds, "duration", 30, "benchmark duration in seconds (excludes warmup)") |
| 42 | + flag.IntVar(&warmupSeconds, "warmup", 5, "warmup duration in seconds before measurement") |
| 43 | + flag.IntVar(&threads, "threads", runtime.NumCPU(), "number of concurrent worker goroutines") |
| 44 | + flag.IntVar(&gomaxprocs, "gomaxprocs", 0, "set GOMAXPROCS (0=leave default)") |
| 45 | + flag.StringVar(&flagKey, "flag", "example-flag", "flag key (without 'flags/' prefix)") |
| 46 | + flag.StringVar(&clientSecret, "client-secret", "secret", "client secret for request signing") |
| 47 | + flag.StringVar(&apiClientID, "api-client-id", "mock-client", "API client ID for token requests") |
| 48 | + flag.StringVar(&apiClientSecret, "api-client-secret", "mock-secret", "API client secret for token requests") |
| 49 | + flag.IntVar(&pollInterval, "poll-interval", 10, "resolver state/log poll interval in seconds (env override)") |
| 50 | + flag.Parse() |
| 51 | + |
| 52 | + if gomaxprocs > 0 { |
| 53 | + runtime.GOMAXPROCS(gomaxprocs) |
| 54 | + } |
| 55 | + if threads < 1 { |
| 56 | + threads = 1 |
| 57 | + } |
| 58 | + if warmupSeconds < 0 { |
| 59 | + warmupSeconds = 0 |
| 60 | + } |
| 61 | + if durationSeconds < 1 { |
| 62 | + durationSeconds = 1 |
| 63 | + } |
| 64 | + |
| 65 | + // Ensure state/log polling is exercised during the run |
| 66 | + // os.Setenv("CONFIDENCE_RESOLVER_POLL_INTERVAL_SECONDS", fmt.Sprintf("%d", pollInterval)) |
| 67 | + |
| 68 | + ctx := context.Background() |
| 69 | + |
| 70 | + // Build a provider wired to the mock server via ConnFactory. The factory ignores the |
| 71 | + // target passed by the provider and always dials the mock address with insecure creds, |
| 72 | + // while preserving any supplied interceptors (e.g., JWT auth). |
| 73 | + connFactory := func(ctx context.Context, _ string, defaultOpts []grpc.DialOption) (grpc.ClientConnInterface, error) { |
| 74 | + // Keep the default options (notably auth interceptors), but ensure we use insecure transport |
| 75 | + // to match the mock server and override any TLS transport credentials. |
| 76 | + opts := append([]grpc.DialOption{}, defaultOpts...) |
| 77 | + opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 78 | + return grpc.NewClient(mockGRPCAddr, opts...) |
| 79 | + } |
| 80 | + |
| 81 | + provider, err := confidence.NewProvider(ctx, confidence.ProviderConfig{ |
| 82 | + APIClientID: apiClientID, |
| 83 | + APIClientSecret: apiClientSecret, |
| 84 | + ClientSecret: clientSecret, |
| 85 | + ConnFactory: connFactory, |
| 86 | + }) |
| 87 | + if err != nil { |
| 88 | + fmt.Fprintf(os.Stderr, "failed to create provider: %v\n", err) |
| 89 | + os.Exit(1) |
| 90 | + } |
| 91 | + |
| 92 | + // Minimal evaluation context; you can extend with attributes to exercise targeting |
| 93 | + evalCtx := openfeature.FlattenedContext{"targetingKey": "tutorial_visitor", "visitor_id": "tutorial_visitor"} |
| 94 | + |
| 95 | + // Prepare cancellation on SIGINT/SIGTERM |
| 96 | + sigCh := make(chan os.Signal, 1) |
| 97 | + signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM) |
| 98 | + |
| 99 | + // Warmup (abort on first error) |
| 100 | + if warmupSeconds > 0 { |
| 101 | + warmupCtx, cancel := context.WithTimeout(ctx, time.Duration(warmupSeconds)*time.Second) |
| 102 | + var warm stats |
| 103 | + runWorkers(warmupCtx, provider, flagKey, evalCtx, threads, &warm, cancel, true) |
| 104 | + cancel() |
| 105 | + if atomic.LoadUint64(&warm.errors) > 0 { |
| 106 | + fmt.Fprintf(os.Stderr, "aborting: error during warmup\n") |
| 107 | + os.Exit(1) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Measurement |
| 112 | + measureCtx, cancelMeasure := context.WithTimeout(ctx, time.Duration(durationSeconds)*time.Second) |
| 113 | + defer cancelMeasure() |
| 114 | + |
| 115 | + var s stats |
| 116 | + // Abort early on signal |
| 117 | + go func() { |
| 118 | + select { |
| 119 | + case <-sigCh: |
| 120 | + cancelMeasure() |
| 121 | + case <-measureCtx.Done(): |
| 122 | + } |
| 123 | + }() |
| 124 | + |
| 125 | + start := time.Now() |
| 126 | + runWorkers(measureCtx, provider, flagKey, evalCtx, threads, &s, cancelMeasure, true) |
| 127 | + elapsed := time.Since(start) |
| 128 | + provider.Shutdown() |
| 129 | + |
| 130 | + completed := atomic.LoadUint64(&s.completed) |
| 131 | + errs := atomic.LoadUint64(&s.errors) |
| 132 | + qps := float64(completed) / elapsed.Seconds() |
| 133 | + |
| 134 | + fmt.Printf("flag=%s threads=%d duration=%s ops=%d errors=%d throughput=%.0f ops/s\n", |
| 135 | + flagKey, threads, elapsed.Truncate(time.Millisecond), completed, errs, qps) |
| 136 | +} |
| 137 | + |
| 138 | +func runWorkers(ctx context.Context, provider *confidence.LocalResolverProvider, flagKey string, evalCtx openfeature.FlattenedContext, threads int, s *stats, cancel context.CancelFunc, abortOnError bool) { |
| 139 | + wg := sync.WaitGroup{} |
| 140 | + wg.Add(threads) |
| 141 | + for i := 0; i < threads; i++ { |
| 142 | + go func() { |
| 143 | + defer wg.Done() |
| 144 | + for { |
| 145 | + select { |
| 146 | + case <-ctx.Done(): |
| 147 | + return |
| 148 | + default: |
| 149 | + res := provider.ObjectEvaluation(context.Background(), flagKey, nil, evalCtx) |
| 150 | + if s != nil { |
| 151 | + atomic.AddUint64(&s.completed, 1) |
| 152 | + // fmt.Printf("reason %s", res.Reason) |
| 153 | + if res.Reason == openfeature.ErrorReason { |
| 154 | + atomic.AddUint64(&s.errors, 1) |
| 155 | + if abortOnError && cancel != nil { |
| 156 | + cancel() |
| 157 | + return |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + } |
| 162 | + } |
| 163 | + }() |
| 164 | + } |
| 165 | + wg.Wait() |
| 166 | +} |
0 commit comments