Skip to content

Commit

Permalink
use experimental native mem management; clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed May 12, 2022
1 parent 1c152f2 commit cb059f9
Show file tree
Hide file tree
Showing 17 changed files with 109 additions and 139 deletions.
16 changes: 16 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
plugins {
`maven-publish`
signing
id("io.github.gradle-nexus.publish-plugin") version "1.1.0"
}

allprojects {
group = "com.amplitude"

Expand All @@ -6,3 +12,13 @@ allprojects {
mavenCentral()
}
}

nexusPublishing {
repositories {
sonatype {
stagingProfileId.set(properties["sonatypeStagingProfileId"].toString())
username.set(properties["sonatypeUsername"].toString())
password.set(properties["sonatypePassword"].toString())
}
}
}
2 changes: 1 addition & 1 deletion buildSrc/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
kotlin("jvm") version "1.6.20"
kotlin("jvm") version "1.6.21"
}

repositories {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/Versions.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
object Versions {
const val serializationPlugin = "1.6.20"
const val serializationPlugin = "1.6.21"
const val serializationRuntime = "1.3.2"
const val npmPublishPlugin = "2.0.2"
const val kotlinLint = "10.2.1"
Expand Down
9 changes: 8 additions & 1 deletion c/Makefile
Original file line number Diff line number Diff line change
@@ -1,2 +1,9 @@
demo: main.c
demo: copy
clang main.c libevaluation_interop.dylib -o demo

clean:
rm -f demo

copy:
cp ../evaluation-interop/build/bin/macosX64/releaseShared/libevaluation_interop_api.h .
cp ../evaluation-interop/build/bin/macosX64/releaseShared/libevaluation_interop.dylib .
8 changes: 4 additions & 4 deletions c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@ This is a demo of running evaluations in C using the shared library outputs.

Build the shared library
```
../gradlew build
../gradlew assemble
```

Copy header and shared library into this directory
```
cp ../evaluation-interop/build/bin/macosX64/releaseShared/libevaluation_interop_api.h
cp ../evaluation-interop/build/bin/macosX64/releaseShared/libevaluation_interop.dylib
cp ../evaluation-interop/build/bin/macosX64/releaseShared/libevaluation_interop_api.h .
cp ../evaluation-interop/build/bin/macosX64/releaseShared/libevaluation_interop.dylib .
```

Make the demo binary
```
make demo
make
```

## Run
Expand Down
Binary file modified c/demo
Binary file not shown.
Binary file modified c/libevaluation_interop.dylib
Binary file not shown.
18 changes: 7 additions & 11 deletions c/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
#include "stdio.h"

int main(int argc, char** argv) {
libevaluation_interop_ExportedSymbols* lib = libevaluation_interop_symbols();

libevaluation_interop_ExportedSymbols* lib = libevaluation_interop_symbols();
const char* rules = "[{\"allUsersTargetingConfig\":{\"allocations\":[{\"percentage\":6600,\"weights\":{\"control\":1,\"treatment\":1}}],\"bucketingKey\":\"amplitude_id\",\"conditions\":[],\"name\":\"default-segment\"},\"bucketingKey\":\"amplitude_id\",\"bucketingSalt\":\"xIsm9BUj\",\"customSegmentTargetingConfigs\":[],\"defaultValue\":\"off\",\"enabled\":true,\"flagKey\":\"brian-bug-safari\",\"flagName\":\"brian-bug-safari\",\"flagVersion\":15,\"globalHoldbackBucketingKey\":\"amplitude_id\",\"globalHoldbackPct\":0,\"globalHoldbackSalt\":\"5inHyVr4\",\"mutualExclusionConfig\":{\"bucketingKey\":\"amplitude_id\",\"groupSalt\":\"yYIqQGSY\",\"lowerBound\":0,\"percentage\":5000},\"useStickyBucketing\":false,\"userProperty\":\"[Amplitude][Flag]brian-bug-safari\",\"variants\":[{\"key\":\"control\",\"payload\":{\"asdf\":\"asdf\"}},{\"key\":\"treatment\",\"payload\":[\"array\"]}],\"variantsExclusions\":null,\"variantsInclusions\":null},{\"allUsersTargetingConfig\":{\"allocations\":[{\"percentage\":0,\"weights\":{\"control\":1,\"treatment\":1}}],\"bucketingKey\":\"amplitude_id\",\"conditions\":[],\"name\":\"default-segment\"},\"bucketingKey\":\"amplitude_id\",\"bucketingSalt\":\"LRVo9Day\",\"customSegmentTargetingConfigs\":[],\"defaultValue\":\"off\",\"enabled\":true,\"flagKey\":\"brian-bug-safari-2\",\"flagName\":\"brian-bug-safari-2\",\"flagVersion\":6,\"globalHoldbackBucketingKey\":\"amplitude_id\",\"globalHoldbackPct\":0,\"globalHoldbackSalt\":\"5inHyVr4\",\"mutualExclusionConfig\":{\"bucketingKey\":\"amplitude_id\",\"groupSalt\":\"yYIqQGSY\",\"lowerBound\":5000,\"percentage\":2500},\"useStickyBucketing\":false,\"userProperty\":\"[Experiment]brian-bug-safari-2\",\"variants\":[{\"key\":\"control\",\"payload\":null},{\"key\":\"treatment\",\"payload\":null}],\"variantsExclusions\":null,\"variantsInclusions\":null}]";
const char* user = "{\"amplitude_id\":1234567,\"user_id\":\"[email protected]\",\"device_brand\":\"asus\",\"device_manufacturer\":\"asus\",\"device_model\":\"asus_t00f1\",\"language\":\"spanish(puertorico)\"}";

const char* rules = "[{\"allUsersTargetingConfig\":{\"allocations\":[{\"percentage\":6600,\"weights\":{\"control\":1,\"treatment\":1}}],\"bucketingKey\":\"amplitude_id\",\"conditions\":[],\"name\":\"default-segment\"},\"bucketingKey\":\"amplitude_id\",\"bucketingSalt\":\"xIsm9BUj\",\"customSegmentTargetingConfigs\":[],\"defaultValue\":\"off\",\"enabled\":true,\"flagKey\":\"brian-bug-safari\",\"flagName\":\"brian-bug-safari\",\"flagVersion\":15,\"globalHoldbackBucketingKey\":\"amplitude_id\",\"globalHoldbackPct\":0,\"globalHoldbackSalt\":\"5inHyVr4\",\"mutualExclusionConfig\":{\"bucketingKey\":\"amplitude_id\",\"groupSalt\":\"yYIqQGSY\",\"lowerBound\":0,\"percentage\":5000},\"useStickyBucketing\":false,\"userProperty\":\"[Amplitude][Flag]brian-bug-safari\",\"variants\":[{\"key\":\"control\",\"payload\":{\"asdf\":\"asdf\"}},{\"key\":\"treatment\",\"payload\":[\"array\"]}],\"variantsExclusions\":null,\"variantsInclusions\":null},{\"allUsersTargetingConfig\":{\"allocations\":[{\"percentage\":0,\"weights\":{\"control\":1,\"treatment\":1}}],\"bucketingKey\":\"amplitude_id\",\"conditions\":[],\"name\":\"default-segment\"},\"bucketingKey\":\"amplitude_id\",\"bucketingSalt\":\"LRVo9Day\",\"customSegmentTargetingConfigs\":[],\"defaultValue\":\"off\",\"enabled\":true,\"flagKey\":\"brian-bug-safari-2\",\"flagName\":\"brian-bug-safari-2\",\"flagVersion\":6,\"globalHoldbackBucketingKey\":\"amplitude_id\",\"globalHoldbackPct\":0,\"globalHoldbackSalt\":\"5inHyVr4\",\"mutualExclusionConfig\":{\"bucketingKey\":\"amplitude_id\",\"groupSalt\":\"yYIqQGSY\",\"lowerBound\":5000,\"percentage\":2500},\"useStickyBucketing\":false,\"userProperty\":\"[Experiment]brian-bug-safari-2\",\"variants\":[{\"key\":\"control\",\"payload\":null},{\"key\":\"treatment\",\"payload\":null}],\"variantsExclusions\":null,\"variantsInclusions\":null}]";
const char* response = lib->kotlin.root.evaluate(rules, user);
printf("result: %s\n", response);
lib->DisposeString(response);

const char* user = "{\"amplitude_id\":1234567,\"user_id\":\"[email protected]\",\"device_brand\":\"asus\",\"device_manufacturer\":\"asus\",\"device_model\":\"asus_t00f1\",\"language\":\"spanish(puertorico)\"}";

const char* response = lib->kotlin.root.evaluate(rules, user);

printf("result: %s\n", response);

lib->DisposeString(response);

return 0;
return 0;
}
57 changes: 55 additions & 2 deletions evaluation-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ plugins {
kotlin("multiplatform")
kotlin("plugin.serialization") version Versions.serializationPlugin
`maven-publish`
signing
id("org.jlleitschuh.gradle.ktlint") version Versions.kotlinLint
}

version = "0.0.1"

kotlin {

sourceSets.all {
Expand Down Expand Up @@ -62,3 +61,57 @@ tasks.withType<Wrapper> {
gradleVersion = "7.4.1"
distributionType = Wrapper.DistributionType.ALL
}

// Publishing

version = "0.0.1"

afterEvaluate {
publishing {
publications {
create<MavenPublication>("release") {
from(components["kotlin"])
pom {
name.set("Amplitude Experiment Evaluation")
description.set("Core kotlin multiplatform package for Amplitude Experiment's evaluation.")
url.set("https://github.com/amplitude/experiment-evaluation")
licenses {
license {
name.set("MIT")
url.set("https://opensource.org/licenses/MIT")
distribution.set("repo")
}
}
developers {
developer {
id.set("amplitude")
name.set("Amplitude")
email.set("[email protected]")
}
}
scm {
url.set("https://github.com/amplitude/experiment-evaluation")
developerConnection.set("scm:[email protected]:amplitude/experiment-evaluation.git")
connection.set("scm:[email protected]:amplitude/experiment-evaluation.git")
}
}
}
}
}

signing {
val publishing = extensions.findByType<PublishingExtension>()
val signingKeyId = properties["signingKeyId"]?.toString()
val signingKey = properties["signingKey"]?.toString()
val signingPassword = properties["signingPassword"]?.toString()
useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword)
sign(publishing?.publications)
}

tasks.withType<Sign>().configureEach {
onlyIf { isReleaseBuild }
}
}

val isReleaseBuild: Boolean
get() = properties.containsKey("signingKey")
91 changes: 3 additions & 88 deletions evaluation-interop/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ plugins {
id("org.jlleitschuh.gradle.ktlint") version Versions.kotlinLint
}

version = "0.0.30"

val isDebug = true
val frameworkName = "EvaluationInterop"

kotlin {

sourceSets.all {
Expand All @@ -19,40 +14,13 @@ kotlin {
}

if (isMacOs()) {
macosArm64().binaries {
sharedLib()
// framework {
// baseName = frameworkName
// }
}
macosX64().binaries {
sharedLib()
// framework {
// baseName = frameworkName
// }
}
// iosArm32().binaries.framework { baseName = frameworkName }
// iosArm64().binaries.framework { baseName = frameworkName }
// iosX64().binaries.framework { baseName = frameworkName }
// tvosArm64().binaries.framework { baseName = frameworkName }
// tvosX64().binaries.framework { baseName = frameworkName }
// watchosArm32().binaries.framework { baseName = frameworkName }
// watchosArm64().binaries.framework { baseName = frameworkName }
// watchosX86().binaries.framework { baseName = frameworkName }
// watchosX64().binaries.framework { baseName = frameworkName }
macosArm64().binaries.sharedLib()
macosX64().binaries.sharedLib()
}

linuxArm64().binaries.sharedLib()
linuxX64().binaries.sharedLib()

jvm {
compilations.all {
kotlinOptions.jvmTarget = "1.8"
}
testRuns["test"].executionTask.configure {
useJUnit()
}
}

sourceSets {
val commonMain by getting {
dependencies {
Expand All @@ -73,56 +41,3 @@ tasks.withType<Wrapper> {
gradleVersion = "7.4.1"
distributionType = Wrapper.DistributionType.ALL
}

// tasks["build"].doLast {
//
// // Clear the old xcframework
// exec { commandLine("rm", "-rf", "build/xcframework") }
//
// // Setup fat-frameworks directory for 32-bit and 64-bit platforms with the same arch
// exec { commandLine("mkdir", "-p", "build/fat-frameworks") }
// exec { commandLine("cp", "-R", "build/bin/iosArm64", "build/fat-frameworks/iosArm") }
// exec { commandLine("cp", "-R", "build/bin/watchosArm64", "build/fat-frameworks/watchosArm") }
// exec { commandLine("cp", "-R", "build/bin/watchosX64", "build/fat-frameworks/watchosX") }
//
// // For debug and release frameworks, create fat frameworks and create the final xcframework
// listOf("debugFramework", "releaseFramework").forEach { debugOrReleaseFramework ->
// exec {
// commandLine(
// "lipo", "-create",
// "build/bin/iosArm32/$debugOrReleaseFramework/$frameworkName.framework/$frameworkName",
// "build/bin/iosArm64/$debugOrReleaseFramework/$frameworkName.framework/$frameworkName",
// "-output", "build/fat-frameworks/iosArm/$debugOrReleaseFramework/$frameworkName.framework/$frameworkName"
// )
// }
// exec {
// commandLine(
// "lipo", "-create",
// "build/bin/watchosArm32/$debugOrReleaseFramework/$frameworkName.framework/$frameworkName",
// "build/bin/watchosArm64/$debugOrReleaseFramework/$frameworkName.framework/$frameworkName",
// "-output", "build/fat-frameworks/watchosArm/$debugOrReleaseFramework/$frameworkName.framework/$frameworkName"
// )
// }
// exec {
// commandLine(
// "lipo", "-create",
// "build/bin/watchosX86/$debugOrReleaseFramework/$frameworkName.framework/$frameworkName",
// "build/bin/watchosX64/$debugOrReleaseFramework/$frameworkName.framework/$frameworkName",
// "-output", "build/fat-frameworks/watchosX/$debugOrReleaseFramework/$frameworkName.framework/$frameworkName"
// )
// }
// exec {
// commandLine(
// "xcodebuild", "-create-xcframework",
// "-framework", "build/fat-frameworks/iosArm/$debugOrReleaseFramework/$frameworkName.framework",
// "-framework", "build/bin/iosX64/$debugOrReleaseFramework/$frameworkName.framework",
// "-framework", "build/bin/macosX64/$debugOrReleaseFramework/$frameworkName.framework",
// "-framework", "build/bin/tvosArm64/$debugOrReleaseFramework/$frameworkName.framework",
// "-framework", "build/bin/tvosX64/$debugOrReleaseFramework/$frameworkName.framework",
// "-framework", "build/fat-frameworks/watchosArm/$debugOrReleaseFramework/$frameworkName.framework",
// "-framework", "build/fat-frameworks/watchosX/$debugOrReleaseFramework/$frameworkName.framework",
// "-output", "build/xcframework/$debugOrReleaseFramework/$frameworkName.xcframework"
// )
// }
// }
// }
18 changes: 10 additions & 8 deletions evaluation-interop/src/commonMain/kotlin/Evaluation.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,16 @@ import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString
import kotlinx.serialization.json.Json
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport

@SharedImmutable
internal val format = Json {
ignoreUnknownKeys = true
isLenient = true
coerceInputValues = true
}

@SharedImmutable
internal val engine = EvaluationEngineImpl()

/**
* [rules] is a JSON representation of Array<[FlagConfig]>
Expand All @@ -14,14 +23,7 @@ import kotlin.js.JsExport
* returns a JSON representation of Map<String, [FlagResult]>
*/
@OptIn(ExperimentalJsExport::class, ExperimentalSerializationApi::class)
@JsExport
fun evaluate(rules: String, user: String): String {
val format = Json {
ignoreUnknownKeys = true
isLenient = true
coerceInputValues = true
}
val engine = EvaluationEngineImpl()
val flagsDecoded = format.decodeFromString<List<FlagConfig>>(rules)
val userDecoded = format.decodeFromString<ExperimentUser>(user)
val results = engine.evaluate(flagsDecoded.map { it.convert() }, userDecoded.convert())
Expand Down
2 changes: 1 addition & 1 deletion evaluation-interop/src/commonTest/kotlin/EvaluationTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ const val RULES = """

const val USER = """
{
"user_id":"brian.giori@amplitude.com",
"user_id":"user@company.com",
"device_id":"hfuiewfueiwnquife"
}
"""
Expand Down
2 changes: 1 addition & 1 deletion go/go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module "github.com/amplitude/experiment-evaluation"

go 1.16
go 1.16
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
kotlin.code.style=official
kotlin.native.enableDependencyPropagation=false
kotlin.js.generate.executable.default=false
kotlin.mpp.stability.nowarn=true
kotlin.native.binary.memoryModel=experimental

org.gradle.jvmargs=-Xmx4096m
14 changes: 0 additions & 14 deletions node_modules/.yarn-integrity

This file was deleted.

3 changes: 1 addition & 2 deletions settings.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

rootProject.name = "experiment-evaluation"

include(
":evaluation-core",
":evaluation-interop",
":evaluation-js"
":evaluation-js",
)
4 changes: 0 additions & 4 deletions yarn.lock

This file was deleted.

0 comments on commit cb059f9

Please sign in to comment.