Skip to content

Commit 96b2ecc

Browse files
committed
speed up inventory query and other housekeeping improvements
1 parent 2a2f1a6 commit 96b2ecc

File tree

3 files changed

+63
-25
lines changed

3 files changed

+63
-25
lines changed

cmd/cyan/main.go

Lines changed: 60 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"log"
1111
"os"
1212
"os/exec"
13+
"runtime/pprof"
1314
"strconv"
1415
"strings"
1516
"text/tabwriter"
@@ -99,10 +100,22 @@ func initdb() {
99100
post.Process(db)
100101
}
101102

103+
var cpuprofile = flag.String("cpuprofile", "", "write cpu profile to file")
104+
102105
func main() {
103106
log.SetFlags(0)
104107
flag.Parse()
105108

109+
flag.Parse()
110+
if *cpuprofile != "" {
111+
f, err := os.Create(*cpuprofile)
112+
if err != nil {
113+
log.Fatal(err)
114+
}
115+
pprof.StartCPUProfile(f)
116+
defer pprof.StopCPUProfile()
117+
}
118+
106119
if *help || flag.NArg() < 1 {
107120
fmt.Println("Usage: cyan -db <cyclus-db> [flags...] <command> [flags...] [args...]")
108121
fmt.Println("Computes metrics for cyclus simulation data in a sqlite database.")
@@ -136,33 +149,45 @@ func doCustom(cmd string, args ...interface{}) *bytes.Buffer {
136149
tw := tabwriter.NewWriter(&buf, 4, 4, 1, ' ', 0)
137150
cols, err := rows.Columns()
138151
fatalif(err)
139-
for _, c := range cols {
152+
153+
simidcol := -1
154+
for i, c := range cols {
155+
if strings.Contains(strings.ToLower(c), "simid") {
156+
simidcol = i
157+
}
140158
_, err := tw.Write([]byte(c + "\t"))
141159
fatalif(err)
142160
}
143161
_, err = tw.Write([]byte("\n"))
144162
fatalif(err)
145163

164+
vs := make([]interface{}, len(cols))
165+
vals := make([]*sql.NullString, len(cols))
166+
for i := range vals {
167+
vals[i] = &sql.NullString{}
168+
vs[i] = vals[i]
169+
}
170+
146171
for rows.Next() {
147-
vs := make([]interface{}, len(cols))
148-
vals := make([]*sql.NullString, len(cols))
149172
for i := range vals {
150-
vals[i] = &sql.NullString{}
151-
vs[i] = vals[i]
173+
vals[i].Valid = false
152174
}
175+
153176
err := rows.Scan(vs...)
154177
fatalif(err)
155178

156179
for i, v := range vals {
157-
s := "NULL"
158180
if v.Valid {
159181
s = v.String
160-
if strings.Contains(strings.ToLower(cols[i]), "simid") {
182+
if i == simidcol {
161183
s = uuid.UUID(v.String).String()
162184
}
185+
tw.Write([]byte(s + "\t"))
186+
} else {
187+
tw.Write([]byte("NULL\t"))
163188
}
164-
fmt.Fprintf(tw, "%v\t", s)
165189
}
190+
166191
_, err = tw.Write([]byte("\n"))
167192
fatalif(err)
168193
}
@@ -335,24 +360,36 @@ func doInv(cmd string, args []string) {
335360
initdb()
336361

337362
proto := fs.Arg(0)
338-
s := `SELECT tl.Time AS Time,TOTAL(sub.qty) AS Quantity FROM timelist as tl
339-
LEFT JOIN (
340-
SELECT tl.simid AS simid, tl.Time as time,TOTAL(inv.Quantity*c.MassFrac) AS qty
341-
FROM timelist as tl
342-
JOIN inventories as inv on inv.starttime <= tl.time and inv.endtime > tl.time AND tl.simid=inv.simid
343-
JOIN agents as a on a.agentid=inv.agentid AND a.simid=inv.simid
344-
JOIN compositions as c on c.qualid=inv.qualid AND c.simid=inv.simid
345-
WHERE a.simid=? AND a.prototype=? {{.}}
346-
GROUP BY tl.Time
347-
) AS sub ON sub.time=tl.time AND sub.simid=tl.simid
348-
WHERE tl.simid=?
349-
GROUP BY tl.Time;
350-
`
363+
364+
filter := nuclidefilter(*nucs)
365+
s := ""
366+
if filter != "" {
367+
s = `SELECT tl.Time AS Time,IFNULL(sub.qty, 0) FROM timelist as tl
368+
LEFT JOIN (
369+
SELECT tl.Time as time,SUM(inv.Quantity*c.MassFrac) AS qty
370+
FROM timelist as tl
371+
JOIN inventories as inv on UNLIKELY(inv.starttime <= tl.time AND inv.endtime > tl.time) AND tl.simid=inv.simid
372+
JOIN agents as a on a.agentid=inv.agentid AND a.simid=inv.simid
373+
JOIN compositions as c on c.qualid=inv.qualid AND c.simid=inv.simid
374+
WHERE a.simid=? AND a.prototype=? {{.}}
375+
GROUP BY tl.Time
376+
) AS sub ON sub.time=tl.time WHERE tl.simid=?`
377+
} else {
378+
s = `SELECT tl.Time AS Time,IFNULL(sub.qty, 0) FROM timelist as tl
379+
LEFT JOIN (
380+
SELECT tl.Time as time,SUM(inv.Quantity) AS qty
381+
FROM timelist as tl
382+
JOIN inventories as inv on UNLIKELY(inv.starttime <= tl.time AND inv.endtime > tl.time) AND tl.simid=inv.simid
383+
JOIN agents as a on a.agentid=inv.agentid AND a.simid=inv.simid
384+
WHERE a.simid=? AND a.prototype=? {{.}}
385+
GROUP BY tl.Time
386+
) AS sub ON sub.time=tl.time
387+
WHERE tl.simid=?`
388+
}
351389

352390
tmpl := template.Must(template.New("sql").Parse(s))
353391
var buf bytes.Buffer
354-
tmpl.Execute(&buf, nuclidefilter(*nucs))
355-
fmt.Println(buf.String())
392+
tmpl.Execute(&buf, filter)
356393
customSql[cmd] = buf.String()
357394
buff := doCustom(cmd, simid, proto, simid)
358395
if *plotit {

nuc/nuc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ func Id(nuc string) (Nuc, error) {
2020
defer C.free(unsafe.Pointer(cs))
2121
n := Nuc(C.id_str(cs))
2222
if n < 0 {
23-
return 0, fmt.Errorf("'%v' is not a valid nuclide", n)
23+
return 0, fmt.Errorf("'%v' is not a valid nuclide", nuc)
2424
}
2525
return n, nil
2626
}
2727

2828
func IdFromInt(nuc int) (Nuc, error) {
2929
n := Nuc(C.id_int(C.int(nuc)))
3030
if n < 0 {
31-
return 0, fmt.Errorf("'%v' is not a valid nuclide", n)
31+
return 0, fmt.Errorf("'%v' is not a valid nuclide", nuc)
3232
}
3333
return n, nil
3434
}

post/walker.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ var (
3535
query.Index("Agents", "SimId", "AgentId", "Prototype"),
3636
query.Index("Inventories", "SimId", "AgentId", "StartTime", "EndTime", "Quantity"),
3737
query.Index("Inventories", "SimId", "ResourceId", "StartTime"),
38+
query.Index("Inventories", "SimId", "StartTime", "EndTime", "ResourceId", "Quantity"),
3839
"ANALYZE;",
3940
}
4041
dumpSql = "INSERT INTO Inventories VALUES (?,?,?,?,?,?,?);"

0 commit comments

Comments
 (0)