Skip to content

Commit

Permalink
inc-storm benchmark (sinc), timer-inc bench (tinc)
Browse files Browse the repository at this point in the history
  • Loading branch information
gritzko committed Apr 13, 2024
1 parent 7db328e commit 4d1f5e5
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 9 deletions.
12 changes: 9 additions & 3 deletions chotki.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type Chotki struct {

outlock sync.Mutex
lock sync.Mutex
idlock sync.Mutex

opts Options

Expand Down Expand Up @@ -384,13 +383,16 @@ func (cho *Chotki) Snapshot() pebble.Reader {
}

func (cho *Chotki) Close() error {
cho.lock.Lock()
if cho.db == nil {
cho.lock.Unlock()
return ErrClosed
}
_ = cho.db.Close()
cho.db = nil
// todo
cho.last = rdx.ID0
cho.lock.Unlock()
return nil
}

Expand All @@ -403,15 +405,19 @@ func Join(records ...[]byte) (ret []byte) {

// Here new packets are timestamped and queued for save
func (cho *Chotki) CommitPacket(lit byte, ref rdx.ID, body toyqueue.Records) (id rdx.ID, err error) {
cho.idlock.Lock()
cho.lock.Lock()
if cho.db == nil {
cho.lock.Unlock()
return rdx.BadId, ErrClosed
}
id = (cho.last & ^rdx.OffMask) + rdx.ProInc
i := toytlv.Record('I', id.ZipBytes())
r := toytlv.Record('R', ref.ZipBytes())
packet := toytlv.Record(lit, i, r, Join(body...))
recs := toyqueue.Records{packet}
err = cho.Drain(recs)
cho.Broadcast(recs, "")
cho.idlock.Unlock()
cho.lock.Unlock()
return
}

Expand Down
21 changes: 21 additions & 0 deletions objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,10 @@ func FieldOffset(fields []string, name string) rdx.ID {
}

func (cho *Chotki) ObjectFieldTLV(fid rdx.ID) (rdt byte, tlv []byte, err error) {
db := cho.db
if db == nil {
return 0, nil, ErrClosed
}
it := cho.db.NewIter(&pebble.IterOptions{})
key := OKey(fid, 0)
if !it.SeekGE(key) {
Expand Down Expand Up @@ -316,3 +320,20 @@ func (cho *Chotki) SetFieldTLV(fid rdx.ID, tlve []byte) (id rdx.ID, err error) {
f := toytlv.Record('F', rdx.ZipUint64(uint64(fid.Off())))
return cho.CommitPacket('E', oid, toyqueue.Records{f, tlve})
}

var ErrWrongFieldType = errors.New("wrong field type")

func (cho *Chotki) IncNField(fid rdx.ID) (id rdx.ID, err error) {
rdt, tlv, err := cho.ObjectFieldTLV(fid)
if err != nil || rdt != rdx.Natural {
return rdx.BadId, ErrWrongFieldType
}
src := cho.Source()
mine := rdx.Nmine(tlv, src)
tlvs := toyqueue.Records{
toytlv.Record('F', rdx.ZipUint64(fid.Off())),
toytlv.Record(rdx.Natural, toytlv.Record(rdx.Term, rdx.ZipUint64Pair(mine+1, src))),
}
id, err = cho.CommitPacket('E', fid.ZeroOff(), tlvs)
return
}
4 changes: 2 additions & 2 deletions rdx/NZ.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Nparse(txt string) (tlv []byte) {
return Ntlv(u)
}

// convert native golang value into a TLV form
// convert a native golang value into TLV
func Ntlv(u uint64) (tlv []byte) {
return toytlv.Record(Term, ZipUint64Pair(u, 0))
}
Expand Down Expand Up @@ -166,7 +166,7 @@ func Zparse(txt string) (tlv []byte) {
return Ztlv(i)
}

// convert native golang value into a TLV form
// convert a native golang value into TLV
func Ztlv(i int64) (tlv []byte) {
return toytlv.Record('I',
toytlv.TinyRecord('T', ZipIntUint64Pair(0, 0)),
Expand Down
116 changes: 114 additions & 2 deletions repl/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"github.com/drpcorg/chotki/rdx"
"github.com/learn-decentralized-systems/toyqueue"
"github.com/learn-decentralized-systems/toytlv"
"os"
"time"
)

var HelpCreate = errors.New("create zone/1 {Name:\"Name\",Description:\"long text\"}")
Expand Down Expand Up @@ -353,7 +355,7 @@ func (repl *REPL) CommandPing(arg *rdx.RDX) (id rdx.ID, err error) {
return
}

var HelpPinc = errors.New("pinc b0b-12-2 // N field id")
var HelpPinc = errors.New("pinc b0b-12-2")
var ErrBadField = errors.New("bad field")

func KeepOddEven(oddeven uint64, cho *chotki.Chotki, fid rdx.ID) error {
Expand Down Expand Up @@ -435,7 +437,117 @@ func (repl *REPL) CommandMute(arg *rdx.RDX) (id rdx.ID, err error) {
return
}

func (repl *REPL) CommandTic(arg *rdx.RDX) (id rdx.ID, err error) {
var HelpTinc = errors.New("tinc b0b-12-2, tinc {fid:b0b-12-2,ms:1000,count:100}")

func (repl *REPL) doTinc(fid rdx.ID, delay time.Duration, count int64) {
var err error
for ; count > 0 && err == nil; count-- {
_, err = repl.Host.IncNField(fid)
if delay > time.Duration(0) {
time.Sleep(delay)
}
}
}

// testing: read-inc loop
func (repl *REPL) CommandTinc(arg *rdx.RDX) (id rdx.ID, err error) {
id, err = rdx.BadId, HelpTinc
count := int64(1)
delay := time.Second
if arg == nil {
return
} else if arg.RdxType == rdx.Reference {
id = rdx.IDFromText(arg.Text)
} else if arg.RdxType == rdx.Map {
for i := 0; i+1 < len(arg.Nested); i += 2 {
key := &arg.Nested[i]
val := &arg.Nested[i+1]
switch key.String() {
case "fid":
id = rdx.IDFromText(val.Text)
case "ms":
ms := rdx.Inative(rdx.Iparse(val.String()))
delay = time.Millisecond * time.Duration(ms)
case "count":
count = rdx.Inative(rdx.Iparse(val.String()))
default:
return
}
}
} else {
return
}
err = nil
go repl.doTinc(id, delay, count)
return
}

var HelpSinc = errors.New("sinc b0b-12-2, tinc {fid:b0b-12-2,ms:1000,count:100}")

func (repl *REPL) doSinc(fid rdx.ID, delay time.Duration, count int64, mine uint64) {
var err error
start := time.Now()
fro := repl.Host.Last()
src := fro.Src()
til := rdx.ID0
for c := count; c > 0 && err == nil; c-- {
mine++
tlvs := toyqueue.Records{
toytlv.Record('F', rdx.ZipUint64(fid.Off())),
toytlv.Record(rdx.Natural, toytlv.Record(rdx.Term, rdx.ZipUint64Pair(mine, src))),
}
til, err = repl.Host.CommitPacket('E', fid.ZeroOff(), tlvs)
if delay > time.Duration(0) {
time.Sleep(delay)
}
}
if err != nil {
fmt.Println(err.Error())
}
timer := time.Since(start)
_, _ = fmt.Fprintf(os.Stdout, "inc storm: %d incs complete for %s, elapsed %s, %s..%s\n",
count, fid.String(), timer.String(), fro.String(), til.String())
}

func (repl *REPL) CommandSinc(arg *rdx.RDX) (id rdx.ID, err error) {
id, err = rdx.BadId, HelpSinc
count := int64(1)
delay := time.Second
if arg == nil {
return
} else if arg.RdxType == rdx.Reference {
id = rdx.IDFromText(arg.Text)
} else if arg.RdxType == rdx.Map {
for i := 0; i+1 < len(arg.Nested); i += 2 {
key := &arg.Nested[i]
val := &arg.Nested[i+1]
switch key.String() {
case "fid":
id = rdx.IDFromText(val.Text)
if id.Off() == 0 {
return rdx.BadId, chotki.ErrWrongFieldType
}
case "ms":
ms := rdx.Inative(rdx.Iparse(val.String()))
delay = time.Millisecond * time.Duration(ms)
case "count":
count = rdx.Inative(rdx.Iparse(val.String()))
default:
return
}
}
} else {
return
}

rdt, tlv, err := repl.Host.ObjectFieldTLV(id)
if err != nil || rdt != rdx.Natural {
return rdx.BadId, chotki.ErrWrongFieldType
}
src := repl.Host.Source()
mine := rdx.Nmine(tlv, src)

go repl.doSinc(id, delay, count, mine)
return
}

Expand Down
6 changes: 4 additions & 2 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,10 @@ func (repl *REPL) REPL() (id rdx.ID, err error) {
id, err = repl.CommandPonc(arg)
case "mute":
id, err = repl.CommandMute(arg)
case "tic":
id, err = repl.CommandTic(arg)
case "tinc":
id, err = repl.CommandTinc(arg)
case "sinc":
id, err = repl.CommandSinc(arg)
default:
_, _ = fmt.Fprintf(os.Stderr, "command unknown: %s\n", cmd)
}
Expand Down

0 comments on commit 4d1f5e5

Please sign in to comment.