-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample_test.go
75 lines (67 loc) · 2.25 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package replay
import (
"bufio"
"fmt"
"log"
"strings"
"time"
)
// exampleData for the example source.
//
// Imagine you have an arbitrarily long stream in this form.
const exampleData = `2014-09-24T19:47:32-07:00 first
2014-09-24T19:47:42-07:00 second
2014-09-24T19:48:13-07:00 third`
// exampleEvent represents a single event extracted from that data.
type exampleEvent struct {
theTime time.Time
eventName string
}
// TS just returns the parsed time from the event.
func (t exampleEvent) TS() time.Time { return t.theTime }
// exampleSrc contains a bufio.Scanner with which it parses the stream.
type exampleSrc struct {
r *bufio.Scanner
}
// Next parses the next event out of the stream and emits it.
//
// This is a super simple example made for this arbitrary test, but
// it's basically how most of them look. You build a parser for
// whatever data you've got that contains timestamped events, and work
// with one at a time.
func (e exampleSrc) Next() Event {
if !e.r.Scan() {
return nil
}
parts := strings.Split(e.r.Text(), " ")
t, err := time.Parse(time.RFC3339, parts[0])
if err != nil {
log.Printf("Failed to parse %q: %v", parts[0], err)
return nil
}
return exampleEvent{t, parts[1]}
}
// Example is a complete example of processing data.
//
// A couple things to note about this:
// 1. It uses fake time (so it runs instantly).
// 2. As such, it introduces fake overhead.
// 3. It's running at 10x realtime, you could also run at 0.1x realtime, but
// it's harder to introduce noticable simulated overhead for the processing
// work.
func Example() {
src := exampleSrc{bufio.NewScanner(strings.NewReader(exampleData))}
r, tm := useFakeTime(New(10))
off := r.Run(src, FunctionAction(func(e Event) {
myEvent := e.(exampleEvent)
// Predictable overhead time
tm.sleep(time.Second * time.Duration(len(myEvent.eventName)))
fmt.Printf("Processing event named %q at %v\n", myEvent.eventName, myEvent.theTime)
}))
fmt.Printf("Took %v with final entry off by %v\n", tm.passed, off)
// Output:
// Processing event named "first" at 2014-09-24 19:47:32 -0700 PDT
// Processing event named "second" at 2014-09-24 19:47:42 -0700 PDT
// Processing event named "third" at 2014-09-24 19:48:13 -0700 PDT
// Took 16s with final entry off by -11.9s
}