Created
March 28, 2023 17:33
-
-
Save eatonphil/89ec5487a3bec40e7481081fdbf3abb4 to your computer and use it in GitHub Desktop.
timing sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"time" | |
"log" | |
"os" | |
"reflect" | |
tb "github.com/tigerbeetledb/tigerbeetle-go" | |
tb_types "github.com/tigerbeetledb/tigerbeetle-go/pkg/types" | |
) | |
func uint128(value string) tb_types.Uint128 { | |
x, err := tb_types.HexStringToUint128(value) | |
if err != nil { | |
panic(err) | |
} | |
return x | |
} | |
// Since we only require Go 1.17 we can't do this as a generic function | |
// even though that would be fine. So do the dynamic approach for now. | |
func assert(a, b interface{}, field string) { | |
if !reflect.DeepEqual(a, b) { | |
log.Fatalf("Expected %s to be [%+v (%T)], got: [%+v (%T)]", field, b, b, a, a) | |
} | |
} | |
func main() { | |
port := os.Getenv("TB_PORT") | |
if port == "" { | |
port = "3000" | |
} | |
client, err := tb.NewClient(0, []string{port}, 1) | |
if err != nil { | |
log.Fatalf("Error creating client: %s", err) | |
} | |
defer client.Close() | |
// Create two accounts | |
res, err := client.CreateAccounts([]tb_types.Account{ | |
{ | |
ID: uint128("1"), | |
Ledger: 1, | |
Code: 1, | |
}, | |
{ | |
ID: uint128("2"), | |
Ledger: 1, | |
Code: 1, | |
}, | |
}) | |
if err != nil { | |
log.Fatalf("Error creating accounts: %s", err) | |
} | |
for _, err := range res { | |
log.Fatalf("Error creating account %d: %s", err.Index, err.Result) | |
} | |
i := 0 | |
var total, min, max time.Duration | |
min = 1_000_000_000 | |
for i < 1000 { | |
i++ | |
t1 := time.Now() | |
transferRes, err := client.CreateTransfers([]tb_types.Transfer{ | |
{ | |
ID: uint128(fmt.Sprintf("%d", i)), | |
DebitAccountID: uint128("1"), | |
CreditAccountID: uint128("2"), | |
Ledger: 1, | |
Code: 1, | |
Amount: 10, | |
}, | |
}) | |
t2 := time.Now() | |
diff := t2.Sub(t1) | |
fmt.Println("Iteration", diff) | |
total += diff | |
if (diff < min) { | |
min = diff | |
} | |
if (diff > max) { | |
max = diff | |
} | |
if err != nil { | |
log.Fatalf("Error creating transfers: %s", err) | |
} | |
for _, err := range transferRes { | |
log.Fatalf("Error creating transfer: %s", err.Result) | |
} | |
} | |
fmt.Println("Average", total / 1_000, "Min", min, "Max", max) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment