Skip to content

Commit

Permalink
use prepared common template for calldata to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
bojand committed Mar 6, 2022
1 parent cef67ed commit 1f14f53
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions runner/calldata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/base64"
"encoding/json"
htmlTemplate "html/template"
"math/rand"
"strings"
"text/template"
Expand All @@ -21,6 +22,8 @@ const charset = "abcdefghijklmnopqrstuvwxyz" +
var seededRand *rand.Rand = rand.New(
rand.NewSource(time.Now().UnixNano()))

var sprigFuncMap htmlTemplate.FuncMap = sprig.FuncMap()

// CallData represents contextualized data available for templating
type CallData struct {
WorkerID string // unique worker ID
Expand All @@ -47,28 +50,26 @@ var tmplFuncMap = template.FuncMap{
"randomInt": randomInt,
}

var commonTemplate *template.Template = template.New("call_template_data").
Funcs(tmplFuncMap).
Funcs(template.FuncMap(sprigFuncMap))

// newCallData returns new CallData
func newCallData(
mtd *desc.MethodDescriptor,
funcs template.FuncMap,
workerID string, reqNum int64) *CallData {

fns := make(template.FuncMap, len(funcs)+2)
for k, v := range tmplFuncMap {
fns[k] = v
}

for k, v := range sprig.FuncMap() {
fns[k] = v
}
fns := make(template.FuncMap, len(funcs))

if len(funcs) > 0 {
for k, v := range funcs {
fns[k] = v
}
}

t := template.New("call_template_data").Funcs(fns)
t, _ := commonTemplate.Clone()
t.Funcs(fns)

now := time.Now()
newUUID, _ := uuid.NewRandom()
Expand Down

0 comments on commit 1f14f53

Please sign in to comment.