/
cqrsprof.go
106 lines (84 loc) · 3.05 KB
/
cqrsprof.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright (c) 2015, Mark Bucciarelli <mkbucc@gmail.com>
Permission to use, copy, modify, and/or distribute this software
for any purpose with or without fee is hereby granted, provided
that the above copyright notice and this permission notice
appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR
CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
package main
import (
. "github.com/mbucc/cqrs"
"flag"
"fmt"
"log"
"math/rand"
"os"
"runtime/pprof"
)
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
var aggregates = flag.Int("a", 100, "Number of aggregate IDs")
var commands = flag.Int("e", 1000, "Number of commands to process")
var tmpdir = flag.String("d", "./tmp/", "Temporary directory to persist events.")
type ShoutCommand struct {
id AggregateID
Comment string
supportsRollback bool
}
func (c *ShoutCommand) ID() AggregateID { return c.id }
func (c *ShoutCommand) BeginTransaction() error { return nil }
func (c *ShoutCommand) Commit() error { return nil }
func (c *ShoutCommand) Rollback() error { return nil }
func (c *ShoutCommand) SupportsRollback() bool { return c.supportsRollback }
type HeardEvent struct {
BaseEvent
id AggregateID
Heard string
}
func (e *HeardEvent) ID() AggregateID { return e.id }
type NullAggregate struct {
id AggregateID
}
func (eh NullAggregate) Handle(c Command) (a []Event, err error) {
a = make([]Event, 1)
c1 := c.(*ShoutCommand)
a[0] = &HeardEvent{id: c1.ID(), Heard: c1.Comment}
return a, nil
}
func (eh NullAggregate) ID() AggregateID { return eh.id }
func (eh NullAggregate) New(id AggregateID) Aggregator { return &NullAggregate{id} }
func (eh NullAggregate) ApplyEvents([]Event) {}
type NullEventListener struct{}
func (h *NullEventListener) Apply(e Event) error { return nil }
func (h *NullEventListener) Reapply(e Event) error { return nil }
func main() {
var id AggregateID
flag.Parse()
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
// Use the same sequence of random numbers
// in case we need to reproduce something.
rand.Seed(42)
//store := NewSqliteEventStore(*tmpdir + "/cqrs.db")
store := &FileSystemEventStore{RootDir: *tmpdir}
RegisterEventListeners(new(HeardEvent), new(NullEventListener))
RegisterEventStore(store)
RegisterCommandAggregator(new(ShoutCommand), NullAggregate{})
for i := 0; i < *commands; i++ {
id = AggregateID(rand.Intn(*aggregates))
SendCommand(&ShoutCommand{id, fmt.Sprintf("hello from command #%d", i), false})
}
}