go-watson-sdk icon indicating copy to clipboard operation
go-watson-sdk copied to clipboard

watson/speech_to_text: very slow responses

Open odeke-em opened this issue 8 years ago • 0 comments

Hello there @liviosoares, thank you for the great library and for the hardwork!

We are interested in using your packages. However, we aren't able to use it to stream to Watson wav files past the first chunk of data. This is irrespective of if we use streamResults

map[string]interface{}{
     "continuous": true,
     "interim_results": false,
}

"continuous" -> true/false, "interim_results" -> true/false using the audio in this media https://j.gifs.com/v24y80.mp4, it takes about 1m 40seconds to get back a single response from the websocket for a single chunk of 32768 bytes, and the program stalls for all that time too waiting to even run readReplies. We never get past the first chunk

writing chunk of size: 32768
chunkCount: [c:0] sendTime: 1477351568
time to receive chunk: [c:0] 1m40.098601911s

To rule out problems, it takes about 1 second to make the .wav audio file and here is a sample setup of what am doing:

eventsChan, wc, err := client.NewStream("start", "audio/wav", streamConfig)
...

f, err := os.Open(theWAVPath)
...
go func() {
    defer wc.Close()
    defer f.Close()
     _, _ = io.Copy(wc, f)
}()

combined with the diff below

diff --git a/watson/speech_to_text/speech_to_text.go b/watson/speech_to_text/speech_to_text.go
index 55847b7..a801ba3 100644
--- a/watson/speech_to_text/speech_to_text.go
+++ b/watson/speech_to_text/speech_to_text.go
@@ -19,8 +19,11 @@ package speech_to_text
 import (
    "encoding/json"
    "errors"
+   "fmt"
    "io"
    "net/url"
+   "sync"
+   "time"

    "github.com/liviosoares/go-watson-sdk/watson"
    "github.com/liviosoares/go-watson-sdk/watson/authorization"
@@ -224,10 +227,38 @@ type stream struct {
    stopped        bool
 }

+type counter struct {
+   sync.RWMutex
+   count uint64
+}
+
+func (c *counter) increment() {
+   c.Lock()
+   c.count += 1
+   c.Unlock()
+}
+
+func (c *counter) value() uint64 {
+   c.RLock()
+   defer c.RUnlock()
+
+   return c.count
+}
+
+func (c *counter) String() string {
+   c.RLock()
+   defer c.RUnlock()
+
+   return fmt.Sprintf("[c:%d]", c.count)
+}
+
+var chunkCounter = new(counter)
+
 func (s *stream) Write(p []byte) (n int, err error) {
    if s.stopped {
        return 0, errors.New("cannot write to stopped stream")
    }
+   fmt.Printf("writing chunk of size: %d\n", len(p))
    if !s.started {
        m := make(map[string]interface{})
        for k, v := range s.options {
@@ -242,6 +273,8 @@ func (s *stream) Write(p []byte) (n int, err error) {
            }
        }
        m["interim_results"] = true
+       sendTime := time.Now()
+       fmt.Printf("chunkCount: %s sendTime: %d\n", chunkCounter, sendTime.Unix())
        err := websocket.JSON.Send(s.ws, m)
        if err != nil {
            return 0, err
@@ -249,6 +282,8 @@ func (s *stream) Write(p []byte) (n int, err error) {
        s.started = true
        var state StateReply
        err = websocket.JSON.Receive(s.ws, &state)
+       fmt.Printf("time to receive chunk: %s %s\n", chunkCounter, time.Since(sendTime))
+       chunkCounter.increment()
        if err != nil {
            return 0, err
        }

I tested the code without the counter code before and it was the same thing, stalled response for which it never gets to read the second chunk.

Thank you!

odeke-em avatar Oct 24 '16 23:10 odeke-em