Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .github/workflows/codspeed.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ permissions:
jobs:
benchmarks:
name: Run benchmarks
runs-on: ubuntu-latest
runs-on: ${{ vars.CODSPEED_RUNS_ON }}

steps:
- name: Checkout repository
Expand All @@ -29,8 +29,11 @@ jobs:
node-version-file: .nvmrc
cache: npm

- name: Build benchmarks
run: make benchmark-build

- name: Run benchmarks
uses: CodSpeedHQ/action@61efe07e57e55bedeab750c434504de272a56e8c # v5.6.1
with:
mode: simulation
run: make benchmark
mode: ${{ vars.CODSPEED_MODE }}
Comment thread
emyller marked this conversation as resolved.
Resolved
run: make benchmark-run
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[submodule "tests/engine/engine-tests/engine-test-data"]
path = tests/engine/engine-tests/engine-test-data
url = git@github.com:Flagsmith/engine-test-data.git
branch = v3.7.0
branch = v3.12.1
[submodule "tests/engine/engine-tests/sdk-benchmarks"]
path = tests/engine/engine-tests/sdk-benchmarks
url = https://github.com/Flagsmith/sdk-benchmarks.git
Expand Down
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,13 @@ $(SDK_BENCHMARKS)/environments:
node_modules/.package-lock.json: package-lock.json
npm ci

.PHONY: benchmark
benchmark: node_modules/.package-lock.json | $(SDK_BENCHMARKS)/environments
.PHONY: benchmark benchmark-build benchmark-run
benchmark:
$(MAKE) benchmark-build
$(MAKE) benchmark-run

# Kept apart from benchmark-run: CodSpeed's simulation mode runs its whole command under Valgrind
benchmark-build: node_modules/.package-lock.json | $(SDK_BENCHMARKS)/environments

benchmark-run: | $(SDK_BENCHMARKS)/environments
SDK_BENCHMARKS_DIR=$(SDK_BENCHMARKS) npx vitest bench --run --config vitest.config.benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,26 @@ export type FeaturePriority = number;
* Feature overrides for the segment.
*/
export type Overrides = FeatureContext[];
/**
* Feature name.
*/
export type Name3 = string;
/**
* Indicates if the feature flag is enabled.
*/
export type Enabled1 = boolean;
/**
* Feature flag value.
*/
export type Value4 = string | number | boolean | null;
/**
* Reason for the feature flag evaluation.
*/
export type Reason = string;
/**
* A stable identifier of the multivariate variant the identity was bucketed into: the variant's key, "control" for the control bucket, or null when no multivariate split applied (a standard feature, an unkeyed variant, or evaluation without an identity).
*/
export type Variant = string | null;

/**
* A context object containing the necessary information to evaluate Flagsmith feature flags.
Expand All @@ -140,6 +160,7 @@ export interface EvaluationContext {
identity?: IdentityContext | null;
segments?: Segments;
features?: Features;
flags?: FlagResults;
[k: string]: unknown;
}
/**
Expand Down Expand Up @@ -250,3 +271,24 @@ export interface SegmentMetadata {
export interface Features {
[k: string]: FeatureContext;
}
/**
* Results of features already evaluated for this context, mapped by feature name. Populated by the engine as evaluation progresses, so that segment conditions may reference `$.flags.<feature name>.enabled`, `.value` and `.variant` to express feature dependencies.
*/
export interface FlagResults {
[k: string]: FlagResult;
}
export interface FlagResult {
name: Name3;
enabled: Enabled1;
value: Value4;
reason: Reason;
variant: Variant;
metadata?: FeatureMetadata1;
[k: string]: unknown;
}
/**
* Additional metadata associated with the feature.
*/
export interface FeatureMetadata1 {
[k: string]: unknown;
}
40 changes: 36 additions & 4 deletions flagsmith-engine/evaluation/evaluationContext/mappers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { EnvironmentModel } from '../../environments/models.js';
import { IdentityModel } from '../../identities/models.js';
import { TraitModel } from '../../identities/traits/models.js';
import { FeatureStateModel } from '../../features/models.js';
import { SegmentRuleModel } from '../../segments/models.js';
import { IDENTITY_OVERRIDE_SEGMENT_NAME } from '../../segments/constants.js';
import { createHash } from 'node:crypto';
import { uuidToBigInt } from '../../features/util.js';
Expand All @@ -23,7 +24,10 @@ export function getEvaluationContext(
overrideTraits?: TraitModel[],
isEnvironmentEvaluation: boolean = false
): GenericEvaluationContext {
const environmentContext = mapEnvironmentModelToEvaluationContext(environment);
const environmentContext = mapEnvironmentModelToEvaluationContext(
environment,
isEnvironmentEvaluation
);
if (isEnvironmentEvaluation) {
return environmentContext;
}
Expand All @@ -39,8 +43,30 @@ export function getEvaluationContext(
return context;
}

const IDENTITY_FREE_PROPERTY_PREFIXES = [
'$.environment.',
'$.environment[',
'$.flags.',
'$.flags['
];

/**
* Whether segment rules can be evaluated without an identity, i.e. they read no traits nor identity.
*/
function isIdentityFree(rules: SegmentRuleModel[]): boolean {
return rules.every(
rule =>
rule.conditions.every(condition =>
IDENTITY_FREE_PROPERTY_PREFIXES.some(prefix =>
condition.property?.startsWith(prefix)
)
) && isIdentityFree(rule.rules)
);
}

function mapEnvironmentModelToEvaluationContext(
environment: EnvironmentModel
environment: EnvironmentModel,
isEnvironmentEvaluation: boolean
): GenericEvaluationContext {
const environmentContext: EnvironmentContext = {
key: environment.apiKey,
Expand All @@ -64,6 +90,7 @@ function mapEnvironmentModelToEvaluationContext(

const segmentOverrides: SegmentsWithMetadata<SDKSegmentMetadata> = {};
for (const segment of environment.project.segments) {
if (isEnvironmentEvaluation && !isIdentityFree(segment.rules)) continue;
segmentOverrides[segment.id.toString()] = {
key: segment.id.toString(),
name: segment.name,
Expand All @@ -90,7 +117,11 @@ function mapEnvironmentModelToEvaluationContext(
}

let identityOverrideSegments: SegmentsWithMetadata<SDKSegmentMetadata> = {};
if (environment.identityOverrides && environment.identityOverrides.length > 0) {
if (
!isEnvironmentEvaluation &&
environment.identityOverrides &&
environment.identityOverrides.length > 0
) {
identityOverrideSegments = mapIdentityOverridesToSegments(environment.identityOverrides);
}

Expand Down Expand Up @@ -128,7 +159,8 @@ function mapFeatureStateVariants(fs: FeatureStateModel) {
? fs.multivariateFeatureStateValues.map(mv => ({
value: mv.multivariateFeatureOption.value,
weight: mv.percentageAllocation,
priority: mv.id ?? uuidToBigInt(mv.mvFsValueUuid)
priority: mv.id ?? uuidToBigInt(mv.mvFsValueUuid),
...(mv.multivariateFeatureOption.key && { key: mv.multivariateFeatureOption.key })
}))
: undefined;
}
Expand Down
2 changes: 2 additions & 0 deletions flagsmith-engine/features/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ export const CONSTANTS = {
STANDARD: 'STANDARD',
MULTIVARIATE: 'MULTIVARIATE'
};

export const CONTROL_VARIANT = 'control';
4 changes: 3 additions & 1 deletion flagsmith-engine/features/models.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,12 @@ export class FeatureModel {
export class MultivariateFeatureOptionModel {
value: any;
id: number | undefined;
key: string | undefined;

constructor(value: any, id?: number) {
constructor(value: any, id?: number, key?: string) {
this.value = value;
this.id = id;
this.key = key;
}
}

Expand Down
1 change: 1 addition & 0 deletions flagsmith-engine/features/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum TARGETING_REASONS {
DEFAULT = 'DEFAULT',
STATIC = 'STATIC',
TARGETING_MATCH = 'TARGETING_MATCH',
SPLIT = 'SPLIT'
}
3 changes: 2 additions & 1 deletion flagsmith-engine/features/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export function buildFeatureStateModel(featuresStateModelJSON: any): FeatureStat
? featuresStateModelJSON.multivariate_feature_state_values.map((fsv: any) => {
const featureOption = new MultivariateFeatureOptionModel(
fsv.multivariate_feature_option.value,
fsv.multivariate_feature_option.id
fsv.multivariate_feature_option.id,
fsv.multivariate_feature_option.key ?? undefined
);
return new MultivariateFeatureStateValueModel(
featureOption,
Expand Down
Loading
Loading