~ubuntu-branches/debian/sid/golang-github-blevesearch-bleve/sid

« back to all changes in this revision

Viewing changes to index/upsidedown/benchmark_common_test.go

  • Committer: Package Import Robot
  • Author(s): Michael Lustfield
  • Date: 2017-03-30 16:06:03 UTC
  • Revision ID: package-import@ubuntu.com-20170330160603-0oogmb960l7918jx
Tags: upstream-0.5.0+git20170324.202.4702785f
Import upstream version 0.5.0+git20170324.202.4702785f

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  Copyright (c) 2014 Couchbase, 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 upsidedown
 
16
 
 
17
import (
 
18
        "os"
 
19
        "strconv"
 
20
        "testing"
 
21
 
 
22
        _ "github.com/blevesearch/bleve/analysis/analyzer/standard"
 
23
        "github.com/blevesearch/bleve/document"
 
24
        "github.com/blevesearch/bleve/index"
 
25
        "github.com/blevesearch/bleve/registry"
 
26
)
 
27
 
 
28
var benchmarkDocBodies = []string{
 
29
        "A boiling liquid expanding vapor explosion (BLEVE, /ˈblɛviː/ blev-ee) is an explosion caused by the rupture of a vessel containing a pressurized liquid above its boiling point.",
 
30
        "A boiler explosion is a catastrophic failure of a boiler. As seen today, boiler explosions are of two kinds. One kind is a failure of the pressure parts of the steam and water sides. There can be many different causes, such as failure of the safety valve, corrosion of critical parts of the boiler, or low water level. Corrosion along the edges of lap joints was a common cause of early boiler explosions.",
 
31
        "A boiler is a closed vessel in which water or other fluid is heated. The fluid does not necessarily boil. (In North America the term \"furnace\" is normally used if the purpose is not actually to boil the fluid.) The heated or vaporized fluid exits the boiler for use in various processes or heating applications,[1][2] including central heating, boiler-based power generation, cooking, and sanitation.",
 
32
        "A pressure vessel is a closed container designed to hold gases or liquids at a pressure substantially different from the ambient pressure.",
 
33
        "Pressure (symbol: p or P) is the ratio of force to the area over which that force is distributed.",
 
34
        "Liquid is one of the four fundamental states of matter (the others being solid, gas, and plasma), and is the only state with a definite volume but no fixed shape.",
 
35
        "The boiling point of a substance is the temperature at which the vapor pressure of the liquid equals the pressure surrounding the liquid[1][2] and the liquid changes into a vapor.",
 
36
        "Vapor pressure or equilibrium vapor pressure is defined as the pressure exerted by a vapor in thermodynamic equilibrium with its condensed phases (solid or liquid) at a given temperature in a closed system.",
 
37
        "Industrial gases are a group of gases that are specifically manufactured for use in a wide range of industries, which include oil and gas, petrochemicals, chemicals, power, mining, steelmaking, metals, environmental protection, medicine, pharmaceuticals, biotechnology, food, water, fertilizers, nuclear power, electronics and aerospace.",
 
38
        "The expansion ratio of a liquefied and cryogenic substance is the volume of a given amount of that substance in liquid form compared to the volume of the same amount of substance in gaseous form, at room temperature and normal atmospheric pressure.",
 
39
}
 
40
 
 
41
type KVStoreDestroy func() error
 
42
 
 
43
func DestroyTest() error {
 
44
        return os.RemoveAll("test")
 
45
}
 
46
 
 
47
func CommonBenchmarkIndex(b *testing.B, storeName string, storeConfig map[string]interface{}, destroy KVStoreDestroy, analysisWorkers int) {
 
48
 
 
49
        cache := registry.NewCache()
 
50
        analyzer, err := cache.AnalyzerNamed("standard")
 
51
        if err != nil {
 
52
                b.Fatal(err)
 
53
        }
 
54
 
 
55
        indexDocument := document.NewDocument("").
 
56
                AddField(document.NewTextFieldWithAnalyzer("body", []uint64{}, []byte(benchmarkDocBodies[0]), analyzer))
 
57
 
 
58
        b.ResetTimer()
 
59
        b.StopTimer()
 
60
        for i := 0; i < b.N; i++ {
 
61
                analysisQueue := index.NewAnalysisQueue(analysisWorkers)
 
62
                idx, err := NewUpsideDownCouch(storeName, storeConfig, analysisQueue)
 
63
                if err != nil {
 
64
                        b.Fatal(err)
 
65
                }
 
66
 
 
67
                err = idx.Open()
 
68
                if err != nil {
 
69
                        b.Fatal(err)
 
70
                }
 
71
                indexDocument.ID = strconv.Itoa(i)
 
72
                // just time the indexing portion
 
73
                b.StartTimer()
 
74
                err = idx.Update(indexDocument)
 
75
                if err != nil {
 
76
                        b.Fatal(err)
 
77
                }
 
78
                b.StopTimer()
 
79
                err = idx.Close()
 
80
                if err != nil {
 
81
                        b.Fatal(err)
 
82
                }
 
83
                err = destroy()
 
84
                if err != nil {
 
85
                        b.Fatal(err)
 
86
                }
 
87
                analysisQueue.Close()
 
88
        }
 
89
}
 
90
 
 
91
func CommonBenchmarkIndexBatch(b *testing.B, storeName string, storeConfig map[string]interface{}, destroy KVStoreDestroy, analysisWorkers, batchSize int) {
 
92
 
 
93
        cache := registry.NewCache()
 
94
        analyzer, err := cache.AnalyzerNamed("standard")
 
95
        if err != nil {
 
96
                b.Fatal(err)
 
97
        }
 
98
 
 
99
        b.ResetTimer()
 
100
        b.StopTimer()
 
101
        for i := 0; i < b.N; i++ {
 
102
 
 
103
                analysisQueue := index.NewAnalysisQueue(analysisWorkers)
 
104
                idx, err := NewUpsideDownCouch(storeName, storeConfig, analysisQueue)
 
105
                if err != nil {
 
106
                        b.Fatal(err)
 
107
                }
 
108
 
 
109
                err = idx.Open()
 
110
                if err != nil {
 
111
                        b.Fatal(err)
 
112
                }
 
113
 
 
114
                b.StartTimer()
 
115
                batch := index.NewBatch()
 
116
                for j := 0; j < 1000; j++ {
 
117
                        if j%batchSize == 0 {
 
118
                                if len(batch.IndexOps) > 0 {
 
119
                                        err := idx.Batch(batch)
 
120
                                        if err != nil {
 
121
                                                b.Fatal(err)
 
122
                                        }
 
123
                                }
 
124
                                batch = index.NewBatch()
 
125
                        }
 
126
                        indexDocument := document.NewDocument("").
 
127
                                AddField(document.NewTextFieldWithAnalyzer("body", []uint64{}, []byte(benchmarkDocBodies[j%10]), analyzer))
 
128
                        indexDocument.ID = strconv.Itoa(i) + "-" + strconv.Itoa(j)
 
129
                        batch.Update(indexDocument)
 
130
                }
 
131
                // close last batch
 
132
                if len(batch.IndexOps) > 0 {
 
133
                        err := idx.Batch(batch)
 
134
                        if err != nil {
 
135
                                b.Fatal(err)
 
136
                        }
 
137
                }
 
138
                b.StopTimer()
 
139
                err = idx.Close()
 
140
                if err != nil {
 
141
                        b.Fatal(err)
 
142
                }
 
143
                err = destroy()
 
144
                if err != nil {
 
145
                        b.Fatal(err)
 
146
                }
 
147
                analysisQueue.Close()
 
148
        }
 
149
}