Skip to content

Instantly share code, notes, and snippets.

@benhenryhunter
Last active May 5, 2023 20:27
Show Gist options
  • Save benhenryhunter/74e85e1a207cb37bff7a80643ff5c30c to your computer and use it in GitHub Desktop.
Save benhenryhunter/74e85e1a207cb37bff7a80643ff5c30c to your computer and use it in GitHub Desktop.
SSZ vs JSON redis set and get
package main
import (
"context"
"os"
"time"
"log"
"github.com/attestantio/go-eth2-client/spec/capella"
"github.com/redis/go-redis/v9"
)
/*
sample output:
2023/05/03 17:10:01 478 µs: Read JSON
2023/05/03 17:10:01 4208 µs: Unmarshal JSON
2023/05/03 17:10:01 32072 µs: SetJSON
2023/05/03 17:10:01 25971 µs: GetJSON
2023/05/03 17:10:01 1098 µs: Read SSZ
2023/05/03 17:10:01 38 µs: Unmarshal SSZ
2023/05/03 17:10:01 22589 µs: SetSSZ
2023/05/03 17:10:01 9624 µs: GetSSZ
*/
func main() {
redisClient, err := Connect(":6379", 10)
if err != nil {
panic(err)
}
payloadJSON := capella.ExecutionPayload{}
readJSON := time.Now()
payloadBytes, err := os.ReadFile("payload.json")
if err != nil {
panic(err)
}
log.Printf("%v µs: Read JSON ", time.Since(readJSON).Microseconds())
unmarshalJSON := time.Now()
if err := payloadJSON.UnmarshalJSON(payloadBytes); err != nil {
panic(err)
}
log.Printf("%v µs: Unmarshal JSON", time.Since(unmarshalJSON).Microseconds())
startSetRedisJSON := time.Now()
if err := redisClient.SetJSON(context.Background(), "json-key", payloadJSON); err != nil {
panic(err)
}
log.Printf("%v µs: SetJSON", time.Since(startSetRedisJSON).Microseconds())
startGetRedisJSON := time.Now()
targetJSON := capella.ExecutionPayload{}
if err := redisClient.GetJSON(context.Background(), "json-key", &targetJSON); err != nil {
panic(err)
}
log.Printf("%v µs: GetJSON", time.Since(startGetRedisJSON).Microseconds())
jsonBytes, err := payloadJSON.MarshalJSON()
if err != nil {
panic(err)
}
payloadJSONTOSSZ := capella.ExecutionPayload{}
if err := payloadJSONTOSSZ.UnmarshalJSON(jsonBytes); err != nil {
panic(err)
}
payloadSSZ := capella.ExecutionPayload{}
readSSZ := time.Now()
payloadBytesSSZ, err := os.ReadFile("payload.ssz")
if err != nil {
panic(err)
}
log.Printf("%v µs: Read SSZ ", time.Since(readSSZ).Microseconds())
unmarshalSSZ := time.Now()
if err := payloadSSZ.UnmarshalSSZ(payloadBytesSSZ); err != nil {
panic(err)
}
log.Printf("%v µs: Unmarshal SSZ", time.Since(unmarshalSSZ).Microseconds())
startSetRedisSSZ := time.Now()
if err := redisClient.SetSSZ(context.Background(), "SSZ-key", payloadSSZ); err != nil {
panic(err)
}
log.Printf("%v µs: SetSSZ", time.Since(startSetRedisSSZ).Microseconds())
startGetRedisSSZ := time.Now()
targetSSZ := new(capella.ExecutionPayload)
if err := redisClient.GetSSZ(context.Background(), "SSZ-key", targetSSZ); err != nil {
panic(err)
}
log.Printf("%v µs: GetSSZ", time.Since(startGetRedisSSZ).Microseconds())
}
type RedisConn struct {
Client *redis.Client
}
func Connect(redisURI string, connectionPoolLimit int) (*RedisConn, error) {
redisClient := redis.NewClient(&redis.Options{
PoolSize: connectionPoolLimit,
Addr: redisURI,
MinIdleConns: connectionPoolLimit / 2,
PoolTimeout: 20 * time.Second,
ReadTimeout: 20 * time.Second,
WriteTimeout: 15 * time.Second,
})
if _, err := redisClient.Ping(context.Background()).Result(); err != nil {
// unable to connect to redis
return nil, err
}
conn := RedisConn{
Client: redisClient,
}
return &conn, nil
}
func (r *RedisConn) SetSSZ(ctx context.Context, key string, data capella.ExecutionPayload) error {
b, err := data.MarshalSSZ()
if err != nil {
return err
}
return r.Client.Set(ctx, key, string(b), 0).Err()
}
func (r *RedisConn) GetSSZ(ctx context.Context, key string, data *capella.ExecutionPayload) error {
resString, err := r.Client.Get(ctx, key).Result()
if err != nil {
return err
}
return data.UnmarshalSSZ([]byte(resString))
}
func (r *RedisConn) SetJSON(ctx context.Context, key string, data capella.ExecutionPayload) error {
b, err := data.MarshalJSON()
if err != nil {
return err
}
return r.Client.Set(ctx, key, b, 0).Err()
}
func (r *RedisConn) GetJSON(ctx context.Context, key string, data *capella.ExecutionPayload) error {
resString, err := r.Client.Get(ctx, key).Result()
if err != nil {
return err
}
return data.UnmarshalJSON([]byte(resString))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment