Skip to content

Commit

Permalink
bindings/go: Add memory usage test for streaming parser (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
varungandhi-src authored Jul 3, 2023
1 parent 335e0fa commit ca0c0bc
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bindings/go/scip/memtest/DO_NOT_ADD_NEW_TEST_FILES_HERE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This package should only have a single test
so that we can use SetMemoryLimit without
affecting other tests running in different
goroutines in the same process.
63 changes: 63 additions & 0 deletions bindings/go/scip/memtest/low_mem_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package memtest

import (
"fmt"
"io"
"os"
"runtime"
"runtime/debug"
"strings"
"testing"

"github.com/stretchr/testify/require"
"google.golang.org/protobuf/proto"

"github.com/sourcegraph/scip/bindings/go/scip"
)

// Do not add any other tests in this sub-package, so that the
// SetMemoryLimit call doesn't interfere with other running tests

func TestLowMemoryParsing(t *testing.T) {
tmpFile, err := os.CreateTemp(t.TempDir(), "very-large-index.scip")
require.NoError(t, err)
defer os.RemoveAll(tmpFile.Name())

// Total index size will be about (textSize + ε) * docCount ~= 128 MB
const textSize = 128 * 1024
const docCount = 1000
{
largeIndex := scip.Index{}
for i := 0; i < docCount; i++ {
doc := scip.Document{}
doc.Text = strings.Repeat(fmt.Sprintf("%d", i), textSize)
largeIndex.Documents = append(largeIndex.Documents, &doc)
}
indexBytes, err := proto.Marshal(&largeIndex)
require.NoError(t, err)
_, err = tmpFile.Write(indexBytes)
require.NoError(t, err)

_, err = tmpFile.Seek(0, io.SeekStart)
require.NoError(t, err)

runtime.GC()
}

require.Greater(t, docCount, 100)
const maxDocsInMemory = docCount / 100
debug.SetMemoryLimit(textSize * maxDocsInMemory)

curDoc := &scip.Document{}
indexVisitor := scip.IndexVisitor{VisitDocument: func(d *scip.Document) {
curDoc = d
}}

// No OOM
err = indexVisitor.ParseStreaming(tmpFile)
_ = curDoc
require.NoError(t, err)
}

// Do not add any other tests in this sub-package, so that the
// SetMemoryLimit call doesn't interfere with other running tests

0 comments on commit ca0c0bc

Please sign in to comment.