Skip to content

Commit

Permalink
fix(inputs/modbus) influxdata#11105 no empty grouped requests
Browse files Browse the repository at this point in the history
Simple modification to
* allow starting the field list with an omitted field (useful if many requests needed)
* avoids requests where all fields are omitted

As a result, the total number of request is generally lower, also improving performance
  • Loading branch information
Timur committed May 17, 2022
1 parent 6dc2b99 commit 35717eb
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 4 deletions.
85 changes: 85 additions & 0 deletions plugins/inputs/modbus/modbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1857,3 +1857,88 @@ func TestConfigurationPerRequestFail(t *testing.T) {
})
}
}

func TestRequestGroupingStartingWithOmit(t *testing.T) {
modbus := Modbus{
Name: "Test",
Controller: "tcp://localhost:1502",
ConfigurationType: "request",
Log: testutil.Logger{},
}
modbus.Requests = []requestDefinition{
{SlaveID: 1,
ByteOrder: "ABCD",
RegisterType: "holding",
Fields: []requestFieldDefinition{
{
Name: "holding-0",
Address: uint16(0),
InputType: "INT16",
Omit: true,
},
{
Name: "holding-1",
Address: uint16(1),
InputType: "UINT16",
Omit: true,
},
{
Name: "holding-2",
Address: uint16(2),
InputType: "INT64",
Scale: 1.2,
OutputType: "FLOAT64",
Measurement: "modbus",
},
},
},
}
require.NoError(t, modbus.Init())
require.NotEmpty(t, modbus.requests)
require.NotNil(t, modbus.requests[1])
require.Len(t, modbus.requests[1].holding, 1)
require.Equal(t, uint16(2), modbus.requests[1].holding[0].address)
require.Equal(t, uint16(4), modbus.requests[1].holding[0].length)
}

func TestRequestGroupingAllOmit(t *testing.T) {
modbus := Modbus{
Name: "Test",
Controller: "tcp://localhost:1502",
ConfigurationType: "request",
Log: testutil.Logger{},
}
modbus.Requests = []requestDefinition{
{SlaveID: 1,
ByteOrder: "ABCD",
RegisterType: "holding",
Fields: []requestFieldDefinition{
{
Name: "holding-0",
Address: uint16(0),
InputType: "INT16",
Omit: true,
},
{
Name: "holding-1",
Address: uint16(1),
InputType: "UINT16",
Omit: true,
},
{
Name: "holding-2",
Address: uint16(2),
InputType: "INT64",
Scale: 1.2,
OutputType: "FLOAT64",
Measurement: "modbus",
Omit: true,
},
},
},
}
require.NoError(t, modbus.Init())
require.NotEmpty(t, modbus.requests)
require.NotNil(t, modbus.requests[1])
require.Len(t, modbus.requests[1].holding, 0)
}
19 changes: 15 additions & 4 deletions plugins/inputs/modbus/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ func newRequest(f field, tags map[string]string) request {
return r
}

func isNonEmptyRequest(c request) bool {
return !((len(c.fields) == 0) || ((len(c.fields) == 1) && c.fields[0].omit))
}

func groupFieldsToRequests(fields []field, tags map[string]string, maxBatchSize uint16) []request {
if len(fields) == 0 {
return nil
Expand Down Expand Up @@ -53,16 +57,23 @@ func groupFieldsToRequests(fields []field, tags map[string]string, maxBatchSize
current.length += f.length
if !f.omit {
// Omit adding the field but use it for constructing the request.
current.fields = append(current.fields, f)
if isNonEmptyRequest(current) {
current.fields = append(current.fields, f)
} else {
current = newRequest(f, tags)
}
}
continue
}

// Finish the current request, add it to the list and construct a new one
requests = append(requests, current)
if isNonEmptyRequest(current) {
requests = append(requests, current)
}
current = newRequest(f, tags)
}
requests = append(requests, current)

if isNonEmptyRequest(current) {
requests = append(requests, current)
}
return requests
}

0 comments on commit 35717eb

Please sign in to comment.