Skip to content

Commit

Permalink
Save request timestamp
Browse files Browse the repository at this point in the history
WIP
  • Loading branch information
slawosz committed Oct 3, 2013
1 parent 30547f2 commit 8b66a99
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
2 changes: 2 additions & 0 deletions listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ func Run() {
messageWriter := bufio.NewWriter(messageBuffer)

// TODO: add timestamp to message
fmt.Fprintf(messageWriter, "%v\n", time.Now().UnixNano())
fmt.Printf("timestamp: %v", time.Now().UnixNano())
fmt.Fprintf(messageWriter, "%s", string(m.Bytes()))

messageWriter.Flush()
Expand Down
14 changes: 13 additions & 1 deletion replay/replay.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ import (
"log"
"net"
"net/http"
"time"
)


const bufSize = 4096

type ReplayManager struct {
Expand Down Expand Up @@ -88,17 +90,27 @@ func (self *ReplayManager) RunReplayFromFile() {
log.Fatal("Can't parse request: ", err)
}

var lastTimestamp int64

if len(requests) > 0 {
lastTimestamp = requests[0].Timestamp
}
for _, request := range requests {
parsedReq, err := ParseRequest(request)

parsedReq, err := ParseRequest(request.Request)

if err != nil {
log.Fatal("Can't parse request...:", err)
}

time.Sleep(time.Duration(request.Timestamp - lastTimestamp))

self.sendRequestToReplay(parsedReq)
lastTimestamp = request.Timestamp
}

// wait forever
// TODO: quit when all request finishes
select {}
}

Expand Down
24 changes: 21 additions & 3 deletions replay/replay_file_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ import (
"log"
"os"
"bytes"
"strconv"

"fmt"
)

func parseReplyFile() (requests [][]byte, err error) {
type ParsedRequest struct {
Request []byte
Timestamp int64
}

func (self ParsedRequest) String() string {
return fmt.Sprintf("Request: %v, timestamp: %v", string(self.Request), self.Timestamp)
}

func parseReplyFile() (requests []ParsedRequest, err error) {
requests, err = readLines(Settings.FileToReplyPath)

if err != nil {
Expand All @@ -19,7 +31,7 @@ func parseReplyFile() (requests [][]byte, err error) {

// readLines reads a whole file into memory
// and returns a slice of its lines.
func readLines(path string) (requests [][]byte, err error) {
func readLines(path string) (requests []ParsedRequest, err error) {
file, err := os.Open(path)

if err != nil {
Expand All @@ -32,7 +44,13 @@ func readLines(path string) (requests [][]byte, err error) {

for scanner.Scan() {
if len(scanner.Text()) > 5 {
requests = append(requests, scanner.Bytes())
Debug(scanner.Text())
i := bytes.IndexByte(scanner.Bytes(), '\n')
timestamp, _ := strconv.Atoi(string(scanner.Bytes()[i + 1:]))
pr := ParsedRequest{scanner.Bytes()[:i], int64(timestamp)}

Debug(pr)
requests = append(requests, pr)
}
}

Expand Down

0 comments on commit 8b66a99

Please sign in to comment.