|
| 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 | + |
1 | 4 | package main
|
2 | 5 |
|
3 | 6 | import (
|
4 | 7 | "context"
|
| 8 | + "encoding/json" |
5 | 9 | "fmt"
|
| 10 | + "io/ioutil" |
| 11 | + "log" |
| 12 | + "os" |
| 13 | + "path" |
6 | 14 | "time"
|
7 | 15 |
|
8 | 16 | "github.com/optimizely/go-sdk/optimizely/client"
|
| 17 | + "github.com/optimizely/go-sdk/optimizely/decision" |
9 | 18 | "github.com/optimizely/go-sdk/optimizely/entities"
|
10 | 19 | "github.com/optimizely/go-sdk/optimizely/logging"
|
| 20 | + "github.com/optimizely/go-sdk/optimizely/notification" |
| 21 | + |
| 22 | + "github.com/pkg/profile" |
11 | 23 | )
|
12 | 24 |
|
13 |
| -func main() { |
| 25 | +// stressTest has everything that test app has. it is used to run profile |
| 26 | +func stressTest() { |
| 27 | + /* |
| 28 | + For the test app, the biggest json file is used with 100 entities. |
| 29 | + DATAFILES_DIR has to be set to point to the path where 100_entities.json is located. |
| 30 | + */ |
| 31 | + type isFeatureEnabledRequestParams struct { |
| 32 | + FeatureKey string `json:"feature_flag_key"` |
| 33 | + UserID string `json:"user_id"` |
| 34 | + Attributes map[string]interface{} `json:"attributes"` |
| 35 | + } |
| 36 | + |
| 37 | + type Context struct { |
| 38 | + CustomEventDispatcher string `json:"custom_event_dispatcher"` |
| 39 | + RequestID string `json:"request_id"` |
| 40 | + UserProfileService string `json:"user_profile_service"` |
| 41 | + Datafile string `json:"datafile"` |
| 42 | + DispatchedEvents []map[string]interface{} `json:"dispatched_events"` |
| 43 | + } |
| 44 | + |
| 45 | + strBytes := []byte(` { "context": { |
| 46 | + "datafile": "100_entities.json", |
| 47 | + "custom_event_dispatcher": "ProxyEventDispatcher", |
| 48 | + "request_id": "4e3e37e3-c7ae-4cb6-bbb5-f6ef93c84d43", |
| 49 | + "user_profile_service": "NoOpService", |
| 50 | + "user_profiles": [], |
| 51 | + "with_listener": [] |
| 52 | + }, |
| 53 | + "user_id": "test_user_1", |
| 54 | + "feature_flag_key": "feature_5", |
| 55 | + "attributes": { |
| 56 | + "attr_5": "testvalue" |
| 57 | + } |
| 58 | + }`) |
| 59 | + |
| 60 | + var params isFeatureEnabledRequestParams |
| 61 | + err := json.Unmarshal(strBytes, ¶ms) |
| 62 | + if err != nil { |
| 63 | + log.Fatal(err) |
| 64 | + } |
| 65 | + |
| 66 | + var requestBodyMap map[string]*json.RawMessage |
| 67 | + |
| 68 | + err = json.Unmarshal(strBytes, &requestBodyMap) |
| 69 | + if err != nil { |
| 70 | + log.Fatal(err) |
| 71 | + } |
| 72 | + |
| 73 | + var fscCtx Context |
| 74 | + err = json.Unmarshal(*requestBodyMap["context"], &fscCtx) |
| 75 | + if err != nil { |
| 76 | + log.Fatal(err) |
| 77 | + } |
| 78 | + |
| 79 | + var datafileDir = path.Join(os.Getenv("DATAFILES_DIR"), fscCtx.Datafile) |
| 80 | + |
| 81 | + datafile, err := ioutil.ReadFile(datafileDir) |
| 82 | + if err != nil { |
| 83 | + log.Fatal(err) |
| 84 | + } |
| 85 | + |
| 86 | + optlyClient := &client.OptimizelyFactory{ |
| 87 | + Datafile: datafile, |
| 88 | + } |
| 89 | + |
| 90 | + user := entities.UserContext{ |
| 91 | + ID: params.UserID, |
| 92 | + Attributes: params.Attributes, |
| 93 | + } |
| 94 | + |
| 95 | + // Creates a default, canceleable context |
| 96 | + notificationCenter := notification.NewNotificationCenter() |
| 97 | + decisionService := decision.NewCompositeService(notificationCenter) |
| 98 | + |
| 99 | + clientOptions := client.Options{ |
| 100 | + DecisionService: decisionService, |
| 101 | + } |
| 102 | + clientApp, err := optlyClient.ClientWithOptions(clientOptions) |
| 103 | + if err != nil { |
| 104 | + log.Fatal(err) |
| 105 | + } |
| 106 | + |
| 107 | + clientApp.IsFeatureEnabled(params.FeatureKey, user) |
| 108 | +} |
| 109 | + |
| 110 | +func examples() { |
14 | 111 | logging.SetLogLevel(logging.LogLevelDebug)
|
15 | 112 | optimizelyFactory := &client.OptimizelyFactory{
|
16 | 113 | SDKKey: "4SLpaJA1r1pgE6T2CoMs9q",
|
@@ -80,3 +177,26 @@ func main() {
|
80 | 177 | enabled, _ = app.IsFeatureEnabled("mutext_feat", user)
|
81 | 178 | fmt.Printf("Is feature enabled? %v\n", enabled)
|
82 | 179 | }
|
| 180 | + |
| 181 | +var RunMemProfile = "false" |
| 182 | +var RunCPUProfile = "false" |
| 183 | + |
| 184 | +func main() { |
| 185 | + |
| 186 | + if RunMemProfile == "true" || RunCPUProfile == "true" { |
| 187 | + |
| 188 | + const RUN_NUMBER = 6 |
| 189 | + if RunMemProfile == "true" { |
| 190 | + defer profile.Start(profile.MemProfile, profile.ProfilePath("."), profile.MemProfileRate(1)).Stop() |
| 191 | + } else if RunCPUProfile == "true" { |
| 192 | + defer profile.Start(profile.CPUProfile, profile.ProfilePath(".")).Stop() |
| 193 | + } |
| 194 | + |
| 195 | + for i := 0; i < RUN_NUMBER; i++ { |
| 196 | + stressTest() |
| 197 | + } |
| 198 | + } else { |
| 199 | + examples() |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments