|
| 1 | +// Copyright 2026 Specter Ops, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +package main |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "flag" |
| 22 | + "fmt" |
| 23 | + "os" |
| 24 | + "os/exec" |
| 25 | + "strings" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/specterops/dawgs" |
| 29 | + "github.com/specterops/dawgs/drivers/pg" |
| 30 | + "github.com/specterops/dawgs/graph" |
| 31 | + "github.com/specterops/dawgs/opengraph" |
| 32 | + "github.com/specterops/dawgs/util/size" |
| 33 | + |
| 34 | + _ "github.com/specterops/dawgs/drivers/neo4j" |
| 35 | +) |
| 36 | + |
| 37 | +func main() { |
| 38 | + var ( |
| 39 | + driver = flag.String("driver", "pg", "database driver (pg, neo4j)") |
| 40 | + connStr = flag.String("connection", "", "database connection string (or PG_CONNECTION_STRING)") |
| 41 | + iterations = flag.Int("iterations", 10, "timed iterations per scenario") |
| 42 | + output = flag.String("output", "", "markdown output file (default: stdout)") |
| 43 | + datasetDir = flag.String("dataset-dir", "integration/testdata", "path to testdata directory") |
| 44 | + localDataset = flag.String("local-dataset", "", "additional local dataset (e.g. local/phantom)") |
| 45 | + onlyDataset = flag.String("dataset", "", "run only this dataset (e.g. diamond, local/phantom)") |
| 46 | + ) |
| 47 | + |
| 48 | + flag.Parse() |
| 49 | + |
| 50 | + conn := *connStr |
| 51 | + if conn == "" { |
| 52 | + conn = os.Getenv("PG_CONNECTION_STRING") |
| 53 | + } |
| 54 | + if conn == "" { |
| 55 | + fatal("no connection string: set -connection flag or PG_CONNECTION_STRING env var") |
| 56 | + } |
| 57 | + |
| 58 | + ctx := context.Background() |
| 59 | + |
| 60 | + cfg := dawgs.Config{ |
| 61 | + GraphQueryMemoryLimit: size.Gibibyte, |
| 62 | + ConnectionString: conn, |
| 63 | + } |
| 64 | + |
| 65 | + if *driver == pg.DriverName { |
| 66 | + pool, err := pg.NewPool(conn) |
| 67 | + if err != nil { |
| 68 | + fatal("failed to create pool: %v", err) |
| 69 | + } |
| 70 | + cfg.Pool = pool |
| 71 | + } |
| 72 | + |
| 73 | + db, err := dawgs.Open(ctx, *driver, cfg) |
| 74 | + if err != nil { |
| 75 | + fatal("failed to open database: %v", err) |
| 76 | + } |
| 77 | + defer db.Close(ctx) |
| 78 | + |
| 79 | + // Build dataset list |
| 80 | + var datasets []string |
| 81 | + if *onlyDataset != "" { |
| 82 | + datasets = []string{*onlyDataset} |
| 83 | + } else { |
| 84 | + datasets = defaultDatasets |
| 85 | + if *localDataset != "" { |
| 86 | + datasets = append(datasets, *localDataset) |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + // Scan all datasets for kinds and assert schema |
| 91 | + nodeKinds, edgeKinds := scanKinds(*datasetDir, datasets) |
| 92 | + |
| 93 | + schema := graph.Schema{ |
| 94 | + Graphs: []graph.Graph{{ |
| 95 | + Name: "integration_test", |
| 96 | + Nodes: nodeKinds, |
| 97 | + Edges: edgeKinds, |
| 98 | + }}, |
| 99 | + DefaultGraph: graph.Graph{Name: "integration_test"}, |
| 100 | + } |
| 101 | + |
| 102 | + if err := db.AssertSchema(ctx, schema); err != nil { |
| 103 | + fatal("failed to assert schema: %v", err) |
| 104 | + } |
| 105 | + |
| 106 | + report := Report{ |
| 107 | + Driver: *driver, |
| 108 | + GitRef: gitRef(), |
| 109 | + Date: time.Now().Format("2006-01-02"), |
| 110 | + Iterations: *iterations, |
| 111 | + } |
| 112 | + |
| 113 | + for _, ds := range datasets { |
| 114 | + fmt.Fprintf(os.Stderr, "benchmarking %s...\n", ds) |
| 115 | + |
| 116 | + // Clear graph |
| 117 | + if err := db.WriteTransaction(ctx, func(tx graph.Transaction) error { |
| 118 | + return tx.Nodes().Delete() |
| 119 | + }); err != nil { |
| 120 | + fmt.Fprintf(os.Stderr, " clear failed: %v\n", err) |
| 121 | + continue |
| 122 | + } |
| 123 | + |
| 124 | + // Load dataset |
| 125 | + path := *datasetDir + "/" + ds + ".json" |
| 126 | + idMap, err := loadDataset(ctx, db, path) |
| 127 | + if err != nil { |
| 128 | + fmt.Fprintf(os.Stderr, " load failed: %v\n", err) |
| 129 | + continue |
| 130 | + } |
| 131 | + |
| 132 | + fmt.Fprintf(os.Stderr, " loaded %d nodes\n", len(idMap)) |
| 133 | + |
| 134 | + // Run scenarios |
| 135 | + for _, s := range scenariosForDataset(ds, idMap) { |
| 136 | + result, err := runScenario(ctx, db, s, *iterations) |
| 137 | + if err != nil { |
| 138 | + fmt.Fprintf(os.Stderr, " %s/%s failed: %v\n", s.Section, s.Label, err) |
| 139 | + continue |
| 140 | + } |
| 141 | + |
| 142 | + report.Results = append(report.Results, result) |
| 143 | + fmt.Fprintf(os.Stderr, " %s/%s: median=%s p95=%s max=%s\n", |
| 144 | + s.Section, s.Label, |
| 145 | + fmtDuration(result.Stats.Median), |
| 146 | + fmtDuration(result.Stats.P95), |
| 147 | + fmtDuration(result.Stats.Max), |
| 148 | + ) |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + // Write markdown |
| 153 | + var mdOut *os.File |
| 154 | + if *output != "" { |
| 155 | + var err error |
| 156 | + mdOut, err = os.Create(*output) |
| 157 | + if err != nil { |
| 158 | + fatal("failed to create output: %v", err) |
| 159 | + } |
| 160 | + defer mdOut.Close() |
| 161 | + } else { |
| 162 | + mdOut = os.Stdout |
| 163 | + } |
| 164 | + |
| 165 | + if err := writeMarkdown(mdOut, report); err != nil { |
| 166 | + fatal("failed to write markdown: %v", err) |
| 167 | + } |
| 168 | + |
| 169 | + if *output != "" { |
| 170 | + fmt.Fprintf(os.Stderr, "wrote %s\n", *output) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func scanKinds(datasetDir string, datasets []string) (graph.Kinds, graph.Kinds) { |
| 175 | + var nodeKinds, edgeKinds graph.Kinds |
| 176 | + |
| 177 | + for _, ds := range datasets { |
| 178 | + path := datasetDir + "/" + ds + ".json" |
| 179 | + f, err := os.Open(path) |
| 180 | + if err != nil { |
| 181 | + continue |
| 182 | + } |
| 183 | + |
| 184 | + doc, err := opengraph.ParseDocument(f) |
| 185 | + f.Close() |
| 186 | + if err != nil { |
| 187 | + continue |
| 188 | + } |
| 189 | + |
| 190 | + nk, ek := doc.Graph.Kinds() |
| 191 | + nodeKinds = nodeKinds.Add(nk...) |
| 192 | + edgeKinds = edgeKinds.Add(ek...) |
| 193 | + } |
| 194 | + |
| 195 | + return nodeKinds, edgeKinds |
| 196 | +} |
| 197 | + |
| 198 | +func loadDataset(ctx context.Context, db graph.Database, path string) (opengraph.IDMap, error) { |
| 199 | + f, err := os.Open(path) |
| 200 | + if err != nil { |
| 201 | + return nil, err |
| 202 | + } |
| 203 | + defer f.Close() |
| 204 | + |
| 205 | + return opengraph.Load(ctx, db, f) |
| 206 | +} |
| 207 | + |
| 208 | +func gitRef() string { |
| 209 | + out, err := exec.Command("git", "rev-parse", "--short", "HEAD").Output() |
| 210 | + if err != nil { |
| 211 | + return "unknown" |
| 212 | + } |
| 213 | + return strings.TrimSpace(string(out)) |
| 214 | +} |
| 215 | + |
| 216 | +func fatal(format string, args ...any) { |
| 217 | + fmt.Fprintf(os.Stderr, format+"\n", args...) |
| 218 | + os.Exit(1) |
| 219 | +} |
0 commit comments