Skip to content
Snippets Groups Projects
Commit 40211ad6 authored by Pawan Rawal's avatar Pawan Rawal
Browse files

Optimizations and refactoring

1. Changing extension of files with binary data to .bin.
2. Running separate benchmark for each query according to numElements in their
result.
parent 5f0ee3c9
No related branches found
No related tags found
No related merge requests found
......@@ -26,17 +26,22 @@ Directors query
}
}
14 May 2016
Benchmarking tests were run for ToJson and ToProtocolBuffer methods. Results
from the `go test` command are tabulated below.
BenchmarkToJson 500 3583970 ns/op 957747 B/op 16115 allocs/op
BenchmarkToProtocolBuffer 1000 2299409 ns/op 566288 B/op 7542 allocs/op
BenchmarkToJSON_10_Actor 20000 92797 ns/op 22616 B/op 319 allocs/op
BenchmarkToJSON_10_Director 20000 87246 ns/op 21111 B/op 303 allocs/op
BenchmarkToJSON_100_Actor 2000 774767 ns/op 207893 B/op 2670 allocs/op
BenchmarkToJSON_100_Director 2000 579467 ns/op 142811 B/op 2103 allocs/op
BenchmarkToJSON_1000_Actor 200 7903001 ns/op 1904863 B/op 24712 allocs/op
BenchmarkToJSON_1000_Director 300 4335375 ns/op 957728 B/op 16115 allocs/op
BenchmarkToPB_10_Actor 100000 19672 ns/op 3176 B/op 60 allocs/op
BenchmarkToPB_10_Director 100000 17891 ns/op 3096 B/op 60 allocs/op
BenchmarkToPB_100_Actor 10000 372288 ns/op 30728 B/op 556 allocs/op
BenchmarkToPB_100_Director 5000 221506 ns/op 37272 B/op 701 allocs/op
BenchmarkToPB_1000_Actor 500 2612757 ns/op 296486 B/op 5383 allocs/op
BenchmarkToPB_1000_Director 300 3980677 ns/op 395600 B/op 7376 allocs/op
We can see that ToProtocolBuffer method allocates less memory and takes lesser
time than ToJson method.
After changing properties inside a graph.Node from a map to a slice, we can see
further improvements.
BenchmarkToJson 500 3726982 ns/op 957679 B/op 16115 allocs/op
BenchmarkToProtocolBuffer 1000 1954618 ns/op 395603 B/op 7377 allocs/op
File moved
File moved
File moved
File moved
File moved
File moved
......@@ -338,17 +338,14 @@ func (g *SubGraph) preTraverse(uid uint64, dst *graph.Node) error {
}
v.Str = ival.(string)
properties = append(properties,
&graph.Property{Prop: pc.Attr, Val: v})
}
}
for i, p := range properties {
if p.Prop == "_xid_" {
dst.Xid = p.Val.Str
// Deleting the _xid_ property if it exists
properties = append(properties[:i], properties[i+1:]...)
break
if pc.Attr == "_xid_" {
dst.Xid = v.Str
} else {
p := &graph.Property{Prop: pc.Attr, Val: v}
properties = append(properties, p)
}
}
}
dst.Properties, dst.Children = properties, children
......
......@@ -326,16 +326,16 @@ func TestToJson(t *testing.T) {
fmt.Printf(string(js))
}
func getProperty(properties []*graph.Property, prop string) (v *graph.Value) {
func getProperty(properties []*graph.Property, prop string) *graph.Value {
for _, p := range properties {
if p.Prop == prop {
return p.Val
}
}
return v
return nil
}
func TestToProtocolBuffer(t *testing.T) {
func TestToPB(t *testing.T) {
dir, _ := populateGraph(t)
defer os.RemoveAll(dir)
......@@ -458,16 +458,14 @@ func benchmarkToJson(file string, b *testing.B) {
}
}
func BenchmarkToJson(b *testing.B) {
benchmarkToJson("benchmark/actors10.txt", b)
benchmarkToJson("benchmark/actors100.txt", b)
benchmarkToJson("benchmark/actors1000.txt", b)
benchmarkToJson("benchmark/directors10.txt", b)
benchmarkToJson("benchmark/directors100.txt", b)
benchmarkToJson("benchmark/directors1000.txt", b)
}
func BenchmarkToJSON_10_Actor(b *testing.B) { benchmarkToJson("benchmark/actors10.bin", b) }
func BenchmarkToJSON_10_Director(b *testing.B) { benchmarkToJson("benchmark/directors10.bin", b) }
func BenchmarkToJSON_100_Actor(b *testing.B) { benchmarkToJson("benchmark/actors100.bin", b) }
func BenchmarkToJSON_100_Director(b *testing.B) { benchmarkToJson("benchmark/directors100.bin", b) }
func BenchmarkToJSON_1000_Actor(b *testing.B) { benchmarkToJson("benchmark/actors1000.bin", b) }
func BenchmarkToJSON_1000_Director(b *testing.B) { benchmarkToJson("benchmark/directors1000.bin", b) }
func benchmarkToProtocolBuffer(file string, b *testing.B) {
func benchmarkToPB(file string, b *testing.B) {
b.ReportAllocs()
var sg SubGraph
var l Latency
......@@ -492,11 +490,9 @@ func benchmarkToProtocolBuffer(file string, b *testing.B) {
}
}
func BenchmarkToProtocolBuffer(b *testing.B) {
benchmarkToProtocolBuffer("benchmark/actors10.txt", b)
benchmarkToProtocolBuffer("benchmark/actors100.txt", b)
benchmarkToProtocolBuffer("benchmark/actors1000.txt", b)
benchmarkToProtocolBuffer("benchmark/directors10.txt", b)
benchmarkToProtocolBuffer("benchmark/directors100.txt", b)
benchmarkToProtocolBuffer("benchmark/directors1000.txt", b)
}
func BenchmarkToPB_10_Actor(b *testing.B) { benchmarkToPB("benchmark/actors10.bin", b) }
func BenchmarkToPB_10_Director(b *testing.B) { benchmarkToPB("benchmark/directors10.bin", b) }
func BenchmarkToPB_100_Actor(b *testing.B) { benchmarkToPB("benchmark/actors100.bin", b) }
func BenchmarkToPB_100_Director(b *testing.B) { benchmarkToPB("benchmark/directors100.bin", b) }
func BenchmarkToPB_1000_Actor(b *testing.B) { benchmarkToPB("benchmark/actors1000.bin", b) }
func BenchmarkToPB_1000_Director(b *testing.B) { benchmarkToPB("benchmark/directors1000.bin", b) }
......@@ -209,10 +209,7 @@ type server struct{}
// This method is used to execute the query and return the response to the
// client as a protocol buffer message.
func (s *server) Query(ctx context.Context,
req *graph.Request) (*graph.Response, error) {
node := new(graph.Node)
gl := new(graph.Latency)
resp := new(graph.Response)
req *graph.Request) (resp *graph.Response, err error) {
if len(req.Query) == 0 {
glog.Error("While reading query")
return resp, fmt.Errorf("Empty query")
......@@ -247,13 +244,14 @@ func (s *server) Query(ctx context.Context,
l.Processing = time.Since(l.Start) - l.Parsing
glog.WithField("q", req.Query).Debug("Graph processed.")
node, err = sg.ToProtocolBuffer(&l)
node, err := sg.ToProtocolBuffer(&l)
if err != nil {
x.Err(glog, err).Error("While converting to protocol buffer.")
return resp, err
}
resp.N = node
gl := new(graph.Latency)
gl.Parsing, gl.Processing, gl.Pb = l.Parsing.String(), l.Processing.String(),
l.ProtocolBuffer.String()
resp.L = gl
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment