本文整理汇总了Golang中github.com/bradfitz/iter.N函数的典型用法代码示例。如果您正苦于以下问题:Golang N函数的具体用法?Golang N怎么用?Golang N使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了N函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Build
// Fetch Txos and Txins
func (tx *Tx) Build(rpool *redis.Pool) (err error) {
c := rpool.Get()
defer c.Close()
tx.TxIns = []*TxIn{}
tx.TxOuts = []*TxOut{}
txinskeys := []interface{}{}
for i := range iter.N(int(tx.TxInCnt)) {
txinskeys = append(txinskeys, fmt.Sprintf("txi:%v:%v", tx.Hash, i))
}
txinsjson, _ := redis.Strings(c.Do("MGET", txinskeys...))
for _, txinjson := range txinsjson {
ctxi := new(TxIn)
err = json.Unmarshal([]byte(txinjson), ctxi)
tx.TxIns = append(tx.TxIns, ctxi)
}
txoutskeys := []interface{}{}
txoutsspentkeys := []interface{}{}
for i := range iter.N(int(tx.TxOutCnt)) {
txoutskeys = append(txoutskeys, fmt.Sprintf("txo:%v:%v", tx.Hash, i))
txoutsspentkeys = append(txoutsspentkeys, fmt.Sprintf("txo:%v:%v:spent", tx.Hash, i))
}
txoutsjson, _ := redis.Strings(c.Do("MGET", txoutskeys...))
txoutsspentjson, _ := redis.Strings(c.Do("MGET", txoutsspentkeys...))
for txoindex, txoutjson := range txoutsjson {
ctxo := new(TxOut)
err = json.Unmarshal([]byte(txoutjson), ctxo)
if txoutsspentjson[txoindex] != "" {
cspent := new(TxoSpent)
err = json.Unmarshal([]byte(txoutsspentjson[txoindex]), cspent)
ctxo.Spent = cspent
}
tx.TxOuts = append(tx.TxOuts, ctxo)
}
return
}
示例2: Benchmark
func Benchmark(b *testing.B) {
db, _ := sql.Open("sqlrpc", serverAddr)
defer db.Close()
db.Exec("drop table if exists a")
db.Exec("create table a(b)")
for range iter.N(b.N) {
for i := range iter.N(10) {
db.Exec("insert into a values (?)", i)
}
rows, _ := db.Query("select * from a where b < ?", 3)
var count int
for rows.Next() {
var b int
rows.Scan(&b)
if b < 3 {
count++
}
}
assert.Nil(b, rows.Err())
assert.EqualValues(b, 3, count)
rows.Close()
db.Exec("delete from a")
}
assert.Equal(b, 0, len(server.refs))
}
示例3: TestRejectDialBacklogFilled
func TestRejectDialBacklogFilled(t *testing.T) {
s, err := NewSocket("udp", "localhost:0")
if err != nil {
t.Fatal(err)
}
errChan := make(chan error)
dial := func() {
_, err := s.Dial(s.Addr().String())
require.Error(t, err)
errChan <- err
}
// Fill the backlog.
for range iter.N(backlog) {
go dial()
}
sleepWhile(&s.mu, func() bool { return len(s.backlog) < backlog })
select {
case err := <-errChan:
t.Fatalf("got premature error: %s", err)
default:
}
// One more connection should cause a dial attempt to get reset.
go dial()
err = <-errChan
assert.EqualError(t, err, "peer reset")
s.Close()
for range iter.N(backlog) {
<-errChan
}
}
示例4: Add
// Add changes the number of workers
func (p *Pool) Add(n int) error {
if n >= 1 {
for range iter.N(n) {
stopC := make(chan struct{})
p.wg.Add(1)
go NewWorker(p.read, p.write, p.fn, stopC).Work(p.wg)
p.Lock()
p.workers = append(p.workers, stopC)
p.Unlock()
}
} else if n <= -1 {
for range iter.N(-1 * n) {
p.Lock()
// close channel to stop worker
close(p.workers[len(p.workers)-1])
// from github.com/golang/go/wiki/SliceTricks
// Delete without preserving order
p.workers[len(p.workers)-1] = nil
p.workers = p.workers[:len(p.workers)-1]
// unlock
p.Unlock()
}
} else {
return fmt.Errorf("%d is not a valid number of workers to add", n)
}
return nil
}
示例5: connectSelfLots
func connectSelfLots(n int, t testing.TB) {
s, err := NewSocket("127.0.0.1:0")
if err != nil {
t.Fatal(err)
}
go func() {
for range iter.N(n) {
c, err := s.Accept()
if err != nil {
log.Fatal(err)
}
defer c.Close()
}
}()
dialErr := make(chan error)
connCh := make(chan net.Conn)
for range iter.N(n) {
go func() {
c, err := s.Dial(s.Addr().String())
if err != nil {
dialErr <- err
return
}
connCh <- c
}()
}
conns := make([]net.Conn, 0, n)
for range iter.N(n) {
select {
case c := <-connCh:
conns = append(conns, c)
case err := <-dialErr:
t.Fatal(err)
}
if testing.Verbose() {
log.Printf("%x", len(conns))
}
}
for _, c := range conns {
if c != nil {
c.Close()
}
}
s.mu.Lock()
for len(s.conns) != 0 {
if testing.Verbose() {
log.Printf("socket conns: %d", len(s.conns))
if len(s.conns) < 10 {
for _, c := range s.conns {
log.Printf("%#v", c)
}
}
}
s.event.Wait()
}
s.mu.Unlock()
s.Close()
}
示例6: main
func main() {
fmt.Println(unsafe.Sizeof(iter.N(10))) // prints 12 on the playground.
// Print 0 - 9, inclusive, without causing any allocations.
for i := range iter.N(10) {
fmt.Println(i)
}
}
示例7: calc
// calc evaluates the function for each cell.
func calc(c *Field, andMask uint32, f ceval) *Field {
out := NewField(c.Width, c.Height)
for y := range iter.N(c.Height) {
for x := range iter.N(c.Width) {
out.State[c.Width*y+x] = f(c, x, y, andMask)
}
}
return out
}
示例8: BenchmarkIteration
func BenchmarkIteration(b *testing.B) {
for range iter.N(b.N) {
i := New()
for p := range iter.N(500) {
i.SetPiece(p, p)
}
for e := i.First(); e != nil; e = e.Next() {
}
}
}
示例9: BenchmarkInsert
func BenchmarkInsert(b *testing.B) {
for range iter.N(b.N) {
li := newLRUItems()
for range iter.N(10000) {
r := rand.Int63()
t := time.Unix(r/1e9, r%1e9)
li.Insert(ItemInfo{
Accessed: t,
})
}
}
}
示例10: connectSelfLots
func connectSelfLots(n int, t testing.TB) {
defer goroutineLeakCheck(t)()
s, err := NewSocket("udp", "localhost:0")
if err != nil {
t.Fatal(err)
}
go func() {
for range iter.N(n) {
c, err := s.Accept()
if err != nil {
log.Fatal(err)
}
defer c.Close()
}
}()
dialErr := make(chan error)
connCh := make(chan net.Conn)
dialSema := make(chan struct{}, backlog)
for range iter.N(n) {
go func() {
dialSema <- struct{}{}
c, err := s.Dial(s.Addr().String())
<-dialSema
if err != nil {
dialErr <- err
return
}
connCh <- c
}()
}
conns := make([]net.Conn, 0, n)
for range iter.N(n) {
select {
case c := <-connCh:
conns = append(conns, c)
case err := <-dialErr:
t.Fatal(err)
}
}
for _, c := range conns {
if c != nil {
c.Close()
}
}
s.mu.Lock()
for len(s.conns) != 0 {
// log.Print(len(s.conns))
s.event.Wait()
}
s.mu.Unlock()
s.Close()
}
示例11: nextState
func nextState(c, is2, is3 *Field) *Field {
out := NewField(c.Width, c.Height)
for y := range iter.N(c.Height) {
for x := range iter.N(c.Width) {
i := y*c.Width + x
// Live cells with 2 or 3 neighbors live.
out.State[i] = ((is2.State[i] | is3.State[i]) & c.State[i]) |
// Dead cells with exactly 3 neighbors live.
(is3.State[i] &^ c.State[i])
}
}
return out
}
示例12: NewWorkspace
func NewWorkspace(shouldShow RepoFilter, presenter RepoPresenter) *workspace {
w := &workspace{
ImportPaths: make(chan string, 64),
unique: make(chan *Repo, 64),
processedFiltered: make(chan *Repo, 64),
Statuses: make(chan string, 64),
Errors: make(chan error, 64),
shouldShow: shouldShow,
presenter: presenter,
repos: make(map[string]*Repo),
}
{
var wg sync.WaitGroup
for range iter.N(parallelism) {
wg.Add(1)
go w.uniqueWorker(&wg)
}
go func() {
wg.Wait()
close(w.unique)
}()
}
{
var wg sync.WaitGroup
for range iter.N(parallelism) {
wg.Add(1)
go w.processFilterWorker(&wg)
}
go func() {
wg.Wait()
close(w.processedFiltered)
}()
}
{
var wg sync.WaitGroup
for range iter.N(parallelism) {
wg.Add(1)
go w.presenterWorker(&wg)
}
go func() {
wg.Wait()
close(w.Statuses)
close(w.Errors)
}()
}
return w
}
示例13: main
func main() {
tagflag.Parse(&flags)
for _, filename := range flags.TorrentFiles {
metainfo, err := metainfo.LoadFromFile(filename)
if err != nil {
log.Print(err)
continue
}
info := &metainfo.Info.Info
if flags.JustName {
fmt.Printf("%s\n", metainfo.Info.Name)
continue
}
d := map[string]interface{}{
"Name": info.Name,
"NumPieces": info.NumPieces(),
"PieceLength": info.PieceLength,
}
if flags.PieceHashes {
d["PieceHashes"] = func() (ret []string) {
for i := range iter.N(info.NumPieces()) {
ret = append(ret, hex.EncodeToString(info.Pieces[i*20:(i+1)*20]))
}
return
}()
}
b, _ := json.MarshalIndent(d, "", " ")
os.Stdout.Write(b)
}
if !flags.JustName {
os.Stdout.WriteString("\n")
}
}
示例14: TestPieceRequestOrder
// Tests the request ordering based on a connections priorities.
func TestPieceRequestOrder(t *testing.T) {
c := connection{
pieceRequestOrder: pieceordering.New(),
piecePriorities: []int{1, 4, 0, 3, 2},
}
testRequestOrder(nil, c.pieceRequestOrder, t)
c.pendPiece(2, PiecePriorityNone, nil)
testRequestOrder(nil, c.pieceRequestOrder, t)
c.pendPiece(1, PiecePriorityNormal, nil)
c.pendPiece(2, PiecePriorityNormal, nil)
testRequestOrder([]int{2, 1}, c.pieceRequestOrder, t)
c.pendPiece(0, PiecePriorityNormal, nil)
testRequestOrder([]int{2, 0, 1}, c.pieceRequestOrder, t)
c.pendPiece(1, PiecePriorityReadahead, nil)
testRequestOrder([]int{1, 2, 0}, c.pieceRequestOrder, t)
c.pendPiece(4, PiecePriorityNow, nil)
// now(4), r(1), normal(0, 2)
testRequestOrder([]int{4, 1, 2, 0}, c.pieceRequestOrder, t)
c.pendPiece(2, PiecePriorityReadahead, nil)
// N(4), R(1, 2), N(0)
testRequestOrder([]int{4, 2, 1, 0}, c.pieceRequestOrder, t)
c.pendPiece(1, PiecePriorityNow, nil)
// now(4, 1), readahead(2), normal(0)
// in the same order, the keys will be: -15+6, -15+12, -5, 1
// so we test that a very low priority (for this connection), "now"
// piece has been placed after a readahead piece.
testRequestOrder([]int{4, 2, 1, 0}, c.pieceRequestOrder, t)
// Note this intentially sets to None a piece that's not in the order.
for i := range iter.N(5) {
c.pendPiece(i, PiecePriorityNone, nil)
}
testRequestOrder(nil, c.pieceRequestOrder, t)
}
示例15: CreateRandomTree
func CreateRandomTree(t *testing.T, path string, rec, maxrec int) (string, int) {
p := NewRandomDir(path)
if rec == 0 {
t.Logf("Creating a new random tree at %v", p)
}
//NewRandomDir(p)
//NewEmptyFile(p)
nfiles := 0
for {
nfiles = mrand.Intn(10)
if nfiles >= 3 {
break
}
}
cnt := 0
var wg sync.WaitGroup
for _ = range iter.N(nfiles) {
wg.Add(1)
go NewRandomFileWg(p, &wg)
cnt++
if rec < maxrec && mrand.Intn(10) < 5 {
_, ncnt := CreateRandomTree(t, p, rec+1, maxrec)
cnt += ncnt
}
// Break at 30 to spend less time
if cnt > 30 {
return p, cnt
}
}
wg.Wait()
if rec == 0 {
t.Log("Random tree created")
}
return p, cnt
}