|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace FlagsmithBenchmark; |
| 4 | + |
| 5 | +use Flagsmith\Flagsmith; |
| 6 | +use Flagsmith\Models\Segment; |
| 7 | +use GuzzleHttp\Client; |
| 8 | +use GuzzleHttp\Promise\Create; |
| 9 | +use GuzzleHttp\Promise\PromiseInterface; |
| 10 | +use GuzzleHttp\Psr7\Response; |
| 11 | +use Psr\Http\Message\RequestInterface; |
| 12 | + |
| 13 | +/** |
| 14 | + * Benchmarks a locally evaluating SDK client against environment documents tiered by size. |
| 15 | + */ |
| 16 | +class SdkBenchmark |
| 17 | +{ |
| 18 | + public const ENVIRONMENTS = ['small', 'medium', 'large']; |
| 19 | + public const BENCHMARKS = [ |
| 20 | + 'getEnvironmentFlags', |
| 21 | + 'getIdentityFlagsWithIdentityOverrides', |
| 22 | + 'getIdentityFlagsNoTraits', |
| 23 | + 'getIdentityFlagsWithTraits', |
| 24 | + 'getIdentitySegmentsWithTraits', |
| 25 | + ]; |
| 26 | + |
| 27 | + private const IDENTITY_OVERRIDES_IDENTIFIER = 'identity-override-1'; |
| 28 | + private const IDENTIFIER = 'user-1'; |
| 29 | + private const TRAITS = [ |
| 30 | + 'plan' => 'plan-7', |
| 31 | + 'age' => 30, |
| 32 | + 'country' => 'GB', |
| 33 | + 'version' => '2.5.1', |
| 34 | + 'email' => 'jane@example.com', |
| 35 | + 'user_id' => 12, |
| 36 | + 'beta' => true, |
| 37 | + 'score' => 95, |
| 38 | + 'tier' => 'pro', |
| 39 | + 'device' => 'iPhone 15', |
| 40 | + ]; |
| 41 | + |
| 42 | + private Flagsmith $client; |
| 43 | + |
| 44 | + /** |
| 45 | + * Build a locally evaluating client serving the environment document from memory. |
| 46 | + */ |
| 47 | + public function __construct(string $environmentsDir, string $environment) |
| 48 | + { |
| 49 | + $environmentDocument = file_get_contents("{$environmentsDir}/{$environment}.json"); |
| 50 | + |
| 51 | + $this->client = (new Flagsmith( |
| 52 | + apiKey: 'ser.benchmark', |
| 53 | + host: 'http://flagsmith.benchmark/api/v1/', |
| 54 | + environmentTtl: 86400, // Refreshed by benchmarks only |
| 55 | + ))->withClient(new Client([ |
| 56 | + 'handler' => static fn (RequestInterface $request): PromiseInterface => Create::promiseFor( |
| 57 | + new Response(200, ['Content-Type' => 'application/json'], $environmentDocument), |
| 58 | + ), |
| 59 | + ])); |
| 60 | + $this->client->updateEnvironment(); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Get a feature value from the environment flags. |
| 65 | + */ |
| 66 | + public function getEnvironmentFlags(): mixed |
| 67 | + { |
| 68 | + return $this->client->getEnvironmentFlags()->getFeatureValue('feature_1'); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Check a feature for an identity with identity overrides. |
| 73 | + */ |
| 74 | + public function getIdentityFlagsWithIdentityOverrides(): bool |
| 75 | + { |
| 76 | + return $this->client->getIdentityFlags(self::IDENTITY_OVERRIDES_IDENTIFIER)->isFeatureEnabled('feature_8'); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Get a feature value for an identity without traits. |
| 81 | + */ |
| 82 | + public function getIdentityFlagsNoTraits(): mixed |
| 83 | + { |
| 84 | + return $this->client->getIdentityFlags(self::IDENTIFIER)->getFeatureValue('feature_1'); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * Get a feature value for an identity with traits. |
| 89 | + */ |
| 90 | + public function getIdentityFlagsWithTraits(): mixed |
| 91 | + { |
| 92 | + return $this->client->getIdentityFlags(self::IDENTIFIER, (object) self::TRAITS)->getFeatureValue('feature_1'); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * List the segments of an identity with traits. |
| 97 | + * @return array<Segment> |
| 98 | + */ |
| 99 | + public function getIdentitySegmentsWithTraits(): array |
| 100 | + { |
| 101 | + return $this->client->getIdentitySegments(self::IDENTIFIER, (object) self::TRAITS); |
| 102 | + } |
| 103 | +} |
0 commit comments