-
Notifications
You must be signed in to change notification settings - Fork 6
/
macrobenchmark.go
67 lines (62 loc) · 1.73 KB
/
macrobenchmark.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
package macrobenchmark
import (
"context"
"database/sql"
"errors"
"fmt"
"time"
)
type Macrobenchmark struct {
ID uint64
ExecutionID uint64
WorkloadID uint64
StartTime time.Time
FinishTime time.Time
}
func GetOrCreate(ctx context.Context, db *sql.DB, m Macrobenchmark) (ret Macrobenchmark, err error) {
ret, err = Get(ctx, db, m.ExecutionID, m.WorkloadID)
if err == nil {
return ret, nil
}
if !errors.Is(err, sql.ErrNoRows) {
return ret, err
}
if err := Create(ctx, db, m); err != nil {
return Macrobenchmark{}, err
}
return Get(ctx, db, m.ExecutionID, m.WorkloadID)
}
func Create(ctx context.Context, db *sql.DB, m Macrobenchmark) error {
stmt, err := db.PrepareContext(ctx, `
INSERT INTO macrobenchmark (execution_id, workload_id, start_time, finish_time)
VALUES ($1, $2, $3, $4)
ON CONFLICT (execution_id, workload_id) DO NOTHING
`)
if err != nil {
return fmt.Errorf("macrobenchmark: %w", err)
}
defer func() {
_ = stmt.Close()
}()
_, err = stmt.ExecContext(ctx, m.ExecutionID, m.WorkloadID, m.StartTime, m.FinishTime)
return err
}
func Get(ctx context.Context, db *sql.DB, executionID, workloadID uint64) (ret Macrobenchmark, err error) {
stmt, err := db.PrepareContext(ctx, `
SELECT id, execution_id, workload_id, start_time, finish_time
FROM macrobenchmark
WHERE execution_id = $1 AND workload_id = $2
`)
if err != nil {
return ret, fmt.Errorf("macrobenchmark: %w", err)
}
defer func() {
_ = stmt.Close()
}()
row := stmt.QueryRowContext(ctx, executionID, workloadID)
err = row.Scan(&ret.ID, &ret.ExecutionID, &ret.WorkloadID, &ret.StartTime, &ret.FinishTime)
if err != nil {
return ret, fmt.Errorf("macrobenchmark: %w", err)
}
return ret, nil
}