Skip to content

Instantly share code, notes, and snippets.

@lambrospetrou
Last active August 26, 2022 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambrospetrou/4e838fa52ff720131875fbb4ad6cb6f2 to your computer and use it in GitHub Desktop.
Save lambrospetrou/4e838fa52ff720131875fbb4ad6cb6f2 to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"context"
"log"
"net/http"
"os"
"strconv"
"sync"
"time"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/go-redis/redis/v8"
)
func redisRest(urlPath string) string {
req, _ := http.NewRequest(http.MethodGet, "https://eu1-actual-dragon-11111.upstash.io"+urlPath, nil)
req.Header.Add("Authorization", "Bearer <REPLACE_WITH_TOKEN>")
resp, err := http.DefaultClient.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
s, _ := bufio.NewReader(resp.Body).ReadString('|')
return s
}
func timeit(name string, f func()) {
start := time.Now()
f()
duration := time.Since(start)
log.Println(name+" elapsed time: ", duration)
}
var client *redis.Client = nil
func init() {
opt, _ := redis.ParseURL("rediss://:<REPLACE_WITH_PASSWORD>@eu1-actual-dragon-11111.upstash.io:38214")
client = redis.NewClient(opt)
}
func run(ctx context.Context) {
var wg sync.WaitGroup
timesStr := os.Getenv("TIMES")
if timesStr == "" {
timesStr = "1"
}
times, _ := strconv.Atoi(timesStr)
for j := 0; j < times; j++ {
wg.Add(1)
go timeit("go-redis", func() {
for i := 0; i < times; i++ {
client.Set(ctx, "foo", "bar", 0)
client.Get(ctx, "foo").Val()
client.HSet(ctx, "foohash", "barkey", "hashval", "bar2key", "bar2val")
client.HGet(ctx, "foohash", "barkey")
}
wg.Done()
})
}
for j := 0; j < times; j++ {
wg.Add(1)
go timeit("rest redis", func() {
for i := 0; i < times; i++ {
redisRest("/set/foo-rest/bar")
redisRest("/get/foo-rest")
redisRest("/hset/foohash-rest/barkey/hashval/bar2key/bar2val")
redisRest("/hget/foohash-rest/barkey")
}
wg.Done()
})
}
wg.Wait()
}
// https://github.com/aws/aws-lambda-go/blob/main/events/lambda_function_urls.go
func HandleRequest(ctx context.Context, request events.LambdaFunctionURLRequest) (events.LambdaFunctionURLResponse, error) {
log.Println(request)
run(ctx)
return events.LambdaFunctionURLResponse{Body: "success", StatusCode: 200}, nil
}
func main() {
lambda.Start(HandleRequest)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment