forked from hamano/lb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.go
99 lines (92 loc) · 1.77 KB
/
search.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"fmt"
"log"
"reflect"
"strings"
"math/rand"
"github.com/urfave/cli"
openldap "github.com/hamano/golang-openldap"
)
type SearchJob struct {
BaseJob
baseDN string
scope int
filter string
first int
last int
idRange int
}
var searchFlags = []cli.Flag {
cli.StringFlag {
Name: "s",
Value: "sub",
Usage: "scope",
},
cli.StringFlag {
Name: "a, filter",
Value: "(objectClass=*)",
Usage: "filter",
},
cli.IntFlag {
Name: "first",
Value: 1,
Usage: "first id",
},
cli.IntFlag {
Name: "last",
Value: 0,
Usage: "last id",
},
}
func Search(c *cli.Context) error {
runBenchmark(c, reflect.TypeOf(SearchJob{}))
return nil
}
func (job *SearchJob) Prep(c *cli.Context) bool {
if job.GetVerbose() >= 1 {
log.Printf("worker[%d]: prepare\n", job.wid)
}
err := job.ldap.Bind(c.String("D"), c.String("w"))
if err != nil {
log.Fatal("bind error: ", err)
return false
}
job.baseDN = c.String("b")
job.filter = c.String("a")
job.first = c.Int("first")
job.last = c.Int("last")
switch c.String("s") {
case "base":
job.scope = openldap.LDAP_SCOPE_BASE
case "one":
job.scope = openldap.LDAP_SCOPE_ONE
case "sub":
job.scope = openldap.LDAP_SCOPE_SUBTREE
case "children":
job.scope = openldap.LDAP_SCOPE_CHILDREN
default:
job.scope = openldap.LDAP_SCOPE_SUBTREE
}
if strings.Contains(job.filter, "%") {
job.idRange = job.last - job.first + 1
}
return true
}
func (job *SearchJob) Request() bool {
var filter string
if job.idRange > 0 {
id := rand.Intn(job.idRange) + job.first
filter = fmt.Sprintf(job.filter, id)
} else {
filter = job.filter
}
res, err := job.ldap.SearchAll(job.baseDN, job.scope, filter, []string{"dn"})
if err != nil {
return false
}
if res.Count() == 0 {
return false
}
return true
}