github.com/rohankumardubey/aresdb@v0.0.2-0.20190517170215-e54e3ca06b9c/query/stats_test.go (about)

     1  //  Copyright (c) 2017-2018 Uber Technologies, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     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  package query
    16  
    17  import (
    18  	"github.com/onsi/ginkgo"
    19  	. "github.com/onsi/gomega"
    20  	"github.com/uber/aresdb/utils"
    21  	"sort"
    22  	"time"
    23  	"unsafe"
    24  )
    25  
    26  var _ = ginkgo.Describe("Stats", func() {
    27  	ginkgo.AfterEach(func() {
    28  		utils.ResetClockImplementation()
    29  	})
    30  
    31  	ginkgo.It("Sort should work for stageSummaryStatsSlice", func() {
    32  		stageStats := stageSummaryStatsSlice{
    33  			&oopkStageSummaryStats{
    34  				name:  "a",
    35  				total: 1,
    36  			},
    37  			&oopkStageSummaryStats{
    38  				name:  "b",
    39  				total: 2,
    40  			},
    41  			&oopkStageSummaryStats{
    42  				name:  "c",
    43  				total: 3,
    44  			},
    45  		}
    46  		Ω(stageStats.Len()).Should(Equal(3))
    47  		Ω(stageStats.Less(0, 1)).Should(BeTrue())
    48  		Ω(stageStats.Less(1, 0)).Should(BeFalse())
    49  		stageStats.Swap(0, 1)
    50  		Ω(stageStats[0].name).Should(BeEquivalentTo("b"))
    51  		Ω(stageStats[1].name).Should(BeEquivalentTo("a"))
    52  		sort.Sort(sort.Reverse(stageStats))
    53  		Ω(stageStats[0].name).Should(BeEquivalentTo("c"))
    54  		Ω(stageStats[1].name).Should(BeEquivalentTo("b"))
    55  		Ω(stageStats[2].name).Should(BeEquivalentTo("a"))
    56  	})
    57  
    58  	ginkgo.It("DataSource implementations should work for oopkQueryStats", func() {
    59  		queryStats := oopkQueryStats{
    60  			stageStats: []*oopkStageSummaryStats{
    61  				{name: "a"},
    62  				{avg: 1, max: 1, min: 1, total: 2, percentage: 1.0, count: 2, name: "b"},
    63  			},
    64  		}
    65  
    66  		Ω(queryStats.NumRows()).Should(Equal(2))
    67  		Ω(len(queryStats.ColumnHeaders())).Should(Equal(7))
    68  		Ω(queryStats.GetValue(1, 0)).Should(BeEquivalentTo("b"))
    69  		Ω(queryStats.GetValue(1, 1)).Should(Equal(1.0))
    70  		Ω(queryStats.GetValue(1, 2)).Should(Equal(1.0))
    71  		Ω(queryStats.GetValue(1, 3)).Should(Equal(1.0))
    72  		Ω(queryStats.GetValue(1, 4)).Should(Equal(2))
    73  		Ω(queryStats.GetValue(1, 5)).Should(Equal(2.0))
    74  		Ω(queryStats.GetValue(1, 6)).Should(Equal("100.00%"))
    75  	})
    76  
    77  	ginkgo.It("applyBatchStats should work", func() {
    78  		queryStats := oopkQueryStats{
    79  			stageStats: []*oopkStageSummaryStats{
    80  				{name: "a"},
    81  				{avg: 1, max: 1, min: 1, total: 2, percentage: 1.0, count: 2, name: "b"},
    82  			},
    83  		}
    84  
    85  		queryStats.applyBatchStats(oopkBatchStats{
    86  			batchSize:        10,
    87  			bytesTransferred: 200,
    88  			numTransferCalls: 10,
    89  		})
    90  
    91  		Ω(queryStats).Should(Equal(oopkQueryStats{
    92  			stageStats: []*oopkStageSummaryStats{
    93  				{name: "a", max: 0, min: 0, avg: 0, count: 0, total: 0, percentage: 0},
    94  				{name: "b", max: 1, min: 1, avg: 1, count: 2, total: 2, percentage: 1},
    95  			},
    96  			Name2Stage:       nil,
    97  			TotalTiming:      0,
    98  			NumBatches:       1,
    99  			NumRecords:       10,
   100  			BytesTransferred: 200,
   101  			NumTransferCalls: 10,
   102  		}))
   103  		queryStats.writeToLog()
   104  	})
   105  
   106  	ginkgo.It("reportTimingForCurrentBatch and reportTiming should work", func() {
   107  		qc := AQLQueryContext{Debug: true}
   108  		qc.OOPK.LiveBatchStats = oopkQueryStats{
   109  			Name2Stage: make(map[stageName]*oopkStageSummaryStats),
   110  		}
   111  		qc.OOPK.currentBatch.stats = oopkBatchStats{
   112  			timings: make(map[stageName]float64),
   113  		}
   114  
   115  		var stream unsafe.Pointer
   116  		utils.SetClockImplementation(func() time.Time {
   117  			return time.Unix(0, 0)
   118  		})
   119  		start := utils.Now()
   120  		utils.SetClockImplementation(func() time.Time {
   121  			return time.Unix(1, 0)
   122  		})
   123  		qc.reportTimingForCurrentBatch(stream, &start, dimEvalTiming)
   124  		utils.ResetClockImplementation()
   125  		Ω(qc.OOPK.currentBatch.stats.timings[dimEvalTiming]).Should(Equal(1000.0))
   126  
   127  		utils.SetClockImplementation(func() time.Time {
   128  			return time.Unix(2, 0)
   129  		})
   130  		qc.reportTiming(stream, &start, prepareForeignTableTiming)
   131  		Ω(*qc.OOPK.LiveBatchStats.Name2Stage[prepareForeignTableTiming]).Should(Equal(oopkStageSummaryStats{
   132  			name:  prepareForeignTableTiming,
   133  			max:   1000.0,
   134  			min:   1000.0,
   135  			count: 1,
   136  			total: 1000.0,
   137  		}))
   138  	})
   139  
   140  	ginkgo.It("reportBatch should work", func() {
   141  		qc := AQLQueryContext{Debug: true}
   142  		qc.OOPK.LiveBatchStats = oopkQueryStats{
   143  			Name2Stage: make(map[stageName]*oopkStageSummaryStats),
   144  		}
   145  
   146  		qc.OOPK.ArchiveBatchStats = oopkQueryStats{
   147  			Name2Stage: make(map[stageName]*oopkStageSummaryStats),
   148  		}
   149  
   150  		qc.OOPK.currentBatch.stats = oopkBatchStats{
   151  			timings: map[stageName]float64{
   152  				filterEvalTiming: 100,
   153  			},
   154  		}
   155  
   156  		qc.reportBatch(true)
   157  		Ω(qc.OOPK.ArchiveBatchStats).Should(Equal(oopkQueryStats{
   158  			stageStats: nil,
   159  			Name2Stage: map[stageName]*oopkStageSummaryStats{
   160  				"filterEval": {name: "filterEval", max: 100, min: 100, avg: 0, count: 1, total: 100, percentage: 0},
   161  			},
   162  			TotalTiming:      100,
   163  			NumBatches:       1,
   164  			NumRecords:       0,
   165  			BytesTransferred: 0,
   166  			NumTransferCalls: 0,
   167  		}))
   168  
   169  		qc.reportBatch(false)
   170  		Ω(qc.OOPK.LiveBatchStats).Should(Equal(oopkQueryStats{
   171  			stageStats: nil,
   172  			Name2Stage: map[stageName]*oopkStageSummaryStats{
   173  				"filterEval": {name: "filterEval", max: 100, min: 100, avg: 0, count: 1, total: 100, percentage: 0},
   174  			},
   175  			TotalTiming:      100,
   176  			NumBatches:       1,
   177  			NumRecords:       0,
   178  			BytesTransferred: 0,
   179  			NumTransferCalls: 0,
   180  		}))
   181  	})
   182  })