diff --git a/wasm/bench/substreams_wasi_go/main.go b/wasm/bench/substreams_wasi_go/main.go index a90910a37..8b9da1267 100644 --- a/wasm/bench/substreams_wasi_go/main.go +++ b/wasm/bench/substreams_wasi_go/main.go @@ -1,18 +1,16 @@ -//go:build wasip1 - package main import ( "encoding/hex" "encoding/json" "fmt" - "io" "log" "os" "strings" "time" "github.com/streamingfast/substreams/wasm/bench/substreams_wasi_go/pb" + "github.com/streamingfast/substreams/wasm/wasi/substream" ) func main() { @@ -20,7 +18,7 @@ func main() { log.Print("let's do it") log.Print("start: ", start) - input, err := readInput() + input, err := substream.ReadInput() if err != nil { panic(fmt.Errorf("reading input: %w", err)) } @@ -69,28 +67,6 @@ func main() { log.Print("total duration: ", time.Since(start)) } -func readAll(r io.Reader) ([]byte, error) { - b := make([]byte, 0, 1024*1024) - count := 0 - for { - count++ - n, err := r.Read(b[len(b):cap(b)]) - log.Print("Read count: ", count) - b = b[:len(b)+n] - if err != nil { - if err == io.EOF { - err = nil - } - return b, err - } - - if len(b) == cap(b) { - // Add more capacity (let append pick how much). - b = append(b, 0)[:len(b)] - } - } -} - type blockStat struct { TrxCount int TransferCount int @@ -141,21 +117,9 @@ func mapBlock(block *pb.Block) error { if err != nil { return fmt.Errorf("marshalling stats: %w", err) } - _, err = writeOutput(data) + _, err = substream.WriteOutput(data) if err != nil { return fmt.Errorf("writing output: %w", err) } return nil } - -func writeOutput(data []byte) (int, error) { - return os.Stdout.Write(data) -} - -func readInput() ([]byte, error) { - return readAll(os.Stdin) -} - -func readFile(path string) ([]byte, error) { - return os.ReadFile(path) -} diff --git a/wasm/bench/substreams_wasi_go/main.wasm b/wasm/bench/substreams_wasi_go/main.wasm index e6005e346..5bc20ff9e 100755 Binary files a/wasm/bench/substreams_wasi_go/main.wasm and b/wasm/bench/substreams_wasi_go/main.wasm differ diff --git a/wasm/wasi/substream/io.go b/wasm/wasi/substream/io.go new file mode 100644 index 000000000..068bbb3fb --- /dev/null +++ b/wasm/wasi/substream/io.go @@ -0,0 +1,41 @@ +package substream + +import ( + "io" + "log" + "os" +) + +func ReadAll(r io.Reader) ([]byte, error) { + b := make([]byte, 0, 1024*1024) + count := 0 + for { + count++ + n, err := r.Read(b[len(b):cap(b)]) + log.Print("Read count: ", count) + b = b[:len(b)+n] + if err != nil { + if err == io.EOF { + err = nil + } + return b, err + } + + if len(b) == cap(b) { + // Add more capacity (let append pick how much). + b = append(b, 0)[:len(b)] + } + } +} + +func WriteOutput(data []byte) (int, error) { + return os.Stdout.Write(data) +} + +func ReadInput() ([]byte, error) { + return ReadAll(os.Stdin) +} + +func ReadFile(path string) ([]byte, error) { + return os.ReadFile(path) +}