Skip to content

Commit

Permalink
add call data docs and clean up code
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Oct 25, 2020
1 parent 10117a4 commit 0a1938f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions runner/call_template_data.go → runner/calldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const charset = "abcdefghijklmnopqrstuvwxyz" +
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))

// call template data
// CallData represents contextualized data available for templating
type CallData struct {
WorkerID string // unique worker ID
RequestNumber int64 // unique incremented request number for each request
Expand All @@ -42,7 +42,7 @@ var tmplFuncMap = template.FuncMap{
"randomString": randomString,
}

// newCallData returns new callData
// newCallData returns new CallData
func newCallData(
mtd *desc.MethodDescriptor,
funcs template.FuncMap,
Expand Down
6 changes: 3 additions & 3 deletions runner/call_template_data_test.go → runner/calldata_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/assert"
)

func TestCallTemplateData_New(t *testing.T) {
func TestCallData_New(t *testing.T) {
md, err := protodesc.GetMethodDescFromProto("helloworld.Greeter/SayHello", "../testdata/greeter.proto", []string{})
assert.NoError(t, err)
assert.NotNil(t, md)
Expand All @@ -36,7 +36,7 @@ func TestCallTemplateData_New(t *testing.T) {
assert.Equal(t, 36, len(ctd.UUID))
}

func TestCallTemplateData_ExecuteData(t *testing.T) {
func TestCallData_ExecuteData(t *testing.T) {
md, err := protodesc.GetMethodDescFromProto("helloworld.Greeter/SayHello", "../testdata/greeter.proto", []string{})
assert.NoError(t, err)
assert.NotNil(t, md)
Expand Down Expand Up @@ -87,7 +87,7 @@ func TestCallTemplateData_ExecuteData(t *testing.T) {
}
}

func TestCallTemplateData_ExecuteMetadata(t *testing.T) {
func TestCallData_ExecuteMetadata(t *testing.T) {
md, err := protodesc.GetMethodDescFromProto("helloworld.Greeter/SayHello", "../testdata/greeter.proto", []string{})
assert.NoError(t, err)
assert.NotNil(t, md)
Expand Down
7 changes: 6 additions & 1 deletion runner/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ import (
"google.golang.org/grpc/credentials"
)

// BinaryDataFunc is a function that can be used for provide binary data for request programatically.
// MethodDescriptor of the call is passed to the data function.
// CallData for the request is passed and can be used to access worker id, request number, etc...
type BinaryDataFunc func(mtd *desc.MethodDescriptor, callData *CallData) []byte

// RunConfig represents the request Configs
type RunConfig struct {
// call settings
Expand Down Expand Up @@ -62,7 +67,7 @@ type RunConfig struct {
data []byte

// data func
dataFunc func(mtd *desc.MethodDescriptor, callData *CallData) []byte
dataFunc BinaryDataFunc

binary bool
metadata []byte
Expand Down
29 changes: 25 additions & 4 deletions www/docs/calldata.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
id: calldata
title: Call Template Data
title: Call Data
---

Data and metadata can specify [template actions](https://golang.org/pkg/text/template/) that will be parsed and evaluated at every request. Each request gets a new instance of the data. The available variables / actions are:

```go
// call template data
type callTemplateData struct {
// CallData represents contextualized data available for templating
type CallData struct {

// unique worker ID
WorkerID string
Expand Down Expand Up @@ -53,7 +53,7 @@ type callTemplateData struct {
}
```

**Functions**
**Template Functions**

There are also two template functions available:

Expand Down Expand Up @@ -95,3 +95,24 @@ Would result in data with JSON representation:
```

See [example calls](examples.md) for some more usage examples.

### Data Function API

When using the `ghz/runner` package programmatically, we can dynamically create data for each request using `WithBinaryDataFunc()` API:

```go
func dataFunc(mtd *desc.MethodDescriptor, cd *runner.CallData) []byte {
msg := &helloworld.HelloRequest{}
msg.Name = cd.WorkerID
binData, err := proto.Marshal(msg)
return binData
}

report, err := runner.Run(
"helloworld.Greeter.SayHello",
"0.0.0.0:50051",
runner.WithProtoFile("./testdata/greeter.proto", []string{}),
runner.WithInsecure(true),
runner.WithBinaryDataFunc(dataFunc),
)
```

0 comments on commit 0a1938f

Please sign in to comment.