Skip to content

Commit 3b48883

Browse files
Merge branch 'pawel/track_event_bug' of github.com:optimizely/go-sdk into pawel/track_event_bug
2 parents 434f008 + c8f74ba commit 3b48883

File tree

8 files changed

+13145
-8
lines changed

8 files changed

+13145
-8
lines changed

examples/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Optimizely Go SDK Examples
2+
3+
Examples of client instantiation, datafile management and Feature enabled are provided in main.go
4+
5+
# Profiling
6+
7+
Prerequisite:
8+
1. `go get github.com/pkg/profile`
9+
2. DATAFILES_DIR env var has to be set to point to the path where 100_entities.json is located
10+
11+
To get profiles:
12+
* CPU profile. Execute: `go build -ldflags "-X main.ProfileMode=cpu" benchmark/main.go && ./main_profile_feature`. It will create cpu.pprof file in your current directory. Then run: `go tool pprof -http=:8080 cpu.pprof` and profile cpu usage using web browser.
13+
* Memory profile. Execute: `go build -ldflags "-X main.ProfileMode=mem" benchmark/main.go && ./main_profile_feature`. It will create mem.pprof file in your current directory. Then run: `go tool pprof -http=:8080 mem.pprof` and profile memory using browser.

examples/benchmark/main.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// to run the CPU profiling: go build -ldflags "-X main.ProfileMode=mem" main.go && ./main
2+
3+
package main
4+
5+
import (
6+
"io/ioutil"
7+
"log"
8+
"os"
9+
"path"
10+
11+
"github.com/optimizely/go-sdk/optimizely/client"
12+
"github.com/optimizely/go-sdk/optimizely/decision"
13+
"github.com/optimizely/go-sdk/optimizely/entities"
14+
"github.com/optimizely/go-sdk/optimizely/notification"
15+
16+
"github.com/pkg/profile"
17+
)
18+
19+
func stressTest() {
20+
/*
21+
For the test app, the biggest json file is used with 100 entities.
22+
DATAFILES_DIR has to be set to point to the path where 100_entities.json is located.
23+
*/
24+
25+
var datafileDir = path.Join(os.Getenv("DATAFILES_DIR"), "100_entities.json")
26+
27+
datafile, err := ioutil.ReadFile(datafileDir)
28+
if err != nil {
29+
log.Print(err)
30+
}
31+
32+
optlyClient := &client.OptimizelyFactory{
33+
Datafile: datafile,
34+
}
35+
36+
user := entities.UserContext{
37+
ID: "test_user_1",
38+
Attributes: map[string]interface{}{
39+
"attr_5": "testvalue",
40+
},
41+
}
42+
43+
// Creates a default, canceleable context
44+
notificationCenter := notification.NewNotificationCenter()
45+
decisionService := decision.NewCompositeService(notificationCenter)
46+
47+
clientOptions := client.Options{
48+
DecisionService: decisionService,
49+
}
50+
clientApp, err := optlyClient.ClientWithOptions(clientOptions)
51+
if err != nil {
52+
log.Print(err)
53+
}
54+
55+
clientApp.IsFeatureEnabled("feature_5", user)
56+
}
57+
58+
var ProfileMode = ""
59+
60+
const RUN_NUMBER = 50
61+
62+
func main() {
63+
64+
switch ProfileMode {
65+
case "mem":
66+
defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.MemProfileRate(1)).Stop()
67+
case "cpu":
68+
defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop()
69+
default:
70+
log.Println("ProfileMode should be set to mem or cpu")
71+
72+
}
73+
74+
for i := 0; i < RUN_NUMBER; i++ {
75+
stressTest()
76+
}
77+
78+
}

examples/main.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// to run the CPU profiling: go build -ldflags "-X main.RunCPUProfile=true" main.go && ./main
2+
// to run the Mem profiling: go build -ldflags "-X main.RunMemProfile=true" main.go && ./main
3+
14
package main
25

36
import (
@@ -11,7 +14,15 @@ import (
1114
)
1215

1316
func main() {
17+
1418
logging.SetLogLevel(logging.LogLevelDebug)
19+
user := entities.UserContext{
20+
ID: "mike ng",
21+
Attributes: map[string]interface{}{
22+
"country": "Unknown",
23+
"likes_donuts": true,
24+
},
25+
}
1526
optimizelyFactory := &client.OptimizelyFactory{
1627
SDKKey: "4SLpaJA1r1pgE6T2CoMs9q",
1728
}
@@ -25,14 +36,6 @@ func main() {
2536
return
2637
}
2738

28-
user := entities.UserContext{
29-
ID: "mike ng",
30-
Attributes: map[string]interface{}{
31-
"country": "Unknown",
32-
"likes_donuts": true,
33-
},
34-
}
35-
3639
enabled, _ := app.IsFeatureEnabled("mutext_feat", user)
3740
fmt.Printf("Is feature enabled? %v\n", enabled)
3841

@@ -79,4 +82,5 @@ func main() {
7982

8083
enabled, _ = app.IsFeatureEnabled("mutext_feat", user)
8184
fmt.Printf("Is feature enabled? %v\n", enabled)
85+
8286
}

optimizely/config/datafileprojectconfig/json_parser_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package datafileprojectconfig
1818

1919
import (
2020
"fmt"
21+
"io/ioutil"
2122
"testing"
2223

2324
"github.com/optimizely/go-sdk/optimizely/config/datafileprojectconfig/entities"
@@ -59,3 +60,15 @@ func TestParseDatafilePasses(t *testing.T) {
5960

6061
assert.Equal(t, expectedDatafile, parsedDatafile)
6162
}
63+
64+
func BenchmarkParseDatafilePasses(b *testing.B) {
65+
for n := 0; n < b.N; n++ {
66+
datafile, err := ioutil.ReadFile("test/100_entities.json")
67+
if err != nil {
68+
fmt.Println("error opening file:", err)
69+
}
70+
Parse(datafile)
71+
72+
}
73+
74+
}

0 commit comments

Comments
 (0)