-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
332 lines (273 loc) · 10.3 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package main
import (
"context"
"database/sql"
_ "database/sql"
e "errors"
"fmt"
v1 "github.com/authzed/authzed-go/proto/authzed/api/v1"
"github.com/authzed/authzed-go/v1"
"github.com/lib/pq"
"io"
"log"
"math"
"sort"
"strings"
"time"
_ "github.com/lib/pq"
"github.com/lpichler/benchmark-sql-spicedb-poc/client"
)
var (
spiceDBURL = "localhost:50051"
spiceDBToken = "foobar"
SpiceDbClient *authzed.Client
connectionString = "postgres://liborpichler:toor@localhost:5432/rbac?sslmode=disable"
)
func initSpiceDB() {
if SpiceDbClient == nil {
SpiceDbClient = client.InitServer(spiceDBURL, spiceDBToken)
}
}
var benchmarkSpicedbUserLargeResults []time.Duration
var benchmarkSpicedbUserSmallResults []time.Duration
var benchmarkPureSpicedbUserMidResults []time.Duration
var benchmarkPureSpicedbUser60000Results []time.Duration
var benchmarkFDWUserLargeResults []time.Duration
var benchmarkFDWUserSmallResults []time.Duration
var benchmarkFDWUserMidResults []time.Duration
var benchmarkFDWUser60000Results []time.Duration
var benchmarkJoinQueryFWDUserLargeResults []time.Duration
var benchmarkJoinQueryFWDUserSmallResults []time.Duration
var benchmarkJoinQueryFWDUserMidResults []time.Duration
var benchmarkJoinQueryFWDUser60000Results []time.Duration
var benchmarkJoinQuerySQLSpicedbUserLargeResults []time.Duration
var benchmarkJoinQuerySQLSpicedbUserSmallResults []time.Duration
var benchmarkJoinQuerySQLSpicedbUserMidResults []time.Duration
var benchmarkJoinQuerySQLSpicedbUser60000Results []time.Duration
// Function 1 to measure
func benchmarkPureSpicedb1(userName, permission string) []string {
ctx := context.Background()
lrClient, err := SpiceDbClient.LookupResources(ctx, &v1.LookupResourcesRequest{
ResourceObjectType: "workspace",
Permission: permission,
Subject: &v1.SubjectReference{
Object: &v1.ObjectReference{
ObjectType: "user",
ObjectId: userName,
},
},
})
if err != nil {
return []string{}
}
var workspaces []string
for {
next, err := lrClient.Recv()
if e.Is(err, io.EOF) {
break
}
if err != nil {
return []string{}
}
workspaces = append(workspaces, next.GetResourceObjectId())
}
return workspaces
}
// startTimer starts a timer for a block of code, and records the elapsed time.
func startTimer() time.Time {
return time.Now()
}
// stopTimer stops the timer and records the elapsed time.
func stopTimer(startTime time.Time, results *[]time.Duration) {
elapsedTime := time.Since(startTime)
*results = append(*results, elapsedTime)
}
// displayStatistics calculates and displays statistics for timing results.
func displayStatistics(blockName string, results []time.Duration) {
var total time.Duration
minimum := time.Duration(math.MaxInt64)
maximum := time.Duration(0)
count := len(results)
for _, duration := range results {
total += duration
if duration < minimum {
minimum = duration
}
if duration > maximum {
maximum = duration
}
}
average := total / time.Duration(count)
// Sort results for percentile calculation
sortedResults := make([]time.Duration, len(results))
copy(sortedResults, results)
sort.Slice(sortedResults, func(i, j int) bool { return sortedResults[i] < sortedResults[j] })
// Calculate percentiles
p95 := sortedResults[int(float64(count)*0.95)-1]
p99 := sortedResults[int(float64(count)*0.99)-1]
// Display in table format
fmt.Printf("Block: %s\n", blockName)
fmt.Println("---------------------------------------------------------")
fmt.Printf("| %12s | %12s | %12s | %12s | %12s |\n", "Min", "Max", "Average", "P95", "P99")
fmt.Println("---------------------------------------------------------")
fmt.Printf("| %12s | %12s | %12s | %12s | %12s |\n", minimum, maximum, average, p95, p99)
fmt.Println("---------------------------------------------------------")
}
func openDB(connectionString string) *sql.DB {
db, err := sql.Open("postgres", connectionString)
if err != nil {
log.Fatalf("Failed to open a DB connection: %v", err)
}
err = db.Ping()
if err != nil {
log.Fatalf("Failed to connect to the DB: %v", err)
}
return db
}
func queryDB(db *sql.DB, username, permission string) []string {
query := `SELECT name FROM public.workspaces WHERE user_name=$1 AND permission=$2;`
rows, err := db.Query(query, username, permission)
if err != nil {
log.Fatalf("Failed to execute a query: %v", err)
}
defer rows.Close()
var workspaces []string
for rows.Next() {
var columnName string
if err := rows.Scan(&columnName); err != nil {
log.Fatal(err)
}
// fmt.Println(columnName)
workspaces = append(workspaces, columnName)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
return workspaces
}
func queryDBJoin(db *sql.DB, workspaces []string) {
if len(workspaces) > 65535 {
return
}
placeholders := make([]string, len(workspaces))
for i := range workspaces {
placeholders[i] = fmt.Sprintf("$%d", i+1)
}
query := fmt.Sprintf("SELECT COUNT(*) FROM items WHERE workspace IN (%s);", strings.Join(placeholders, ","))
args := make([]interface{}, len(workspaces))
for i, v := range workspaces {
args[i] = v
}
row := db.QueryRow(query, args...)
var count int
if err := row.Scan(&count); err != nil {
fmt.Printf("Error: %v\n", err)
}
}
func queryDBJoinUNEST(db *sql.DB, workspaces []string) {
// Construct the query using UNNEST to handle the array of workspaces
query := "SELECT COUNT(*) FROM items WHERE workspace IN (SELECT * FROM UNNEST($1::text[]));"
// Convert workspaces slice to an interface{} using pq.Array for the query argument
// This ensures the slice is passed correctly to the query
// Execute the query
row := db.QueryRow(query, pq.Array(workspaces))
var count int
if err := row.Scan(&count); err != nil {
fmt.Printf("Error: %v\n", err)
} else {
fmt.Printf("Count: %d\n", count)
}
}
func queryDBJoinFWD(db *sql.DB, username, permission string) {
query := fmt.Sprintf("SELECT COUNT(*) FROM items INNER JOIN workspaces ON workspaces.name=items.workspace WHERE user_name=$1 AND permission=$2")
rows, err := db.Query(query, username, permission)
if err != nil {
log.Fatalf("Failed to execute a query: %v", err)
}
defer rows.Close()
for rows.Next() {
var columnName string
if err := rows.Scan(&columnName); err != nil {
log.Fatal(err)
}
//fmt.Println(columnName)
}
if err := rows.Err(); err != nil {
log.Fatal(err)
}
}
func main() {
db := openDB(connectionString)
defer db.Close()
initSpiceDB()
for i := 0; i < 100; i++ {
startTime1 := startTimer()
benchmarkPureSpicedb1("userlarge", "inventory_all_read")
stopTimer(startTime1, &benchmarkSpicedbUserLargeResults)
startTime2 := startTimer()
benchmarkPureSpicedb1("usersmall", "inventory_all_read")
stopTimer(startTime2, &benchmarkSpicedbUserSmallResults)
startTime21 := startTimer()
benchmarkPureSpicedb1("usermid", "inventory_all_read")
stopTimer(startTime21, &benchmarkPureSpicedbUserMidResults)
startTime22 := startTimer()
benchmarkPureSpicedb1("user60000", "inventory_all_read")
stopTimer(startTime22, &benchmarkPureSpicedbUser60000Results)
startTime3 := startTimer()
queryDB(db, "userlarge", "inventory_all_read")
stopTimer(startTime3, &benchmarkFDWUserLargeResults)
startTime4 := startTimer()
queryDB(db, "usersmall", "inventory_all_read")
stopTimer(startTime4, &benchmarkFDWUserSmallResults)
startTime31 := startTimer()
queryDB(db, "usermid", "inventory_all_read")
stopTimer(startTime31, &benchmarkFDWUserMidResults)
startTime32 := startTimer()
queryDB(db, "user60000", "inventory_all_read")
stopTimer(startTime32, &benchmarkFDWUser60000Results)
startTime5 := startTimer()
queryDBJoinFWD(db, "userlarge", "inventory_all_read")
stopTimer(startTime5, &benchmarkJoinQueryFWDUserLargeResults)
startTime6 := startTimer()
queryDBJoinFWD(db, "usersmall", "inventory_all_read")
stopTimer(startTime6, &benchmarkJoinQueryFWDUserSmallResults)
startTime51 := startTimer()
queryDBJoinFWD(db, "usermid", "inventory_all_read")
stopTimer(startTime51, &benchmarkJoinQueryFWDUserMidResults)
startTime52 := startTimer()
queryDBJoinFWD(db, "user60000", "inventory_all_read")
stopTimer(startTime52, &benchmarkJoinQueryFWDUser60000Results)
startTime7 := startTimer()
workspaces := benchmarkPureSpicedb1("userlarge", "inventory_all_read")
queryDBJoinUNEST(db, workspaces)
stopTimer(startTime7, &benchmarkJoinQuerySQLSpicedbUserLargeResults)
startTime8 := startTimer()
workspaces = benchmarkPureSpicedb1("usersmall", "inventory_all_read")
queryDBJoin(db, workspaces)
stopTimer(startTime8, &benchmarkJoinQuerySQLSpicedbUserSmallResults)
startTime71 := startTimer()
workspaces = benchmarkPureSpicedb1("usermid", "inventory_all_read")
queryDBJoin(db, workspaces)
stopTimer(startTime71, &benchmarkJoinQuerySQLSpicedbUserMidResults)
startTime72 := startTimer()
workspaces = benchmarkPureSpicedb1("user60000", "inventory_all_read")
queryDBJoin(db, workspaces)
stopTimer(startTime72, &benchmarkJoinQuerySQLSpicedbUser60000Results)
}
displayStatistics("SpiceDB call Only(large user)", benchmarkSpicedbUserLargeResults)
displayStatistics("SpiceDB call Only(small user)", benchmarkSpicedbUserSmallResults)
displayStatistics("SpiceDB call Only(usermid)", benchmarkPureSpicedbUserMidResults)
displayStatistics("SpiceDB call Only(user60000)", benchmarkPureSpicedbUser60000Results)
displayStatistics("FDW query(large user)", benchmarkFDWUserLargeResults)
displayStatistics("FDW query(small user)", benchmarkFDWUserSmallResults)
displayStatistics("FDW query(usermid)", benchmarkFDWUserMidResults)
displayStatistics("FDW query(user60000)", benchmarkFDWUser60000Results)
displayStatistics("Join SQL with FWD query(large user)", benchmarkJoinQueryFWDUserLargeResults)
displayStatistics("Join SQL with FWD query(small user)", benchmarkJoinQueryFWDUserSmallResults)
displayStatistics("Join SQL with FWD query(usermid)", benchmarkJoinQueryFWDUserMidResults)
displayStatistics("Join SQL with FWD query(user60000)", benchmarkJoinQueryFWDUser60000Results)
displayStatistics("Join SQL(UNNEST instead of IN) with SpiceDB call(large user)", benchmarkJoinQuerySQLSpicedbUserLargeResults)
displayStatistics("Join SQL with SpiceDB call(small user)", benchmarkJoinQuerySQLSpicedbUserSmallResults)
displayStatistics("Join SQL with SpiceDB call(usermid)", benchmarkJoinQuerySQLSpicedbUserMidResults)
displayStatistics("Join SQL with SpiceDB call(usermid60000)", benchmarkJoinQuerySQLSpicedbUser60000Results)
}