Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Commit

Permalink
Expose whether an outgoing frame was compressed or not
Browse files Browse the repository at this point in the history
Clients might want to track this info.
  • Loading branch information
martin-sucha committed Sep 22, 2023
1 parent 4fe1228 commit 169eaf1
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
1 change: 1 addition & 0 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -1254,6 +1254,7 @@ type ObservedStream struct {
FramePayloadUncompressedSize int
// FramePayloadCompressedSize is the compressed size of the frame payload (without frame header).
// This field is only available in StreamStarted.
// FramePayloadCompressedSize is zero if the frame was not compressed.
FramePayloadCompressedSize int
// QueryValuesSize is the total uncompressed size of query values in the frame (without other query options).
// For a batch, it is the sum for all queries in the batch.
Expand Down
6 changes: 3 additions & 3 deletions frame.go
Original file line number Diff line number Diff line change
Expand Up @@ -864,10 +864,10 @@ func (f *framer) finish() (outFrameInfo, error) {
}

f.buf = append(f.buf[:f.headSize], compressed...)

info.compressedSize = len(f.buf) - f.headSize
}
length := len(f.buf) - f.headSize
info.compressedSize = length
f.setLength(length)
f.setLength(len(f.buf) - f.headSize)

return info, nil
}
Expand Down
27 changes: 25 additions & 2 deletions frame_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ func TestFrameReadTooLong(t *testing.T) {
func TestOutFrameInfo(t *testing.T) {
tests := map[string]struct {
frame frameBuilder
compress bool
expectedInfo outFrameInfo
}{
"query": {
Expand All @@ -132,6 +133,7 @@ func TestOutFrameInfo(t *testing.T) {
},
customPayload: nil,
},
compress: true,
expectedInfo: outFrameInfo{
op: opQuery,
uncompressedSize: 81,
Expand All @@ -155,6 +157,7 @@ func TestOutFrameInfo(t *testing.T) {
},
customPayload: nil,
},
compress: true,
expectedInfo: outFrameInfo{
op: opExecute,
compressedSize: 50,
Expand Down Expand Up @@ -198,6 +201,7 @@ func TestOutFrameInfo(t *testing.T) {
defaultTimestampValue: 0,
customPayload: nil,
},
compress: true,
expectedInfo: outFrameInfo{
op: opBatch,
compressedSize: 96,
Expand All @@ -207,7 +211,8 @@ func TestOutFrameInfo(t *testing.T) {
},
},
"options": {
frame: &writeOptionsFrame{},
frame: &writeOptionsFrame{},
compress: true,
expectedInfo: outFrameInfo{
op: opOptions,
compressedSize: 0,
Expand All @@ -220,6 +225,7 @@ func TestOutFrameInfo(t *testing.T) {
frame: &writeRegisterFrame{
events: []string{"event1", "event2"},
},
compress: true,
expectedInfo: outFrameInfo{
op: opRegister,
compressedSize: 20,
Expand All @@ -228,10 +234,27 @@ func TestOutFrameInfo(t *testing.T) {
queryCount: 0,
},
},
"register uncompressed": {
frame: &writeRegisterFrame{
events: []string{"event1", "event2"},
},
compress: false,
expectedInfo: outFrameInfo{
op: opRegister,
compressedSize: 0,
uncompressedSize: 18,
queryValuesSize: 0,
queryCount: 0,
},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
fr := newFramer(SnappyCompressor{}, 4)
var compressor Compressor
if test.compress {
compressor = SnappyCompressor{}
}
fr := newFramer(compressor, 4)
ofi, err := test.frame.buildFrame(fr, 42)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit 169eaf1

Please sign in to comment.