Skip to content

Commit

Permalink
Add tests and benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
i5heu committed May 18, 2024
1 parent 41f0a41 commit 602debf
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 8 deletions.
149 changes: 149 additions & 0 deletions ouroboros_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,53 @@ func Test_Index_RebuildIndex(t *testing.T) {
}
}

func Test_Index_GetDirectParentOfEvent(t *testing.T) {
ou, evs := setupDBWithData(t, setupDBConfig{
totalEvents: 1000,
returnAllEvents: true,
eventLevels: 5,
})

var parentCount int

for _, evHash := range evs {
parent, err := ou.Index.GetDirectParentOfEvent(evHash)
if err != nil {
t.Errorf("GetDirectParentOfEvent failed with error: %v", err)
}
if parent == nil {
continue
}
parentCount++
}

if parentCount == 0 {
t.Errorf("GetDirectParentOfEvent failed, expected non-zero count, got %d", parentCount)
}
}

func Test_Index_GetParentHashOfEvent(t *testing.T) {
ou, evs := setupDBWithData(t, setupDBConfig{
totalEvents: 1000,
returnAllEvents: true,
eventLevels: 5,
})

var childrenCount int

for _, evHash := range evs {
parent := ou.Index.GetParentHashOfEvent(evHash)
if parent == [64]byte{} {
continue
}
childrenCount++
}

if childrenCount == 0 {
t.Errorf("GetChildrenHashesOfEvent failed, expected non-zero count, got %d", childrenCount)
}
}

func Test_Index_GetDirectChildrenOfEvent(t *testing.T) {
ou, evs := setupDBWithData(t, setupDBConfig{
totalEvents: 100,
Expand Down Expand Up @@ -429,6 +476,42 @@ func Test_DB_CreateNewEvent(t *testing.T) {
}
}

func Test_DB_fastMeta(t *testing.T) {
ou, evs := setupDBWithData(t, setupDBConfig{
totalEvents: 1,
})

rootEv := evs[0]

fm := types.FastMeta{
types.FastMetaParameter([]byte("test1")),
types.FastMetaParameter([]byte("test2")),
types.FastMetaParameter([]byte("test3")),
types.FastMetaParameter([]byte("test4")),
types.FastMetaParameter([]byte("test5")),
types.FastMetaParameter([]byte("test6")),
}

ev, err := ou.DB.CreateNewEvent(storage.EventOptions{
ParentEvent: rootEv,
FastMeta: fm,
})
if err != nil {
t.Errorf("CreateNewEvent failed with error: %v", err)
}

evGet, err := ou.DB.GetEvent(ev.EventIdentifier.EventHash)
if err != nil {
t.Errorf("GetEvent failed with error: %v", err)
}

for i, param := range evGet.FastMeta {
if string(param) != string(fm[i]) {
t.Errorf("FastMeta failed, expected %s, got %s", fm[i], param)
}
}
}

func Example() {
// Initialize OuroborosDB with basic configuration
ou, err := ouroboros.NewOuroborosDB(ouroboros.Config{
Expand Down Expand Up @@ -731,3 +814,69 @@ func Benchmark_DB_CreateNewEvent(b *testing.B) {
}
})
}

func Benchmark_DB_fastMeta(b *testing.B) {
ou, evs := setupDBWithData(b, setupDBConfig{
totalEvents: 1,
})

rootEv := evs[0]

fm := types.FastMeta{
types.FastMetaParameter([]byte("test1")),
types.FastMetaParameter([]byte("test2")),
types.FastMetaParameter([]byte("test3")),
types.FastMetaParameter([]byte("test4")),
types.FastMetaParameter([]byte("test5")),
types.FastMetaParameter([]byte("test6")),
}

b.Run("CreateNewEvent with FastMeta", func(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := ou.DB.CreateNewEvent(storage.EventOptions{
ParentEvent: rootEv,
FastMeta: fm,
})
if err != nil {
b.Errorf("CreateNewEvent failed with error: %v", err)
}
}
})

ev, err := ou.DB.CreateNewEvent(storage.EventOptions{
ParentEvent: rootEv,
FastMeta: fm,
})
if err != nil {
b.Errorf("CreateNewEvent failed with error: %v", err)
}

b.Run("GetEvent with FastMeta", func(b *testing.B) {
for i := 0; i < b.N; i++ {
evGet, err := ou.DB.GetEvent(ev.EventIdentifier.EventHash)
if err != nil {
b.Errorf("GetEvent failed with error: %v", err)
}
for i, param := range evGet.FastMeta {
if string(param) != string(fm[i]) {
b.Errorf("FastMeta failed, expected %s, got %s", fm[i], param)
}
}
}
})
}

func Benchmark_Index_GetParentHashOfEvent(b *testing.B) {
ou, evs := setupDBWithData(b, setupDBConfig{
totalEvents: 1000,
returnAllEvents: true,
eventLevels: 5,
})

b.Run("GetParentHashOfEvent", func(b *testing.B) {
for i := 0; i < b.N; i++ {
evHash := evs[rand.Intn(len(evs))]
_ = ou.Index.GetParentHashOfEvent(evHash)
}
})
}
11 changes: 5 additions & 6 deletions pkg/index/ChildrenToParents.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,16 @@ func (i *Index) RebuildChildrenToParents(allEvents []types.Event) error {
return nil
}

func (i *Index) GetParentHashOfEvent(eventHash types.Hash) (types.Hash, bool) {
func (i *Index) GetParentHashOfEvent(eventHash types.Hash) types.Hash {
i.evChildToParentLock.RLock()
defer i.evChildToParentLock.RUnlock()
parentHash, exists := i.evChildToParent[eventHash]
return parentHash, exists
return i.evChildToParent[eventHash]
}

func (i *Index) GetDirectParentOfEvent(eventHash types.Hash) (*types.Event, error) {
parentHash, exists := i.GetParentHashOfEvent(eventHash)
if !exists {
return nil, nil // No parent found
parentHash := i.GetParentHashOfEvent(eventHash)
if parentHash == (types.Hash{}) {
return nil, nil
}

parentEvent, err := i.s.GetEvent(parentHash)
Expand Down
1 change: 1 addition & 0 deletions pkg/index/FastMeta.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package index
3 changes: 1 addition & 2 deletions pkg/index/index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ func TestIndex_GetParentHashOfEvent(t *testing.T) {
childHash := types.Hash{2}
index.evChildToParent[childHash] = parentHash

retrievedParentHash, exists := index.GetParentHashOfEvent(childHash)
assert.True(t, exists)
retrievedParentHash := index.GetParentHashOfEvent(childHash)
assert.Equal(t, parentHash, retrievedParentHash)
}

Expand Down

0 comments on commit 602debf

Please sign in to comment.