Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Benchmark CBOR encoding/decoding using mainnet data #783

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 166 additions & 0 deletions runtime/interpreter/encoding_benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Cadence - The resource-oriented smart contract programming language
*
* Copyright 2021 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package interpreter

import (
"bytes"
"compress/gzip"
"fmt"
"io"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"testing"

"github.com/onflow/cadence/runtime/common"
"github.com/stretchr/testify/require"
)

type benchmark struct {
name string
value Value
}

func BenchmarkEncodeCBOR(b *testing.B) {
benchmarks := prepareCBORTestData()

for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, _, err := EncodeValue(bm.value, nil, true, nil)
require.NoError(b, err)
}
})
}
}

func BenchmarkDecodeCBOR(b *testing.B) {
benchmarks := prepareCBORTestData()

for _, bm := range benchmarks {
b.Run(bm.name, func(b *testing.B) {

encoded, _, err := EncodeValue(bm.value, nil, true, nil)
require.NoError(b, err)

b.ReportAllocs()
b.ResetTimer()

for i := 0; i < b.N; i++ {
_, err = DecodeValue(encoded, nil, nil, CurrentEncodingVersion, nil)
require.NoError(b, err)
}
})
}
}

// prepareCBORTestData processes testdata/*.cbor.gz files and
// returns a []benchmark including each benchmark name and Value.
// For safety, the number of *.cbor.gz files processed is limited and
// io.LimitReader is used to limit uncompressed CBOR data size.
func prepareCBORTestData() []benchmark {
// maxNumFiles limits the number of files processed in testdata folder.
const maxNumFiles = 100

// maxCBORSize limits max size of each uncompressed CBOR data to 16 MB.
const maxCBORSize = 16 * 1024 * 1024

// Compile regular expression to match version number in file name
versionRegExp := regexp.MustCompile(`_v([\d]+)_`)

fileNames, err := filepath.Glob("testdata/*.cbor.gz")
if err != nil {
panic(err)
}

numFiles := len(fileNames)
if numFiles > maxNumFiles {
numFiles = maxNumFiles

fmt.Printf("Found %d *.cbor.gz files in testdata. Processing only the first %d files.\n",
len(fileNames),
maxNumFiles)
}

var benchmarks []benchmark
for _, fileName := range fileNames[:numFiles] {

// Read cbor.gz file
f, err := os.Open(fileName)
if err != nil {
panic(err)
}
defer f.Close()

zr, err := gzip.NewReader(f)
if err != nil {
panic(err)
}
defer zr.Close()

lzr := io.LimitReader(zr, maxCBORSize)

var buf bytes.Buffer
fileSize, err := io.Copy(&buf, lzr)
if err != nil {
panic(err)
}

// File name without path nor extension
name := strings.TrimSuffix(filepath.Base(fileName), ".cbor.gz")

// Get version number from file name
// If file name doesn't match "_v([\d+])_", version number is default to current version.
version := CurrentEncodingVersion
m := versionRegExp.FindStringSubmatch(name)
if len(m) > 1 {
v, err := strconv.ParseUint(m[1], 10, 16)
if err == nil {
version = uint16(v)
}
}

// Construct benchmark name
name = name + fmt.Sprintf("_%dbytes", fileSize)

// Decode test data to value
owner := common.BytesToAddress([]byte{})

var value Value
if version <= 3 {
value, err = DecodeValueV3(buf.Bytes(), &owner, nil, version, nil)
} else {
value, err = DecodeValue(buf.Bytes(), &owner, nil, version, nil)
}
if err != nil {
panic(fmt.Sprintf("failed to decode value in file %s: %s\n", fileName, err))
}

// Add to benchmarks
benchmarks = append(benchmarks, benchmark{name: name, value: value})
}

return benchmarks
}
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.