Skip to content

Commit

Permalink
Merge branch 'feat/calculate-cid' of github.com:ucan-wg/go-ucan into …
Browse files Browse the repository at this point in the history
…feat/calculate-cid
  • Loading branch information
smoyer64 committed Sep 24, 2024
2 parents 5509cce + f4ad976 commit 6dd6f8a
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
4 changes: 1 addition & 3 deletions delegation/delegation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,7 @@ func TestConstructors(t *testing.T) {
})
}

func privKey(t *testing.T, privKeyCfg string) crypto.PrivKey {
t.Helper()

func privKey(t require.TestingT, privKeyCfg string) crypto.PrivKey {
privKeyMar, err := crypto.ConfigDecodeKey(privKeyCfg)
require.NoError(t, err)

Expand Down
89 changes: 88 additions & 1 deletion delegation/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestSchemaRoundTrip(t *testing.T) {
// format: dagJson --> PayloadModel --> dagCbor --> PayloadModel --> dagJson
// function: DecodeDagJson() Seal() Unseal() EncodeDagJson()

p1, err := delegation.FromDagJson([]byte(delegationJson))
p1, err := delegation.FromDagJson(delegationJson)
require.NoError(t, err)

cborBytes, id, err := p1.Seal(privKey)
Expand Down Expand Up @@ -88,3 +88,90 @@ func BenchmarkSchemaLoad(b *testing.B) {
_, _ = ipld.LoadSchemaBytes(schemaBytes)
}
}

func BenchmarkRoundTrip(b *testing.B) {
delegationJson := golden.Get(b, "new.dagjson")
privKey := privKey(b, issuerPrivKeyCfg)

b.Run("via buffers", func(b *testing.B) {
p1, _ := delegation.FromDagJson(delegationJson)
cborBytes, _, _ := p1.Seal(privKey)
p2, _ := delegation.Unseal(cborBytes)

b.ResetTimer()

b.Run("FromDagJson", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = delegation.FromDagJson(delegationJson)
}
})

b.Run("Seal", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _, _ = p1.Seal(privKey)
}
})

b.Run("Unseal", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = delegation.Unseal(cborBytes)
}
})

b.Run("ToDagJson", func(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, _ = p2.ToDagJson(privKey)
}
})
})

b.Run("via streaming", func(b *testing.B) {
p1, _ := delegation.FromDagJsonReader(bytes.NewReader(delegationJson))
cborBuf := &bytes.Buffer{}
_, _ = p1.SealWriter(cborBuf, privKey)
cborBytes := cborBuf.Bytes()
p2, _ := delegation.UnsealReader(bytes.NewReader(cborBytes))

b.ResetTimer()

b.Run("FromDagJsonReader", func(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(delegationJson)
for i := 0; i < b.N; i++ {
_, _ = reader.Seek(0, 0)
_, _ = delegation.FromDagJsonReader(reader)
}
})

b.Run("SealWriter", func(b *testing.B) {
b.ReportAllocs()
buf := &bytes.Buffer{}
for i := 0; i < b.N; i++ {
buf.Reset()
_, _ = p1.SealWriter(buf, privKey)
}
})

b.Run("UnsealReader", func(b *testing.B) {
b.ReportAllocs()
reader := bytes.NewReader(cborBytes)
for i := 0; i < b.N; i++ {
_, _ = reader.Seek(0, 0)
_, _ = delegation.UnsealReader(reader)
}
})

b.Run("ToDagJsonReader", func(b *testing.B) {
b.ReportAllocs()
buf := &bytes.Buffer{}
for i := 0; i < b.N; i++ {
buf.Reset()
_ = p2.ToDagJsonWriter(buf, privKey)
}
})
})
}

0 comments on commit 6dd6f8a

Please sign in to comment.