github.com/unigraph-dev/dgraph@v1.1.1-0.20200923154953-8b52b426f765/query/benchmark_test.go (about)

     1  /*
     2   * Copyright 2016-2018 Dgraph Labs, Inc. and Contributors
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package query
    18  
    19  /*
    20  // TODO: Fix this test.
    21  func prepareTest(b *testing.B) (dgs.Store, string, string) {
    22  	dir, err := ioutil.TempDir("", "storetest_")
    23  	if err != nil {
    24  		b.Fatal(err)
    25  	}
    26  
    27  	ps, err := store.NewStore(dir)
    28  	if err != nil {
    29  		b.Fatal(err)
    30  	}
    31  
    32  	schema.ParseBytes([]byte(""))
    33  	posting.Init(ps)
    34  	worker.Init(ps)
    35  
    36  	group.ParseGroupConfig("")
    37  	dir2, err := ioutil.TempDir("", "wal_")
    38  	if err != nil {
    39  		b.Fatal(err)
    40  	}
    41  	worker.StartRaftNodes(dir2)
    42  	time.Sleep(3 * time.Second)
    43  	return ps, dir, dir2
    44  }
    45  
    46  func buildValueList(data []string) *taskp.ValueList {
    47  	b := flatbuffers.NewBuilder(0)
    48  	offsets := make([]flatbuffers.UOffsetT, 0, len(data))
    49  	for _, s := range data {
    50  		bvo := b.CreateString(s)
    51  		taskp.ValueStart(b)
    52  		taskp.ValueAddVal(b, bvo)
    53  		offsets = append(offsets, taskp.ValueEnd(b))
    54  	}
    55  
    56  	taskp.ValueListStartValuesVector(b, len(data))
    57  	for i := 0; i < len(data); i++ {
    58  		b.PrependUOffsetT(offsets[i])
    59  	}
    60  	voffset := b.EndVector(len(data))
    61  
    62  	taskp.ValueListStart(b)
    63  	taskp.ValueListAddValues(b, voffset)
    64  	b.Finish(taskp.ValueListEnd(b))
    65  	buf := b.FinishedBytes()
    66  
    67  	out := new(x.ValueList)
    68  	x.Check(out.UnmarshalBinary(buf))
    69  	return out
    70  }
    71  
    72  // benchmarkHelper runs against some data from benchmark folder.
    73  func benchmarkHelper(b *testing.B, f func(*testing.B, *SubGraph)) {
    74  	for _, s := range []string{"actor", "director"} {
    75  		for i := 0; i < 3; i++ {
    76  			label := fmt.Sprintf("%s_%d", s, i)
    77  			filename := fmt.Sprintf("benchmark/%s.%d.gob", s, i)
    78  			if !b.Run(label, func(b *testing.B) {
    79  				b.ReportAllocs()
    80  				sg := new(SubGraph)
    81  				data, err := ioutil.ReadFile(filename)
    82  				if err != nil {
    83  					b.Fatal(err)
    84  				}
    85  				buf := bytes.NewBuffer(data)
    86  				dec := gob.NewDecoder(buf)
    87  				err = dec.Decode(sg)
    88  				if err != nil {
    89  					b.Fatal(err)
    90  				}
    91  				f(b, sg)
    92  			}) {
    93  				b.FailNow()
    94  			}
    95  		}
    96  	}
    97  }
    98  
    99  func BenchmarkToJSON(b *testing.B) {
   100  	benchmarkHelper(b, func(b *testing.B, sg *SubGraph) {
   101  		var l Latency
   102  		b.ResetTimer()
   103  		for i := 0; i < b.N; i++ {
   104  			if _, err := sg.ToJSON(&l); err != nil {
   105  				b.Fatal(err)
   106  			}
   107  		}
   108  	})
   109  }
   110  
   111  func BenchmarkToProto(b *testing.B) {
   112  	benchmarkHelper(b, func(b *testing.B, sg *SubGraph) {
   113  		var l Latency
   114  		b.ResetTimer()
   115  		for i := 0; i < b.N; i++ {
   116  			pb, err := sg.ToProtocolBuffer(&l)
   117  			if err != nil {
   118  				b.Fatal(err)
   119  			}
   120  			r := new(graph.Response)
   121  			r.N = pb
   122  			var c Codec
   123  			if _, err = c.Marshal(r); err != nil {
   124  				b.Fatal(err)
   125  			}
   126  		}
   127  	})
   128  }
   129  
   130  func valueSubGraph(attr string, uid []uint64, data []string) *SubGraph {
   131  	x.AssertTrue(len(uid) == len(data))
   132  	var r []*algo.UIDList
   133  	for i := 0; i < len(uid); i++ {
   134  		r = append(r, algo.NewUIDList([]uint64{}))
   135  	}
   136  	return &SubGraph{
   137  		Attr:    attr,
   138  		SrcUIDs: algo.NewUIDList(uid),
   139  		Values:  buildValueList(data),
   140  		Result:  r,
   141  	}
   142  }
   143  
   144  func uint64Range(a, b int) *algo.UIDList {
   145  	var out []uint64
   146  	for i := a; i < b; i++ {
   147  		out = append(out, uint64(i))
   148  	}
   149  	return algo.NewUIDList(out)
   150  }
   151  
   152  func leafSubGraphs(srcUID []uint64) []*SubGraph {
   153  	var children []*SubGraph
   154  	data := make([]string, 0, len(srcUID))
   155  	for i := 0; i < len(srcUID); i++ {
   156  		data = append(data, "somedata")
   157  	}
   158  	for i := 0; i < 50; i++ {
   159  		children = append(children, valueSubGraph(
   160  			fmt.Sprintf("attr%d", i), srcUID, data))
   161  	}
   162  	return children
   163  }
   164  
   165  const uidStart = 10000
   166  const uidEnd = 15000
   167  
   168  // sampleSubGraph creates a subgraph with given number of unique descendents.
   169  func sampleSubGraph(numUnique int) *SubGraph {
   170  	var r []*algo.UIDList
   171  	for i := uidStart; i < uidEnd; i++ {
   172  		r = append(r, algo.NewUIDList([]uint64{uint64(100000000 + (i % numUnique))}))
   173  	}
   174  
   175  	var destUID []uint64
   176  	for i := 0; i < numUnique; i++ {
   177  		destUID = append(destUID, uint64(100000000+i))
   178  	}
   179  
   180  	c := &SubGraph{
   181  		Attr:     "aaa",
   182  		SrcUIDs:  uint64Range(uidStart, uidEnd),
   183  		DestUIDs: algo.NewUIDList(destUID),
   184  		Values:   createNilValuesList(len(r)),
   185  		Result:   r,
   186  		Children: leafSubGraphs(destUID),
   187  	}
   188  
   189  	d := &SubGraph{
   190  		Attr:     "bbb",
   191  		SrcUIDs:  algo.NewUIDList([]uint64{1}),
   192  		DestUIDs: uint64Range(uidStart, uidEnd),
   193  		Result:   []*algo.UIDList{uint64Range(uidStart, uidEnd)},
   194  		Values:   createNilValuesList(1),
   195  		Children: []*SubGraph{c},
   196  	}
   197  
   198  	return &SubGraph{
   199  		Attr:     "ignore",
   200  		SrcUIDs:  algo.NewUIDList([]uint64{1}),
   201  		DestUIDs: algo.NewUIDList([]uint64{1}),
   202  		Values:   createNilValuesList(1),
   203  		Result:   []*algo.UIDList{algo.NewUIDList([]uint64{1})},
   204  		Children: []*SubGraph{d},
   205  	}
   206  }
   207  
   208  func BenchmarkToProtoSynthetic(b *testing.B) {
   209  	for _, numUnique := range []int{1, 1000, 2000, 3000, 4000, 5000} {
   210  		b.Run(fmt.Sprintf("unique%d", numUnique), func(b *testing.B) {
   211  			b.ReportAllocs()
   212  			sg := sampleSubGraph(numUnique)
   213  			var l Latency
   214  			b.ResetTimer()
   215  			for i := 0; i < b.N; i++ {
   216  				pb, err := sg.ToProtocolBuffer(&l)
   217  				if err != nil {
   218  					b.Fatal(err)
   219  				}
   220  				r := new(graph.Response)
   221  				r.N = pb
   222  				var c Codec
   223  				if _, err = c.Marshal(r); err != nil {
   224  					b.Fatal(err)
   225  				}
   226  			}
   227  		})
   228  	}
   229  }
   230  
   231  func BenchmarkToJSONSynthetic(b *testing.B) {
   232  	for _, numUnique := range []int{1, 1000, 2000, 3000, 4000, 5000} {
   233  		b.Run(fmt.Sprintf("unique%d", numUnique), func(b *testing.B) {
   234  			b.ReportAllocs()
   235  			sg := sampleSubGraph(numUnique)
   236  			var l Latency
   237  			b.ResetTimer()
   238  			for i := 0; i < b.N; i++ {
   239  				if _, err := sg.ToJSON(&l); err != nil {
   240  					b.Fatal(err)
   241  				}
   242  			}
   243  		})
   244  	}
   245  }
   246  */