Skip to content

Instantly share code, notes, and snippets.

@gloriousCode
Created March 17, 2024 23:20
Show Gist options
  • Save gloriousCode/9d6bda979cfdd62292b144c8a353c32c to your computer and use it in GitHub Desktop.
Save gloriousCode/9d6bda979cfdd62292b144c8a353c32c to your computer and use it in GitHub Desktop.
master benches
func BenchmarkUpdateByID(b *testing.B) {
b.ReportAllocs()
s := newStack()
asks := linkedList{}
asksSnapshot := Items{
{Price: 1, Amount: 1, ID: 1},
{Price: 3, Amount: 1, ID: 3},
{Price: 5, Amount: 1, ID: 5},
{Price: 7, Amount: 1, ID: 7},
{Price: 9, Amount: 1, ID: 9},
{Price: 11, Amount: 1, ID: 11},
}
asks.load(asksSnapshot, s, time.Now())
for i := 0; i < b.N; i++ {
err := asks.updateByID(asksSnapshot)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkLoad(b *testing.B) {
b.ReportAllocs()
s := newStack()
ts := linkedList{}
for i := 0; i < b.N; i++ {
ts.load(ask, s, time.Now())
}
}
func BenchmarkUpdateInsertByPrice_Amend(b *testing.B) {
b.ReportAllocs()
a := asks{}
stack := newStack()
a.load(ask, stack, time.Now())
updates := Items{
{
Price: 1337, // Amend
Amount: 2,
},
{
Price: 1337, // Amend
Amount: 1,
},
}
for i := 0; i < b.N; i++ {
a.updateInsertByPrice(updates, stack, 0, time.Now())
}
}
func BenchmarkUpdateInsertByPrice_Insert_Delete(b *testing.B) {
b.ReportAllocs()
a := asks{}
stack := newStack()
a.load(ask, stack, time.Now())
updates := Items{
{
Price: 1337.5, // Insert
Amount: 2,
},
{
Price: 1337.5, // Delete
Amount: 0,
},
}
for i := 0; i < b.N; i++ {
a.updateInsertByPrice(updates, stack, 0, time.Now())
}
}
func BenchmarkRetrieve(b *testing.B) {
b.ReportAllocs()
a := asks{}
stack := newStack()
asksSnapshot := Items{
{Price: 1, Amount: 1, ID: 1},
{Price: 3, Amount: 1, ID: 3},
{Price: 5, Amount: 1, ID: 5},
{Price: 7, Amount: 1, ID: 7},
{Price: 9, Amount: 1, ID: 9},
{Price: 11, Amount: 1, ID: 11},
}
a.load(asksSnapshot, stack, time.Now())
for i := 0; i < b.N; i++ {
_ = a.retrieve(int(6))
}
}
func BenchmarkUpdateInsertByID_bids(b *testing.B) {
b.ReportAllocs()
bids := bids{}
stack := newStack()
bidsSnapshot := Items{
{Price: 0.5, Amount: 2, ID: 0},
{Price: 1, Amount: 2, ID: 1},
{Price: 3, Amount: 2, ID: 3},
{Price: 12, Amount: 2, ID: 5},
{Price: 7, Amount: 2, ID: 7},
{Price: 9, Amount: 2, ID: 9},
{Price: 11, Amount: 2, ID: 11},
}
bids.load(bidsSnapshot, stack, time.Now())
for i := 0; i < b.N; i++ {
err := bids.updateInsertByID(bidsSnapshot, stack)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkDeleteByID(b *testing.B) {
b.ReportAllocs()
asks := asks{}
stack := newStack()
asksSnapshot := Items{
{Price: 1, Amount: 1, ID: 1},
{Price: 3, Amount: 1, ID: 3},
{Price: 5, Amount: 1, ID: 5},
{Price: 7, Amount: 1, ID: 7},
{Price: 9, Amount: 1, ID: 9},
{Price: 11, Amount: 1, ID: 11},
}
asks.load(asksSnapshot, stack, time.Now())
for i := 0; i < b.N; i++ {
err := asks.deleteByID(asksSnapshot, stack, false, time.Now())
if err != nil {
b.Fatal(err)
}
asks.load(asksSnapshot, stack, time.Now()) // reset
}
}
func BenchmarkUpdateInsertByID_asks(b *testing.B) {
b.ReportAllocs()
asks := asks{}
stack := newStack()
asksSnapshot := Items{
{Price: 1, Amount: 1, ID: 1},
{Price: 3, Amount: 1, ID: 3},
{Price: 5, Amount: 1, ID: 5},
{Price: 7, Amount: 1, ID: 7},
{Price: 9, Amount: 1, ID: 9},
{Price: 11, Amount: 1, ID: 11},
}
asks.load(asksSnapshot, stack, time.Now())
for i := 0; i < b.N; i++ {
err := asks.updateInsertByID(asksSnapshot, stack)
if err != nil {
b.Fatal(err)
}
}
}
func BenchmarkReverse(b *testing.B) {
b.ReportAllocs()
s := deploySliceOrdered()
if len(s) != 1000 {
b.Fatal("incorrect length")
}
for i := 0; i < b.N; i++ {
s.Reverse()
}
}
func BenchmarkSortAsksDecending(b *testing.B) {
b.ReportAllocs()
s := deploySliceOrdered()
bucket := make(Items, len(s))
for i := 0; i < b.N; i++ {
//nolint: gocritic
copy(bucket, s)
bucket.SortAsks()
}
}
func BenchmarkSortBidsAscending(b *testing.B) {
b.ReportAllocs()
s := deploySliceOrdered()
s.Reverse()
bucket := make(Items, len(s))
for i := 0; i < b.N; i++ {
copy(bucket, s)
bucket.SortBids()
}
}
func BenchmarkSortAsksStandard(b *testing.B) {
b.ReportAllocs()
s := deployUnorderedSlice()
bucket := make(Items, len(s))
for i := 0; i < b.N; i++ {
copy(bucket, s)
bucket.SortAsks()
}
}
func BenchmarkSortBidsStandard(b *testing.B) {
b.ReportAllocs()
s := deployUnorderedSlice()
bucket := make(Items, len(s))
for i := 0; i < b.N; i++ {
copy(bucket, s)
bucket.SortBids()
}
}
func BenchmarkSortAsksAscending(b *testing.B) {
b.ReportAllocs()
s := deploySliceOrdered()
bucket := make(Items, len(s))
for i := 0; i < b.N; i++ {
copy(bucket, s)
bucket.SortAsks()
}
}
func BenchmarkSortBidsDescending(b *testing.B) {
b.ReportAllocs()
s := deploySliceOrdered()
s.Reverse()
bucket := make(Items, len(s))
for i := 0; i < b.N; i++ {
copy(bucket, s)
bucket.SortBids()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment