-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* init performance monitor package * handle new rmb call * resolve comments: - introduce Task interface - remove testing code - use redis connection from pool - unexpose some methods and fields - update running zos node docs & add example for using perf * resolve comments: - use redis pool correctly - add GetAll method - better naming for tasks with prefix `perf.` - use unix date * update go.mod file * - add new handler for getAll method - reuse Get code * run tasks if it has no results stored, fix getAll method * run the tasks in separate routines and use the redis pool pointer * removing the wg and log the errors in the routine
- Loading branch information
1 parent
aa6efe6
commit 4944c63
Showing
11 changed files
with
317 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Perf | ||
|
||
Perf is a performance test module that is scheduled to run and cache those tests results in redis which can be retrieved later over RMB call. | ||
|
||
Perf tests are monitored by `noded` service from zos modules. | ||
|
||
### Usage Example | ||
|
||
1. Create a task that implement `Task` interface | ||
|
||
```go | ||
type LoggingTask struct { | ||
TaskID string | ||
Schedule string // should be in cron syntax with seconds ("* 0 * * * *") | ||
} | ||
|
||
func (t *LoggingTask) ID() string { | ||
return t.TaskID | ||
} | ||
|
||
func (t *LoggingTask) Cron() string { | ||
return t.Schedule | ||
} | ||
|
||
// a simple task that returns a string with the current time | ||
func (t *LoggingTask) Run(ctx context.Context) (interface{}, error) { | ||
result := fmt.Sprintf("time is: %v", time.Now()) | ||
return result, nil | ||
} | ||
``` | ||
|
||
2. Add the created task to scheduler | ||
|
||
```go | ||
perfMon.AddTask(&perf.LoggingTask{ | ||
TaskID: "LoggingTask", | ||
Schedule: "* 0 * * * *", // when minutes is 0 (aka: every hour) | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package perf | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/garyburd/redigo/redis" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
const ( | ||
moduleName = "perf" | ||
) | ||
|
||
var ( | ||
ErrResultNotFound = errors.New("result not found") | ||
) | ||
|
||
// TaskResult the result test schema | ||
type TaskResult struct { | ||
Name string `json:"name"` | ||
Timestamp uint64 `json:"timestamp"` | ||
Result interface{} `json:"result"` | ||
} | ||
|
||
// generateKey is helper method to add moduleName as prefix for the taskName | ||
func generateKey(taskName string) string { | ||
return fmt.Sprintf("%s.%s", moduleName, taskName) | ||
} | ||
|
||
// setCache set result in redis | ||
func (pm *PerformanceMonitor) setCache(ctx context.Context, result TaskResult) error { | ||
data, err := json.Marshal(result) | ||
if err != nil { | ||
return errors.Wrap(err, "failed to marshal data to JSON") | ||
} | ||
|
||
conn := pm.pool.Get() | ||
defer conn.Close() | ||
|
||
_, err = conn.Do("SET", generateKey(result.Name), data) | ||
return err | ||
} | ||
|
||
// get directly gets result for some key | ||
func get(conn redis.Conn, key string) (TaskResult, error) { | ||
var res TaskResult | ||
|
||
data, err := conn.Do("GET", key) | ||
if err != nil { | ||
return res, errors.Wrap(err, "failed to get the result") | ||
} | ||
|
||
if data == nil { | ||
return res, ErrResultNotFound | ||
} | ||
|
||
err = json.Unmarshal(data.([]byte), &res) | ||
if err != nil { | ||
return res, errors.Wrap(err, "failed to unmarshal data from json") | ||
} | ||
|
||
return res, nil | ||
} | ||
|
||
// Get gets data from redis | ||
func (pm *PerformanceMonitor) Get(taskName string) (TaskResult, error) { | ||
conn := pm.pool.Get() | ||
defer conn.Close() | ||
return get(conn, generateKey(taskName)) | ||
} | ||
|
||
// GetAll gets the results for all the tests with moduleName as prefix | ||
func (pm *PerformanceMonitor) GetAll() ([]TaskResult, error) { | ||
var res []TaskResult | ||
|
||
conn := pm.pool.Get() | ||
defer conn.Close() | ||
|
||
var keys []string | ||
|
||
cursor := 0 | ||
for { | ||
values, err := redis.Values(conn.Do("SCAN", cursor, "MATCH", generateKey("*"))) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
_, err = redis.Scan(values, &cursor, &keys) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
for _, key := range keys { | ||
result, err := get(conn, key) | ||
if err != nil { | ||
continue | ||
} | ||
res = append(res, result) | ||
} | ||
|
||
if cursor == 0 { | ||
break | ||
} | ||
|
||
} | ||
return res, nil | ||
} | ||
|
||
// exists check if a key exists | ||
func (pm *PerformanceMonitor) exists(key string) (bool, error) { | ||
conn := pm.pool.Get() | ||
defer conn.Close() | ||
|
||
ok, err := redis.Bool(conn.Do("EXISTS", generateKey(key))) | ||
if err != nil { | ||
return false, errors.Wrapf(err, "error checking if key %s exists", generateKey(key)) | ||
} | ||
return ok, nil | ||
} |
Oops, something went wrong.