<commit_before>package eth

import (
	"math/big"

	"github.com/ethereum/go-ethereum/accounts"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/ethclient"
	lpTypes "github.com/livepeer/go-livepeer/eth/types"
)

type StubClient struct {
	StrmID            string
	TOpts             string
	MaxPrice          *big.Int
	Jid               *big.Int
	SegSeqNum         *big.Int
	VeriRate          uint64
	DStorageHash      string
	DHash             [32]byte
	TDHash            [32]byte
	BSig              []byte
	Proof             []byte
	VerifyCounter     int
	ClaimJid          []*big.Int
	ClaimStart        []*big.Int
	ClaimEnd          []*big.Int
	ClaimRoot         map[[32]byte]bool
	ClaimCounter      int
	SubLogsCh         chan types.Log
	JobsMap           map[string]*lpTypes.Job
	BlockNum          *big.Int
	BlockHashToReturn common.Hash
}

func (e *StubClient) Setup(password string, gasLimit, gasPrice *big.Int) error { return nil }
func (e *StubClient) Account() accounts.Account                                { return accounts.Account{} }
func (e *StubClient) Backend() (*ethclient.Client, error)                      { return nil, ErrMissingBackend }

// Rounds

func (e *StubClient) InitializeRound() (*types.Transaction, error) { return nil, nil }
func (e *StubClient) CurrentRound() (*big.Int, error)              { return big.NewInt(0), nil }
func (e *StubClient) LastInitializedRound() (*big.Int, error)      { return big.NewInt(0), nil }
func (e *StubClient) CurrentRoundInitialized() (bool, error)       { return false, nil }
func (e *StubClient) CurrentRoundLocked() (bool, error)            { return false, nil }

// Token

func (e *StubClient) Transfer(toAddr common.Address, amount *big.Int) (*types.Transaction, error) {
	return nil, nil
}
func (e *StubClient) Request() (*types.Transaction, error)            { return nil, nil }
func (e *StubClient) BalanceOf(addr common.Address) (*big.Int, error) { return big.NewInt(0), nil }
func (e *StubClient) TotalSupply() (*big.Int, error)                  { return big.NewInt(0), nil }

// Staking

func (e *StubClient) Transcoder(blockRewardCut *big.Int, feeShare *big.Int, pricePerSegment *big.Int) (*types.Transaction, error) {
	return nil, nil
}
func (e *StubClient) Reward() (*types.Transaction, error) { return nil, nil }
func (e *StubClient) Bond(amount *big.Int, toAddr common.Address) (*types.Transaction, error) {
	return nil, nil
}
func (e *StubClient) Unbond() (*types.Transaction, error)        { return nil, nil }
func (e *StubClient) WithdrawStake() (*types.Transaction, error) { return nil, nil }
func (e *StubClient) WithdrawFees() (*types.Transaction, error)  { return nil, nil }
func (e *StubClient) ClaimEarnings(endRound *big.Int) error {
	return nil
}
func (e *StubClient) GetTranscoder(addr common.Address) (*lpTypes.Transcoder, error) { return nil, nil }
func (e *StubClient) GetDelegator(addr common.Address) (*lpTypes.Delegator, error)   { return nil, nil }
func (e *StubClient) GetTranscoderTokenPoolsForRound(addr common.Address, round *big.Int) (*lpTypes.TokenPools, error) {
	return nil, nil
}
func (e *StubClient) RegisteredTranscoders() ([]*lpTypes.Transcoder, error) { return nil, nil }
func (e *StubClient) IsActiveTranscoder() (bool, error)                     { return false, nil }
func (e *StubClient) IsAssignedTranscoder(jobID *big.Int) (bool, error)     { return false, nil }
func (e *StubClient) GetTotalBonded() (*big.Int, error)                     { return big.NewInt(0), nil }

// Jobs

func (e *StubClient) Job(streamId string, transcodingOptions string, maxPricePerSegment *big.Int, endBlock *big.Int) (*types.Transaction, error) {
	return nil, nil
}
func (e *StubClient) ClaimWork(jobId *big.Int, segmentRange [2]*big.Int, claimRoot [32]byte) (*types.Transaction, error) {
	e.ClaimCounter++
	e.ClaimJid = append(e.ClaimJid, jobId)
	e.ClaimStart = append(e.ClaimStart, segmentRange[0])
	e.ClaimEnd = append(e.ClaimEnd, segmentRange[1])
	e.ClaimRoot[claimRoot] = true
	return nil, nil
}
func (e *StubClient) Verify(jobId *big.Int, claimId *big.Int, segmentNumber *big.Int, dataStorageHash string, dataHashes [2][32]byte, broadcasterSig []byte, proof []byte) (*types.Transaction, error) {
	e.Jid = jobId
	e.SegSeqNum = segmentNumber
	e.DStorageHash = dataStorageHash
	e.DHash = dataHashes[0]
	e.TDHash = dataHashes[1]
	e.BSig = broadcasterSig
	e.Proof = proof
	e.VerifyCounter++

	return nil, nil
}
func (e *StubClient) DistributeFees(jobId *big.Int, claimId *big.Int) (*types.Transaction, error) {
	return nil, nil
}
func (c *StubClient) Deposit(amount *big.Int) (*types.Transaction, error) {
	return nil, nil
}
func (c *StubClient) Withdraw() (*types.Transaction, error) { return nil, nil }
func (c *StubClient) BroadcasterDeposit(broadcaster common.Address) (*big.Int, error) {
	return big.NewInt(0), nil
}
func (e *StubClient) GetJob(jobID *big.Int) (*lpTypes.Job, error) {
	return nil, nil
}
func (c *StubClient) GetClaim(jobID *big.Int, claimID *big.Int) (*lpTypes.Claim, error) {
	return nil, nil
}

// Parameters

func (c *StubClient) NumActiveTranscoders() (*big.Int, error)          { return big.NewInt(0), nil }
func (c *StubClient) RoundLength() (*big.Int, error)                   { return big.NewInt(0), nil }
func (c *StubClient) RoundLockAmount() (*big.Int, error)               { return big.NewInt(0), nil }
func (c *StubClient) UnbondingPeriod() (uint64, error)                 { return 0, nil }
func (c *StubClient) VerificationRate() (uint64, error)                { return 0, nil }
func (c *StubClient) VerificationPeriod() (*big.Int, error)            { return big.NewInt(0), nil }
func (c *StubClient) SlashingPeriod() (*big.Int, error)                { return big.NewInt(0), nil }
func (c *StubClient) FailedVerificationSlashAmount() (*big.Int, error) { return big.NewInt(0), nil }
func (c *StubClient) MissedVerificationSlashAmount() (*big.Int, error) { return big.NewInt(0), nil }
func (c *StubClient) DoubleClaimSegmentSlashAmount() (*big.Int, error) { return big.NewInt(0), nil }
func (c *StubClient) FinderFee() (*big.Int, error)                     { return big.NewInt(0), nil }
func (c *StubClient) Inflation() (*big.Int, error)                     { return big.NewInt(0), nil }
func (c *StubClient) InflationChange() (*big.Int, error)               { return big.NewInt(0), nil }
func (c *StubClient) TargetBondingRate() (*big.Int, error)             { return big.NewInt(0), nil }
func (c *StubClient) VerificationCodeHash() (string, error)            { return "", nil }

// Helpers

func (c *StubClient) ContractAddresses() map[string]common.Address { return nil }
func (c *StubClient) CheckTx(tx *types.Transaction) error          { return nil }
func (e *StubClient) Sign(msg []byte) ([]byte, error)              { return nil, nil }
<commit_msg>fix tests<commit_after>package eth

import (
	"math/big"

	"github.com/ethereum/go-ethereum/accounts"
	"github.com/ethereum/go-ethereum/common"
	"github.com/ethereum/go-ethereum/core/types"
	"github.com/ethereum/go-ethereum/ethclient"
	lpTypes "github.com/livepeer/go-livepeer/eth/types"
)

type StubClient struct {
	StrmID            string
	TOpts             string
	MaxPrice          *big.Int
	Jid               *big.Int
	SegSeqNum         *big.Int
	VeriRate          uint64
	DStorageHash      string
	DHash             [32]byte
	TDHash            [32]byte
	BSig              []byte
	Proof             []byte
	VerifyCounter     int
	ClaimJid          []*big.Int
	ClaimStart        []*big.Int
	ClaimEnd          []*big.Int
	ClaimRoot         map[[32]byte]bool
	ClaimCounter      int
	SubLogsCh         chan types.Log
	JobsMap           map[string]*lpTypes.Job
	BlockNum          *big.Int
	BlockHashToReturn common.Hash
}

func (e *StubClient) Setup(password string, gasLimit, gasPrice *big.Int) error { return nil }
func (e *StubClient) Account() accounts.Account                                { return accounts.Account{} }
func (e *StubClient) Backend() (*ethclient.Client, error)                      { return nil, ErrMissingBackend }

// Rounds

func (e *StubClient) InitializeRound() (*types.Transaction, error) { return nil, nil }
func (e *StubClient) CurrentRound() (*big.Int, error)              { return big.NewInt(0), nil }
func (e *StubClient) LastInitializedRound() (*big.Int, error)      { return big.NewInt(0), nil }
func (e *StubClient) CurrentRoundInitialized() (bool, error)       { return false, nil }
func (e *StubClient) CurrentRoundLocked() (bool, error)            { return false, nil }
func (e *StubClient) Paused() (bool, error)                        { return false, nil }

// Token

func (e *StubClient) Transfer(toAddr common.Address, amount *big.Int) (*types.Transaction, error) {
	return nil, nil
}
func (e *StubClient) Request() (*types.Transaction, error)            { return nil, nil }
func (e *StubClient) BalanceOf(addr common.Address) (*big.Int, error) { return big.NewInt(0), nil }
func (e *StubClient) TotalSupply() (*big.Int, error)                  { return big.NewInt(0), nil }

// Staking

func (e *StubClient) Transcoder(blockRewardCut *big.Int, feeShare *big.Int, pricePerSegment *big.Int) (*types.Transaction, error) {
	return nil, nil
}
func (e *StubClient) Reward() (*types.Transaction, error) { return nil, nil }
func (e *StubClient) Bond(amount *big.Int, toAddr common.Address) (*types.Transaction, error) {
	return nil, nil
}
func (e *StubClient) Unbond() (*types.Transaction, error)        { return nil, nil }
func (e *StubClient) WithdrawStake() (*types.Transaction, error) { return nil, nil }
func (e *StubClient) WithdrawFees() (*types.Transaction, error)  { return nil, nil }
func (e *StubClient) ClaimEarnings(endRound *big.Int) error {
	return nil
}
func (e *StubClient) GetTranscoder(addr common.Address) (*lpTypes.Transcoder, error) { return nil, nil }
func (e *StubClient) GetDelegator(addr common.Address) (*lpTypes.Delegator, error)   { return nil, nil }
func (e *StubClient) GetTranscoderEarningsPoolForRound(addr common.Address, round *big.Int) (*lpTypes.TokenPools, error) {
	return nil, nil
}
func (e *StubClient) RegisteredTranscoders() ([]*lpTypes.Transcoder, error) { return nil, nil }
func (e *StubClient) IsActiveTranscoder() (bool, error)                     { return false, nil }
func (e *StubClient) IsAssignedTranscoder(jobID *big.Int) (bool, error)     { return false, nil }
func (e *StubClient) GetTotalBonded() (*big.Int, error)                     { return big.NewInt(0), nil }

// Jobs

func (e *StubClient) Job(streamId string, transcodingOptions string, maxPricePerSegment *big.Int, endBlock *big.Int) (*types.Transaction, error) {
	return nil, nil
}
func (e *StubClient) ClaimWork(jobId *big.Int, segmentRange [2]*big.Int, claimRoot [32]byte) (*types.Transaction, error) {
	e.ClaimCounter++
	e.ClaimJid = append(e.ClaimJid, jobId)
	e.ClaimStart = append(e.ClaimStart, segmentRange[0])
	e.ClaimEnd = append(e.ClaimEnd, segmentRange[1])
	e.ClaimRoot[claimRoot] = true
	return nil, nil
}
func (e *StubClient) Verify(jobId *big.Int, claimId *big.Int, segmentNumber *big.Int, dataStorageHash string, dataHashes [2][32]byte, broadcasterSig []byte, proof []byte) (*types.Transaction, error) {
	e.Jid = jobId
	e.SegSeqNum = segmentNumber
	e.DStorageHash = dataStorageHash
	e.DHash = dataHashes[0]
	e.TDHash = dataHashes[1]
	e.BSig = broadcasterSig
	e.Proof = proof
	e.VerifyCounter++

	return nil, nil
}
func (e *StubClient) DistributeFees(jobId *big.Int, claimId *big.Int) (*types.Transaction, error) {
	return nil, nil
}
func (c *StubClient) Deposit(amount *big.Int) (*types.Transaction, error) {
	return nil, nil
}
func (c *StubClient) Withdraw() (*types.Transaction, error) { return nil, nil }
func (c *StubClient) BroadcasterDeposit(broadcaster common.Address) (*big.Int, error) {
	return big.NewInt(0), nil
}
func (e *StubClient) GetJob(jobID *big.Int) (*lpTypes.Job, error) {
	return nil, nil
}
func (c *StubClient) GetClaim(jobID *big.Int, claimID *big.Int) (*lpTypes.Claim, error) {
	return nil, nil
}

// Parameters

func (c *StubClient) NumActiveTranscoders() (*big.Int, error)          { return big.NewInt(0), nil }
func (c *StubClient) RoundLength() (*big.Int, error)                   { return big.NewInt(0), nil }
func (c *StubClient) RoundLockAmount() (*big.Int, error)               { return big.NewInt(0), nil }
func (c *StubClient) UnbondingPeriod() (uint64, error)                 { return 0, nil }
func (c *StubClient) VerificationRate() (uint64, error)                { return 0, nil }
func (c *StubClient) VerificationPeriod() (*big.Int, error)            { return big.NewInt(0), nil }
func (c *StubClient) VerificationSlashingPeriod() (*big.Int, error)    { return big.NewInt(0), nil }
func (c *StubClient) FailedVerificationSlashAmount() (*big.Int, error) { return big.NewInt(0), nil }
func (c *StubClient) MissedVerificationSlashAmount() (*big.Int, error) { return big.NewInt(0), nil }
func (c *StubClient) DoubleClaimSegmentSlashAmount() (*big.Int, error) { return big.NewInt(0), nil }
func (c *StubClient) FinderFee() (*big.Int, error)                     { return big.NewInt(0), nil }
func (c *StubClient) Inflation() (*big.Int, error)                     { return big.NewInt(0), nil }
func (c *StubClient) InflationChange() (*big.Int, error)               { return big.NewInt(0), nil }
func (c *StubClient) TargetBondingRate() (*big.Int, error)             { return big.NewInt(0), nil }
func (c *StubClient) VerificationCodeHash() (string, error)            { return "", nil }

// Helpers

func (c *StubClient) ContractAddresses() map[string]common.Address { return nil }
func (c *StubClient) CheckTx(tx *types.Transaction) error          { return nil }
func (e *StubClient) Sign(msg []byte) ([]byte, error)              { return nil, nil }
<|endoftext|>


<commit_before>package flipcounter

import (
	"encoding/binary"
	"math"
	"math/rand"

	metro "github.com/dgryski/go-metro"
)

const (
	exp            = 1.00002
	guaranteeLimit = 8388607
	countbits      = 23
	expBase        = 255999
)

func value(c uint32) float64 {
	if c <= 1 {
		return float64(c)
	}
	return (1 - math.Pow(exp, float64(c))) / (1 - exp)
}

type key [5]byte   // guarantees us 1,099,511,627,776 unique keys
type count [3]byte // 24 bits, where 23 are counter and one is estimation flag

func hashToBytes(hash uint64) key {
	bytes := make([]byte, 10)
	_ = binary.PutUvarint(bytes, hash)
	return key{bytes[0], bytes[1], bytes[2], bytes[3]}
}

func makeKey(value []byte) key {
	return hashToBytes(metro.Hash64(value, 1337))
}

func getCount(value count) (bool, uint32) {
	p := value[2]>>7 == 1
	value[2] = value[2] << 1 >> 1
	bytes := []byte{value[0], value[1], value[2], 0}
	val := binary.LittleEndian.Uint32(bytes)
	return p, val
}

func makeCount(p bool, c uint32) count {
	bytes := make([]byte, 4)
	binary.LittleEndian.PutUint32(bytes, c)
	if p {
		bytes[2] = bytes[2] | (1 << 7)
	}
	return count{bytes[0], bytes[1], bytes[2]}
}

// Counter consits of a 5 bytes key and a 3 bytes estimating counter with a 100% accurary up to 8388607 hits, then an estimation of 1% error
type Counter map[key]count

// New return a HashLog sketch with 5 bytes keys and 3 bytes counters
func New() *Counter {
	dict := make(map[key]count)
	counter := Counter(dict)
	return &counter
}

// Increment increments the counter of val []byte by +1
func (fc *Counter) Increment(val []byte) {
	k := makeKey(val)
	v := (*fc)[k]
	p, c := getCount(v)

	switch {
	case !p && c < guaranteeLimit: // good old +1 counting
		(*fc)[k] = makeCount(false, c+1)
	case !p && c == guaranteeLimit: // flip the bit, its estimation time
		(*fc)[k] = makeCount(true, expBase)
	case true && rand.Float64() < 1/math.Pow(exp, float64(c)): // roll the dice on incrementing
		(*fc)[k] = makeCount(true, c+1)
	}
}

// Get returns the number of hits for val []byte
func (fc *Counter) Get(val []byte) uint64 {
	k := makeKey(val)
	if v, ok := (*fc)[k]; ok {
		p, c := getCount(v)
		if !p {
			return uint64(c)
		}
		// if we are estimating the calculate the estimation
		return uint64(value(c))
	}
	return 0
}
<commit_msg>Add Len to return FlipCounter cardinality<commit_after>package flipcounter

import (
	"encoding/binary"
	"math"
	"math/rand"

	metro "github.com/dgryski/go-metro"
)

const (
	exp            = 1.00002
	guaranteeLimit = 8388607
	countbits      = 23
	expBase        = 255999
)

func value(c uint32) float64 {
	if c <= 1 {
		return float64(c)
	}
	return (1 - math.Pow(exp, float64(c))) / (1 - exp)
}

type key [5]byte   // guarantees us 1,099,511,627,776 unique keys
type count [3]byte // 24 bits, where 23 are counter and one is estimation flag

func hashToBytes(hash uint64) key {
	bytes := make([]byte, 10)
	_ = binary.PutUvarint(bytes, hash)
	return key{bytes[0], bytes[1], bytes[2], bytes[3]}
}

func makeKey(value []byte) key {
	return hashToBytes(metro.Hash64(value, 1337))
}

func getCount(value count) (bool, uint32) {
	p := value[2]>>7 == 1
	value[2] = value[2] << 1 >> 1
	bytes := []byte{value[0], value[1], value[2], 0}
	val := binary.LittleEndian.Uint32(bytes)
	return p, val
}

func makeCount(p bool, c uint32) count {
	bytes := make([]byte, 4)
	binary.LittleEndian.PutUint32(bytes, c)
	if p {
		bytes[2] = bytes[2] | (1 << 7)
	}
	return count{bytes[0], bytes[1], bytes[2]}
}

// Counter consits of a 5 bytes key and a 3 bytes estimating counter with a 100% accurary up to 8388607 hits, then an estimation of 1% error
type Counter map[key]count

// New return a HashLog sketch with 5 bytes keys and 3 bytes counters
func New() *Counter {
	dict := make(map[key]count)
	counter := Counter(dict)
	return &counter
}

// Increment increments the counter of val []byte by +1
func (fc *Counter) Increment(val []byte) {
	k := makeKey(val)
	v := (*fc)[k]
	p, c := getCount(v)

	switch {
	case !p && c < guaranteeLimit: // good old +1 counting
		(*fc)[k] = makeCount(false, c+1)
	case !p && c == guaranteeLimit: // flip the bit, its estimation time
		(*fc)[k] = makeCount(true, expBase)
	case true && rand.Float64() < 1/math.Pow(exp, float64(c)): // roll the dice on incrementing
		(*fc)[k] = makeCount(true, c+1)
	}
}

// Get returns the number of hits for val []byte
func (fc *Counter) Get(val []byte) uint64 {
	k := makeKey(val)
	if v, ok := (*fc)[k]; ok {
		p, c := getCount(v)
		if !p {
			return uint64(c)
		}
		// if we are estimating the calculate the estimation
		return uint64(value(c))
	}
	return 0
}

// Len ...
func (fc *Counter) Len() uint64 {
	return uint64(len(*fc))
}
<|endoftext|>


<commit_before>package test

import (
	"bytes"
	"errors"
	"fmt"
	"math"
	"reflect"
	"runtime"
	"strings"
	"testing"
)

// ErrPlain is the default error that is returned for functions in this package.
var ErrPlain = errors.New("error")

////////////////////////////////////////////////////////////////

func fileline(i int) string {
	_, file, line, ok := runtime.Caller(i)
	if !ok {
		return ""
	}
	parts := strings.Split(file, "/")
	file = parts[len(parts)-1]
	return fmt.Sprintf("%s:%d", file, line)
}

func trace() string {
	trace2 := fileline(2)
	trace3 := fileline(3)
	return "\r    " + strings.Repeat(" ", len(fmt.Sprintf("%s:", trace2))) + "\r    " + trace3
}

func message(msgs ...interface{}) string {
	if len(msgs) == 0 {
		return ""
	}
	s := fmt.Sprintln(msgs...)
	s = s[:len(s)-1] // remove newline
	return ": " + s
}

func printable(s string) string {
	s = strings.Replace(s, "\n", `\n`, -1)
	s = strings.Replace(s, "\r", `\r`, -1)
	s = strings.Replace(s, "\t", `\t`, -1)
	s = strings.Replace(s, "\x00", `\0`, -1)
	return s
}

const (
	Red   = "31"
	Green = "32"
)

func color(color string, s interface{}) string {
	return fmt.Sprintf("\033[00;%sm%v\033[00m", color, s)
}

/////////////////////////////////////////////////////////////////

func Fail(t *testing.T, msgs ...interface{}) {
	t.Fatalf("%s%s", trace(), message(msgs...))
}

func Error(t *testing.T, err error, msgs ...interface{}) {
	if err != nil {
		t.Fatalf("%s%s: %s", trace(), message(msgs...), color(Red, err.Error()))
	}
}

func That(t *testing.T, condition bool, msgs ...interface{}) {
	if !condition {
		t.Fatalf("%s%s: false", trace(), message(msgs...))
	}
}

func T(t *testing.T, got, wanted interface{}, msgs ...interface{}) {
	gotType := reflect.TypeOf(got)
	wantedType := reflect.TypeOf(wanted)
	if gotType != wantedType {
		t.Fatalf("%s%s: type %v != %v", trace(), message(msgs...), color(Red, gotType), color(Green, wantedType))
		return
	}
	if got == wanted {
		return
	}

	if wantedType != nil {
		if equals, ok := wantedType.MethodByName("Equals"); ok && equals.Type.NumIn() == 2 && equals.Type.NumOut() == 1 && equals.Type.In(0) == wantedType && equals.Type.In(1) == gotType && equals.Type.Out(0).Kind() == reflect.Bool && equals.Func.Call([]reflect.Value{reflect.ValueOf(wanted), reflect.ValueOf(got)})[0].Bool() {
			return
		}
	}
	t.Fatalf("%s%s: %v != %v", trace(), message(msgs...), color(Red, got), color(Green, wanted))
}

func Bytes(t *testing.T, got, wanted []byte, msgs ...interface{}) {
	if !bytes.Equal(got, wanted) {
		gotString := printable(string(got))
		wantedString := printable(string(wanted))
		t.Fatalf("%s%s:\n%s\n%s", trace(), message(msgs...), color(Red, gotString), color(Green, wantedString))
	}
}

func String(t *testing.T, got, wanted string, msgs ...interface{}) {
	if got != wanted {
		gotString := printable(got)
		wantedString := printable(wanted)
		t.Fatalf("%s%s:\n%s\n%s", trace(), message(msgs...), color(Red, gotString), color(Green, wantedString))
	}
}

func Float(t *testing.T, got, wanted float64, msgs ...interface{}) {
	if math.Abs(got-wanted) > 1e-6 {
		t.Fatalf("%s%s: %v != %v", trace(), message(msgs...), color(Red, got), color(Green, wanted))
	}
}

func Minify(t *testing.T, input string, err error, got, wanted string, msgs ...interface{}) {
	inputString := printable(input)
	if err != nil {
		t.Fatalf("%s%s:\n%s\n%s", trace(), message(msgs...), inputString, color(Red, err.Error()))
		return
	}

	if got != wanted {
		gotString := printable(got)
		wantedString := printable(wanted)
		t.Fatalf("%s%s:\n%s\n%s\n%s", trace(), message(msgs...), inputString, color(Red, gotString), color(Green, wantedString))
	}
}
<commit_msg>Float test for NaN<commit_after>package test

import (
	"bytes"
	"errors"
	"fmt"
	"math"
	"reflect"
	"runtime"
	"strings"
	"testing"
)

// ErrPlain is the default error that is returned for functions in this package.
var ErrPlain = errors.New("error")

////////////////////////////////////////////////////////////////

func fileline(i int) string {
	_, file, line, ok := runtime.Caller(i)
	if !ok {
		return ""
	}
	parts := strings.Split(file, "/")
	file = parts[len(parts)-1]
	return fmt.Sprintf("%s:%d", file, line)
}

func trace() string {
	trace2 := fileline(2)
	trace3 := fileline(3)
	return "\r    " + strings.Repeat(" ", len(fmt.Sprintf("%s:", trace2))) + "\r    " + trace3
}

func message(msgs ...interface{}) string {
	if len(msgs) == 0 {
		return ""
	}
	s := fmt.Sprintln(msgs...)
	s = s[:len(s)-1] // remove newline
	return ": " + s
}

func printable(s string) string {
	s = strings.Replace(s, "\n", `\n`, -1)
	s = strings.Replace(s, "\r", `\r`, -1)
	s = strings.Replace(s, "\t", `\t`, -1)
	s = strings.Replace(s, "\x00", `\0`, -1)
	return s
}

const (
	Red   = "31"
	Green = "32"
)

func color(color string, s interface{}) string {
	return fmt.Sprintf("\033[00;%sm%v\033[00m", color, s)
}

/////////////////////////////////////////////////////////////////

func Fail(t *testing.T, msgs ...interface{}) {
	t.Fatalf("%s%s", trace(), message(msgs...))
}

func Error(t *testing.T, err error, msgs ...interface{}) {
	if err != nil {
		t.Fatalf("%s%s: %s", trace(), message(msgs...), color(Red, err.Error()))
	}
}

func That(t *testing.T, condition bool, msgs ...interface{}) {
	if !condition {
		t.Fatalf("%s%s: false", trace(), message(msgs...))
	}
}

func T(t *testing.T, got, wanted interface{}, msgs ...interface{}) {
	gotType := reflect.TypeOf(got)
	wantedType := reflect.TypeOf(wanted)
	if gotType != wantedType {
		t.Fatalf("%s%s: type %v != %v", trace(), message(msgs...), color(Red, gotType), color(Green, wantedType))
		return
	}
	if got == wanted {
		return
	}

	if wantedType != nil {
		if equals, ok := wantedType.MethodByName("Equals"); ok && equals.Type.NumIn() == 2 && equals.Type.NumOut() == 1 && equals.Type.In(0) == wantedType && equals.Type.In(1) == gotType && equals.Type.Out(0).Kind() == reflect.Bool && equals.Func.Call([]reflect.Value{reflect.ValueOf(wanted), reflect.ValueOf(got)})[0].Bool() {
			return
		}
	}
	t.Fatalf("%s%s: %v != %v", trace(), message(msgs...), color(Red, got), color(Green, wanted))
}

func Bytes(t *testing.T, got, wanted []byte, msgs ...interface{}) {
	if !bytes.Equal(got, wanted) {
		gotString := printable(string(got))
		wantedString := printable(string(wanted))
		t.Fatalf("%s%s:\n%s\n%s", trace(), message(msgs...), color(Red, gotString), color(Green, wantedString))
	}
}

func String(t *testing.T, got, wanted string, msgs ...interface{}) {
	if got != wanted {
		gotString := printable(got)
		wantedString := printable(wanted)
		t.Fatalf("%s%s:\n%s\n%s", trace(), message(msgs...), color(Red, gotString), color(Green, wantedString))
	}
}

func Float(t *testing.T, got, wanted float64, msgs ...interface{}) {
	if math.IsNaN(wanted) != math.IsNaN(got) || !math.IsNaN(wanted) && math.Abs(got-wanted) > 1e-6 {
		t.Fatalf("%s%s: %v != %v", trace(), message(msgs...), color(Red, got), color(Green, wanted))
	}
}

func Minify(t *testing.T, input string, err error, got, wanted string, msgs ...interface{}) {
	inputString := printable(input)
	if err != nil {
		t.Fatalf("%s%s:\n%s\n%s", trace(), message(msgs...), inputString, color(Red, err.Error()))
		return
	}

	if got != wanted {
		gotString := printable(got)
		wantedString := printable(wanted)
		t.Fatalf("%s%s:\n%s\n%s\n%s", trace(), message(msgs...), inputString, color(Red, gotString), color(Green, wantedString))
	}
}
<|endoftext|>


<commit_before>package ircmsg

import "testing"

type testcase struct {
	escaped   string
	unescaped string
}

var tests = []testcase{
	{"te\\nst", "te\nst"},
}

func TestEscape(t *testing.T) {
	for _, pair := range tests {
		val := EscapeTagValue(pair.unescaped)

		if val != pair.escaped {
			t.Error(
				"For", pair.unescaped,
				"expected", pair.escaped,
				"got", val,
			)
		}
	}
}

func TestUnescape(t *testing.T) {
	for _, pair := range tests {
		val := UnescapeTagValue(pair.escaped)

		if val != pair.unescaped {
			t.Error(
				"For", pair.escaped,
				"expected", pair.unescaped,
				"got", val,
			)
		}
	}
}
<commit_msg>ircmsg: Add more tests<commit_after>package ircmsg

import "testing"

type testcase struct {
	escaped   string
	unescaped string
}

var tests = []testcase{
	{"te\\nst", "te\nst"},
	{"tes\\\\st", "tes\\st"},
}

var unescapeTests = []testcase{
	{"te\\n\\kst", "te\nkst"},
}

func TestEscape(t *testing.T) {
	for _, pair := range tests {
		val := EscapeTagValue(pair.unescaped)

		if val != pair.escaped {
			t.Error(
				"For", pair.unescaped,
				"expected", pair.escaped,
				"got", val,
			)
		}
	}
}

func TestUnescape(t *testing.T) {
	for _, pair := range tests {
		val := UnescapeTagValue(pair.escaped)

		if val != pair.unescaped {
			t.Error(
				"For", pair.escaped,
				"expected", pair.unescaped,
				"got", val,
			)
		}
	}
	for _, pair := range unescapeTests {
		val := UnescapeTagValue(pair.escaped)

		if val != pair.unescaped {
			t.Error(
				"For", pair.escaped,
				"expected", pair.unescaped,
				"got", val,
			)
		}
	}
}
<|endoftext|>


<commit_before>package stun

import (
	"encoding/binary"

	"github.com/pkg/errors"
)

type ChannelData struct {
	ChannelNumber uint16
	Data          []byte
}

func NewChannelData(packet []byte) (*ChannelData, error) {
	cn, err := getChannelNumber(packet)
	if err != nil {
		return nil, err
	}

	return &ChannelData{
		ChannelNumber: cn,
		Data:          packet[2:],
	}, nil
}

//  0b01: ChannelData message (since the channel number is the first
//  field in the ChannelData message and channel numbers fall in the
//  range 0x4000 - 0x7FFF).
func getChannelNumber(header []byte) (uint16, error) {
	cn := binary.BigEndian.Uint16(header)
	if cn < 0x4000 || cn > 0x7FFF {
		return 0, errors.Errorf("ChannelNumber is out of range: %d", cn)
	}
	return cn, nil
}
<commit_msg>turn: ChannelData works, ICE still never enters completed<commit_after>package stun

import (
	"encoding/binary"

	"github.com/pkg/errors"
)

type ChannelData struct {
	ChannelNumber uint16
	Data          []byte
}

func NewChannelData(packet []byte) (*ChannelData, error) {
	cn, err := getChannelNumber(packet)
	if err != nil {
		return nil, err
	}

	return &ChannelData{
		ChannelNumber: cn,
		Data:          packet,
	}, nil
}

//  0b01: ChannelData message (since the channel number is the first
//  field in the ChannelData message and channel numbers fall in the
//  range 0x4000 - 0x7FFF).
func getChannelNumber(header []byte) (uint16, error) {
	cn := binary.BigEndian.Uint16(header)
	if cn < 0x4000 || cn > 0x7FFF {
		return 0, errors.Errorf("ChannelNumber is out of range: %d", cn)
	}
	return cn, nil
}
<|endoftext|>


<commit_before>package drivers

import (
	"fmt"
	"os"
	"path/filepath"

	log "github.com/sirupsen/logrus"
	"github.com/docker/go-plugins-helpers/volume"
)

const (
	NfsOptions   = "nfsopts"
	DefaultNfsV3 = "port=2049,nolock,proto=tcp"
)

type nfsDriver struct {
	volumeDriver
	version int
	nfsopts map[string]string
}

var (
	EmptyMap = map[string]string{}
)

func NewNFSDriver(root string, version int, nfsopts string, mounts *MountManager) nfsDriver {
	d := nfsDriver{
		volumeDriver: newVolumeDriver(root, mounts),
		version:      version,
		nfsopts:      map[string]string{},
	}

	if len(nfsopts) > 0 {
		d.nfsopts[NfsOptions] = nfsopts
	}
	return d
}

func (n nfsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
	log.Debugf("Entering Mount: %v", r)
	n.m.Lock()
	defer n.m.Unlock()

	resolvedName, resOpts := resolveName(r.Name)

	hostdir := mountpoint(n.root, resolvedName)
	source := n.fixSource(resolvedName)

	// Support adhoc mounts (outside of docker volume create)
	// need to adjust source for ShareOpt
	if resOpts != nil {
		if share, found := resOpts[ShareOpt]; found {
			source = n.fixSource(share)
		}
	}

	if n.mountm.HasMount(resolvedName) && n.mountm.Count(resolvedName) > 0 {
		log.Infof("Using existing NFS volume mount: %s", hostdir)
		n.mountm.Increment(resolvedName)
		if err := run(fmt.Sprintf("grep -c %s /proc/mounts", hostdir)); err != nil {
			log.Infof("Existing NFS volume not mounted, force remount.")
			// maintain count
			n.mountm.Decrement(resolvedName)
		} else {
			//n.mountm.Increment(resolvedName)
			return &volume.MountResponse{Mountpoint: hostdir}, nil
		}
	}

	log.Infof("Mounting NFS volume %s on %s", source, hostdir)

	if err := createDest(hostdir); err != nil {
		return nil, err
	}

	if n.mountm.HasMount(resolvedName) == false {
		n.mountm.Create(resolvedName, hostdir, resOpts)
	}

	n.mountm.Add(resolvedName, hostdir)

	if err := n.mountVolume(resolvedName, source, hostdir, n.version); err != nil {
		return nil, err
	}

	if n.mountm.GetOption(resolvedName, ShareOpt) != "" && n.mountm.GetOptionAsBool(resolvedName, CreateOpt) {
		log.Infof("Mount: Share and Create options enabled - using %s as sub-dir mount", resolvedName)
		datavol := filepath.Join(hostdir, resolvedName)
		if err := createDest(filepath.Join(hostdir, resolvedName)); err != nil {
			return nil, err
		}
		hostdir = datavol
	}

	return &volume.MountResponse{Mountpoint: hostdir}, nil
}

func (n nfsDriver) Unmount(r *volume.UnmountRequest) error {
	log.Debugf("Entering Unmount: %v", r)

	n.m.Lock()
	defer n.m.Unlock()

	resolvedName, _ := resolveName(r.Name)

	hostdir := mountpoint(n.root, resolvedName)

	if n.mountm.HasMount(resolvedName) {
		if n.mountm.Count(resolvedName) > 1 {
			log.Printf("Skipping unmount for %s - in use by other containers", resolvedName)
			n.mountm.Decrement(resolvedName)
			return nil
		}
		n.mountm.Decrement(resolvedName)
	}

	log.Infof("Unmounting volume name %s from %s", resolvedName, hostdir)

	if err := run(fmt.Sprintf("umount %s", hostdir)); err != nil {
		log.Errorf("Error unmounting volume from host: %s", err.Error())
		return err
	}

	n.mountm.DeleteIfNotManaged(resolvedName)

	// Check if directory is empty. This command will return "err" if empty
	if err := run(fmt.Sprintf("ls -1 %s | grep .", hostdir)); err == nil {
		log.Warnf("Directory %s not empty after unmount. Skipping RemoveAll call.", hostdir)
	} else {
		if err := os.RemoveAll(hostdir); err != nil {
			return err
		}
	}

	return nil
}

func (n nfsDriver) fixSource(name string) string {
	if n.mountm.HasOption(name, ShareOpt) {
		return addShareColon(n.mountm.GetOption(name, ShareOpt))
	}
	return addShareColon(name)
}

func (n nfsDriver) mountVolume(name, source, dest string, version int) error {
	var cmd string

	options := merge(n.mountm.GetOptions(name), n.nfsopts)
	opts := ""
	if val, ok := options[NfsOptions]; ok {
		opts = val
	}

	mountCmd := "mount"

	if log.GetLevel() == log.DebugLevel {
		mountCmd = mountCmd + " -v"
	}

	switch version {
	case 3:
		log.Debugf("Mounting with NFSv3 - src: %s, dest: %s", source, dest)
		if len(opts) < 1 {
			opts = DefaultNfsV3
		}
		cmd = fmt.Sprintf("%s -t nfs -o %s %s %s", mountCmd, opts, source, dest)
	default:
		log.Debugf("Mounting with NFSv4 - src: %s, dest: %s", source, dest)
		if len(opts) > 0 {
			cmd = fmt.Sprintf("%s -t nfs4 -o %s %s %s", mountCmd, opts, source, dest)
		} else {
			cmd = fmt.Sprintf("%s -t nfs4 %s %s", mountCmd, source, dest)
		}
	}
	log.Debugf("exec: %s\n", cmd)
	return run(cmd)
}
<commit_msg>Fixed the bug with mount error.<commit_after>package drivers

import (
	"fmt"
	"os"
	"path/filepath"

	"github.com/docker/go-plugins-helpers/volume"
	log "github.com/sirupsen/logrus"
)

const (
	NfsOptions   = "nfsopts"
	DefaultNfsV3 = "port=2049,nolock,proto=tcp"
)

type nfsDriver struct {
	volumeDriver
	version int
	nfsopts map[string]string
}

var (
	EmptyMap = map[string]string{}
)

func NewNFSDriver(root string, version int, nfsopts string, mounts *MountManager) nfsDriver {
	d := nfsDriver{
		volumeDriver: newVolumeDriver(root, mounts),
		version:      version,
		nfsopts:      map[string]string{},
	}

	if len(nfsopts) > 0 {
		d.nfsopts[NfsOptions] = nfsopts
	}
	return d
}

func (n nfsDriver) Mount(r *volume.MountRequest) (*volume.MountResponse, error) {
	log.Debugf("Entering Mount: %v", r)
	n.m.Lock()
	defer n.m.Unlock()

	resolvedName, resOpts := resolveName(r.Name)

	hostdir := mountpoint(n.root, resolvedName)
	source := n.fixSource(resolvedName)

	// Support adhoc mounts (outside of docker volume create)
	// need to adjust source for ShareOpt
	if resOpts != nil {
		if share, found := resOpts[ShareOpt]; found {
			source = n.fixSource(share)
		}
	}

	if n.mountm.HasMount(resolvedName) {
		log.Infof("Using existing NFS volume mount: %s", hostdir)
		n.mountm.Increment(resolvedName)
		if err := run(fmt.Sprintf("grep -c %s /proc/mounts", hostdir)); err != nil {
			log.Infof("Existing NFS volume not mounted, force remount.")
			// maintain count
			if n.mountm.Count(resolvedName) > 0 {
				n.mountm.Decrement(resolvedName)
			}
		} else {
			//n.mountm.Increment(resolvedName)
			return &volume.MountResponse{Mountpoint: hostdir}, nil
		}
	}

	log.Infof("Mounting NFS volume %s on %s", source, hostdir)

	if err := createDest(hostdir); err != nil {
		if n.mountm.Count(resolvedName) > 0 {
			n.mountm.Decrement(resolvedName)
		}
		return nil, err
	}

	if n.mountm.HasMount(resolvedName) == false {
		n.mountm.Create(resolvedName, hostdir, resOpts)
	}

	n.mountm.Add(resolvedName, hostdir)

	if err := n.mountVolume(resolvedName, source, hostdir, n.version); err != nil {
		n.mountm.Decrement(resolvedName)
		return nil, err
	}

	if n.mountm.GetOption(resolvedName, ShareOpt) != "" && n.mountm.GetOptionAsBool(resolvedName, CreateOpt) {
		log.Infof("Mount: Share and Create options enabled - using %s as sub-dir mount", resolvedName)
		datavol := filepath.Join(hostdir, resolvedName)
		if err := createDest(filepath.Join(hostdir, resolvedName)); err != nil {
			n.mountm.Decrement(resolvedName)
			return nil, err
		}
		hostdir = datavol
	}

	return &volume.MountResponse{Mountpoint: hostdir}, nil
}

func (n nfsDriver) Unmount(r *volume.UnmountRequest) error {
	log.Debugf("Entering Unmount: %v", r)

	n.m.Lock()
	defer n.m.Unlock()

	resolvedName, _ := resolveName(r.Name)

	hostdir := mountpoint(n.root, resolvedName)

	if n.mountm.HasMount(resolvedName) {
		if n.mountm.Count(resolvedName) > 1 {
			log.Printf("Skipping unmount for %s - in use by other containers", resolvedName)
			n.mountm.Decrement(resolvedName)
			return nil
		}
		n.mountm.Decrement(resolvedName)
	}

	log.Infof("Unmounting volume name %s from %s", resolvedName, hostdir)

	if err := run(fmt.Sprintf("umount %s", hostdir)); err != nil {
		log.Errorf("Error unmounting volume from host: %s", err.Error())
		return err
	}

	n.mountm.DeleteIfNotManaged(resolvedName)

	// Check if directory is empty. This command will return "err" if empty
	if err := run(fmt.Sprintf("ls -1 %s | grep .", hostdir)); err == nil {
		log.Warnf("Directory %s not empty after unmount. Skipping RemoveAll call.", hostdir)
	} else {
		if err := os.RemoveAll(hostdir); err != nil {
			return err
		}
	}

	return nil
}

func (n nfsDriver) fixSource(name string) string {
	if n.mountm.HasOption(name, ShareOpt) {
		return addShareColon(n.mountm.GetOption(name, ShareOpt))
	}
	return addShareColon(name)
}

func (n nfsDriver) mountVolume(name, source, dest string, version int) error {
	var cmd string

	options := merge(n.mountm.GetOptions(name), n.nfsopts)
	opts := ""
	if val, ok := options[NfsOptions]; ok {
		opts = val
	}

	mountCmd := "mount"

	if log.GetLevel() == log.DebugLevel {
		mountCmd = mountCmd + " -v"
	}

	switch version {
	case 3:
		log.Debugf("Mounting with NFSv3 - src: %s, dest: %s", source, dest)
		if len(opts) < 1 {
			opts = DefaultNfsV3
		}
		cmd = fmt.Sprintf("%s -t nfs -o %s %s %s", mountCmd, opts, source, dest)
	default:
		log.Debugf("Mounting with NFSv4 - src: %s, dest: %s", source, dest)
		if len(opts) > 0 {
			cmd = fmt.Sprintf("%s -t nfs4 -o %s %s %s", mountCmd, opts, source, dest)
		} else {
			cmd = fmt.Sprintf("%s -t nfs4 %s %s", mountCmd, source, dest)
		}
	}
	log.Debugf("exec: %s\n", cmd)
	return run(cmd)
}
<|endoftext|>


<commit_before>package network_test

import (
	"github.com/APTrust/exchange/network"
	apt_testutil "github.com/APTrust/exchange/util/testutil"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"os"
	"strings"
	"testing"
)

func TestHead(t *testing.T) {
	// canTestS3, testBucket, testFile, testFileSize,
	// and testFileEtag are defined in s3_download_test
	if !canTestS3() {
		return
	}
	_context, err := apt_testutil.GetContext("integration.json")
	require.Nil(t, err, "Could not create context")
	client := network.NewS3Head(
		os.Getenv("AWS_ACCESS_KEY_ID"),
		os.Getenv("AWS_SECRET_ACCESS_KEY"),
		_context.Config.APTrustS3Region, testBucket)
	client.Head(testFile)
	assert.EqualValues(t, testFileSize, *client.Response.ContentLength)
	assert.Equal(t, testFileETag, *client.Response.ETag)
	assert.Equal(t, "application/x-tar", *client.Response.ContentType)
	trimmedETag := strings.Replace(testFileETag, "\"", "", -1)

	storedFile := client.StoredFile()
	assert.NotNil(t, storedFile)
	assert.Equal(t, trimmedETag, storedFile.ETag)

	dpnStoredFile := client.StoredFile()
	assert.NotNil(t, dpnStoredFile)

}
<commit_msg>Changed content type since file was re-uploaded<commit_after>package network_test

import (
	"github.com/APTrust/exchange/network"
	apt_testutil "github.com/APTrust/exchange/util/testutil"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
	"os"
	"strings"
	"testing"
)

func TestHead(t *testing.T) {
	// canTestS3, testBucket, testFile, testFileSize,
	// and testFileEtag are defined in s3_download_test
	if !canTestS3() {
		return
	}
	_context, err := apt_testutil.GetContext("integration.json")
	require.Nil(t, err, "Could not create context")
	client := network.NewS3Head(
		os.Getenv("AWS_ACCESS_KEY_ID"),
		os.Getenv("AWS_SECRET_ACCESS_KEY"),
		_context.Config.APTrustS3Region, testBucket)
	client.Head(testFile)
	assert.EqualValues(t, testFileSize, *client.Response.ContentLength)
	assert.Equal(t, testFileETag, *client.Response.ETag)
	assert.Equal(t, "binary/octet-stream", *client.Response.ContentType)
	trimmedETag := strings.Replace(testFileETag, "\"", "", -1)

	storedFile := client.StoredFile()
	assert.NotNil(t, storedFile)
	assert.Equal(t, trimmedETag, storedFile.ETag)

	dpnStoredFile := client.StoredFile()
	assert.NotNil(t, dpnStoredFile)

}
<|endoftext|>


<commit_before>package leds

import (
	"fmt"
	"math"
	"time"

	"github.com/siggy/bbox/bbox/color"
	"github.com/siggy/bbox/beatboxer/render/web"
	"github.com/siggy/rpi_ws281x/golang/ws2811"
)

const (
	// TODO: bbox testing
	// 2x structure
	BAUX_STRAND_COUNT1 = 1
	BAUX_STRAND_LEN1   = 85

	// 1x globe
	BAUX_STRAND_COUNT2 = 4
	BAUX_STRAND_LEN2   = 60

	BAUX_LED_COUNT1 = BAUX_STRAND_COUNT1 * BAUX_STRAND_LEN1 // 5*30 // 30/m
	BAUX_LED_COUNT2 = BAUX_STRAND_COUNT2 * BAUX_STRAND_LEN2 // 4*60 // 60/m

	DEFAULT_INTERVAL_MS = 2000

	BAUX_STREAK_LENGTH = BAUX_LED_COUNT1 * 3 / 4
)

// LEDS
var globeLeds = []int{
	0,
	29,
	55,
	79,
	102,
	124,
	145,
	165,
	183,
	200,
	216,
	230,
}

type Baux struct {
	ampLevel float64
	closing  chan struct{}
	level    <-chan float64
	w        *web.Web
}

func InitBaux(level <-chan float64, w *web.Web) *Baux {
	InitLeds(DEFAULT_FREQ, BAUX_LED_COUNT1, BAUX_LED_COUNT2)

	return &Baux{
		closing: make(chan struct{}),
		level:   level,
		w:       w,
	}
}

func (c *Baux) Run() {
	defer func() {
		ws2811.Clear()
		ws2811.Render()
		ws2811.Wait()
		ws2811.Fini()
	}()

	fmt.Printf("ws2811.Clear()\n")
	ws2811.Clear()

	strand1 := make([]uint32, BAUX_LED_COUNT1) // base
	strand2 := make([]uint32, BAUX_LED_COUNT2) // globe

	phoneR := uint32(200)
	phoneG := uint32(0)
	phoneB := uint32(100)
	webMotion := uint32(0)

	globeColor1 := color.TrueRed
	globeColor2 := color.Black

	last := time.Now()
	interval := 2 * time.Second

	for {
		select {
		case phone, more := <-c.w.Phone():
			if more {
				webMotion = color.ScaleMotion(
					phone.Motion.Acceleration.X,
					phone.Motion.Acceleration.Y,
					phone.Motion.Acceleration.Z,
				)
				phoneR = phone.R
				phoneG = phone.G
				phoneB = phone.B

				globeColor1 = color.Make(
					phoneR,
					phoneG,
					phoneB,
					0,
				)

				globeColor2 = color.Make(
					255-phoneR,
					255-phoneG,
					255-phoneB,
					0,
				)

			} else {
				return
			}
		case level, more := <-c.level:
			if more {
				c.ampLevel = level
			} else {
				return
			}
		case _, more := <-c.closing:
			if !more {
				return
			}
		default:
			_ = uint32(255.0 * c.ampLevel)
			interval = time.Duration(math.Max(
				DEFAULT_INTERVAL_MS-(DEFAULT_INTERVAL_MS*c.ampLevel),
				100,
			)) * time.Millisecond

			now := time.Now()
			loc := 1.0 - float64(now.Sub(last).Nanoseconds())/float64(interval.Nanoseconds())

			if loc < 0 {
				loc = 1
				last = now
			}

			// streaks
			sineMap := color.GetSineVals(BAUX_LED_COUNT1, loc*BAUX_LED_COUNT1, BAUX_STREAK_LENGTH)
			for led, value := range sineMap {
				mag := float64(value) / 254.0
				strand1[led] = color.Make(
					uint32(float64(phoneR)*mag),
					uint32(float64(phoneG)*mag),
					uint32(float64(phoneB)*mag),
					uint32(float64(webMotion)*mag),
				)
			}

			// globe
			for i := 0; i < BAUX_LED_COUNT2; i++ {
				strand2[i] = 0
			}
			for i := 0; i < len(globeLeds)-1; i++ {
				start := globeLeds[i]
				end := globeLeds[i+1] - 1
				peak1 := float64(start) + float64(end-start)*loc

				p2 := loc + 0.5 - math.Trunc(loc+0.5)
				peak2 := float64(start) + float64(end-start)*p2

				sineMap1 := color.GetSineVals(end-start, peak1, (end-start)/2)
				for led, value := range sineMap1 {
					mag := float64(value) / 254.0
					strand2[start+led] = color.MultiplyColor(globeColor1, mag)
				}

				sineMap2 := color.GetSineVals(end-start, peak2, (end-start)/2)
				for led, value := range sineMap2 {
					mag := float64(value) / 254.0
					strand2[start+led] = color.MultiplyColor(globeColor2, mag)
				}
			}

			ws2811.SetBitmap(0, strand1)
			ws2811.SetBitmap(1, strand2)

			err := ws2811.Render()
			if err != nil {
				fmt.Printf("ws2811.Render failed: %+v\n", err)
				panic(err)
			}

			err = ws2811.Wait()
			if err != nil {
				fmt.Printf("ws2811.Wait failed: %+v\n", err)
				panic(err)
			}
		}
	}
}

func (c *Baux) Close() {
	// TODO: this doesn't block?
	close(c.closing)
}
<commit_msg>fixed baux globe streak calculations<commit_after>package leds

import (
	"fmt"
	"math"
	"time"

	"github.com/siggy/bbox/bbox/color"
	"github.com/siggy/bbox/beatboxer/render/web"
	"github.com/siggy/rpi_ws281x/golang/ws2811"
)

const (
	// TODO: bbox testing
	// 2x structure
	BAUX_STRAND_COUNT1 = 1
	BAUX_STRAND_LEN1   = 85

	// 1x globe
	BAUX_STRAND_COUNT2 = 4
	BAUX_STRAND_LEN2   = 60

	BAUX_LED_COUNT1 = BAUX_STRAND_COUNT1 * BAUX_STRAND_LEN1 // 5*30 // 30/m
	BAUX_LED_COUNT2 = BAUX_STRAND_COUNT2 * BAUX_STRAND_LEN2 // 4*60 // 60/m

	DEFAULT_INTERVAL_MS = 2000

	BAUX_STREAK_LENGTH = BAUX_LED_COUNT1 * 3 / 4
)

// LEDS
var globeLeds = []int{
	0,
	29,
	55,
	79,
	102,
	124,
	145,
	165,
	183,
	200,
	216,
	230,
}

type Baux struct {
	ampLevel float64
	closing  chan struct{}
	level    <-chan float64
	w        *web.Web
}

func InitBaux(level <-chan float64, w *web.Web) *Baux {
	InitLeds(DEFAULT_FREQ, BAUX_LED_COUNT1, BAUX_LED_COUNT2)

	return &Baux{
		closing: make(chan struct{}),
		level:   level,
		w:       w,
	}
}

func (c *Baux) Run() {
	defer func() {
		ws2811.Clear()
		ws2811.Render()
		ws2811.Wait()
		ws2811.Fini()
	}()

	fmt.Printf("ws2811.Clear()\n")
	ws2811.Clear()

	strand1 := make([]uint32, BAUX_LED_COUNT1) // base
	strand2 := make([]uint32, BAUX_LED_COUNT2) // globe

	phoneR := uint32(200)
	phoneG := uint32(0)
	phoneB := uint32(100)
	webMotion := uint32(0)

	globeColor1 := color.TrueRed
	globeColor2 := color.Black

	last := time.Now()
	interval := 2 * time.Second

	for {
		select {
		case phone, more := <-c.w.Phone():
			if more {
				webMotion = color.ScaleMotion(
					phone.Motion.Acceleration.X,
					phone.Motion.Acceleration.Y,
					phone.Motion.Acceleration.Z,
				)
				phoneR = phone.R
				phoneG = phone.G
				phoneB = phone.B

				globeColor1 = color.Make(
					phoneR,
					phoneG,
					phoneB,
					0,
				)

				globeColor2 = color.Make(
					255-phoneR,
					255-phoneG,
					255-phoneB,
					0,
				)

			} else {
				return
			}
		case level, more := <-c.level:
			if more {
				c.ampLevel = level
			} else {
				return
			}
		case _, more := <-c.closing:
			if !more {
				return
			}
		default:
			_ = uint32(255.0 * c.ampLevel)
			interval = time.Duration(math.Max(
				DEFAULT_INTERVAL_MS-(DEFAULT_INTERVAL_MS*c.ampLevel),
				100,
			)) * time.Millisecond

			now := time.Now()
			loc := 1.0 - float64(now.Sub(last).Nanoseconds())/float64(interval.Nanoseconds())

			if loc < 0 {
				loc = 1
				last = now
			}

			// streaks
			sineMap := color.GetSineVals(BAUX_LED_COUNT1, loc*BAUX_LED_COUNT1, BAUX_STREAK_LENGTH)
			for led, value := range sineMap {
				mag := float64(value) / 254.0
				strand1[led] = color.Make(
					uint32(float64(phoneR)*mag),
					uint32(float64(phoneG)*mag),
					uint32(float64(phoneB)*mag),
					uint32(float64(webMotion)*mag),
				)
			}

			// globe
			for i := 0; i < BAUX_LED_COUNT2; i++ {
				strand2[i] = 0
			}
			for i := 0; i < len(globeLeds)-1; i++ {
				start := globeLeds[i]
				end := globeLeds[i+1] - 1
				length := end - start

				peak1 := float64(length) * loc

				loc2 := loc + 0.5 - math.Trunc(loc+0.5)
				peak2 := float64(length) * loc2

				sineMap1 := color.GetSineVals(length, peak1, length/2)
				for led, value := range sineMap1 {
					mag := float64(value) / 254.0
					strand2[start+led] = color.MultiplyColor(globeColor1, mag)
				}

				sineMap2 := color.GetSineVals(length, peak2, length/2)
				for led, value := range sineMap2 {
					mag := float64(value) / 254.0
					strand2[start+led] = color.MultiplyColor(globeColor2, mag)
				}

				// if i == 7 {
				// 	fmt.Printf("\nGLOBE[%2d->%2d]: %3d->%3d peak1: %5.1f peak2: %5.1f\n", i, i+1, start, end, peak1, peak2)

				// 	fmt.Printf("  GetSineVals1(%3d, %5.1f, %3d): ", length, peak1, length/2)
				// 	keys := []int{}
				// 	for k := range sineMap1 {
				// 		keys = append(keys, k)
				// 	}
				// 	sort.Ints(keys)
				// 	for _, k := range keys {
				// 		fmt.Printf("%d:%3.2f, ", start+k, float64(sineMap1[k])/254.0)
				// 	}
				// 	// log.Infof("    %+v", sineMap1)

				// 	fmt.Printf("\n  GetSineVals2(%3d, %5.1f, %3d): ", length, peak2, length/2)
				// 	keys = []int{}
				// 	for k := range sineMap2 {
				// 		keys = append(keys, k)
				// 	}
				// 	sort.Ints(keys)
				// 	for _, k := range keys {
				// 		fmt.Printf("%d:%3.2f, ", start+k, float64(sineMap2[k])/254.0)
				// 	}
				// 	// log.Infof("    %+v", sineMap2)
				// }
			}

			ws2811.SetBitmap(0, strand1)
			ws2811.SetBitmap(1, strand2)

			err := ws2811.Render()
			if err != nil {
				fmt.Printf("ws2811.Render failed: %+v\n", err)
				panic(err)
			}

			err = ws2811.Wait()
			if err != nil {
				fmt.Printf("ws2811.Wait failed: %+v\n", err)
				panic(err)
			}
		}
	}
}

func (c *Baux) Close() {
	// TODO: this doesn't block?
	close(c.closing)
}
<|endoftext|>


<commit_before>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package sysinit

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"

	log "github.com/Sirupsen/logrus"

	"mynewt.apache.org/newt/newt/newtutil"
	"mynewt.apache.org/newt/newt/pkg"
	"mynewt.apache.org/newt/util"
)

type initFunc struct {
	stage int
	name  string
	pkg   *pkg.LocalPackage
}

func buildStageMap(pkgs []*pkg.LocalPackage) map[int][]*initFunc {
	sm := map[int][]*initFunc{}

	for _, p := range pkgs {
		for name, stage := range p.Init() {
			initFunc := &initFunc{
				stage: stage,
				name:  name,
				pkg:   p,
			}
			sm[stage] = append(sm[stage], initFunc)
		}
	}

	return sm
}

func writePrototypes(pkgs []*pkg.LocalPackage, w io.Writer) {
	sorted := pkg.SortLclPkgs(pkgs)
	for _, p := range sorted {
		init := p.Init()
		for name, _ := range init {
			fmt.Fprintf(w, "void %s(void);\n", name)
		}
	}
}

func writeStage(stage int, initFuncs []*initFunc, w io.Writer) {
	fmt.Fprintf(w, "    /*** Stage %d */\n", stage)
	for i, initFunc := range initFuncs {
		fmt.Fprintf(w, "    /* %d.%d: %s */\n", stage, i, initFunc.pkg.Name())
		fmt.Fprintf(w, "    %s();\n", initFunc.name)
	}
}

func write(pkgs []*pkg.LocalPackage, isLoader bool,
	w io.Writer) {

	stageMap := buildStageMap(pkgs)

	i := 0
	stages := make([]int, len(stageMap))
	for k, _ := range stageMap {
		stages[i] = k
		i++
	}
	sort.Ints(stages)

	fmt.Fprintf(w, newtutil.GeneratedPreamble())

	if isLoader {
		fmt.Fprintf(w, "#if SPLIT_LOADER\n\n")
	} else {
		fmt.Fprintf(w, "#if !SPLIT_LOADER\n\n")
	}

	writePrototypes(pkgs, w)

	var fnName string
	if isLoader {
		fnName = "sysinit_loader"
	} else {
		fnName = "sysinit_app"
	}

	fmt.Fprintf(w, "\n")
	fmt.Fprintf(w, "void\n%s(void)\n{\n", fnName)

	for _, s := range stages {
		fmt.Fprintf(w, "\n")
		writeStage(s, stageMap[s], w)
	}

	fmt.Fprintf(w, "}\n\n")
	fmt.Fprintf(w, "#endif\n")
}

func writeRequired(contents []byte, path string) (bool, error) {
	oldSrc, err := ioutil.ReadFile(path)
	if err != nil {
		if os.IsNotExist(err) {
			// File doesn't exist; write required.
			return true, nil
		}

		return true, util.NewNewtError(err.Error())
	}

	rc := bytes.Compare(oldSrc, contents)
	return rc != 0, nil
}

func EnsureWritten(pkgs []*pkg.LocalPackage, srcDir string, targetName string,
	isLoader bool) error {

	buf := bytes.Buffer{}
	write(pkgs, isLoader, &buf)

	var path string
	if isLoader {
		path = fmt.Sprintf("%s/%s-sysinit-loader.c", srcDir, targetName)
	} else {
		path = fmt.Sprintf("%s/%s-sysinit-app.c", srcDir, targetName)
	}

	writeReqd, err := writeRequired(buf.Bytes(), path)
	if err != nil {
		return err
	}

	if !writeReqd {
		log.Debugf("sysinit unchanged; not writing src file (%s).", path)
		return nil
	}

	log.Debugf("sysinit changed; writing src file (%s).", path)

	if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
		return util.NewNewtError(err.Error())
	}

	if err := ioutil.WriteFile(path, buf.Bytes(), 0644); err != nil {
		return util.NewNewtError(err.Error())
	}

	return nil
}
<commit_msg>MYNEWT-753 newt - sysinit file inconsistent<commit_after>/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package sysinit

import (
	"bytes"
	"fmt"
	"io"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"

	log "github.com/Sirupsen/logrus"

	"mynewt.apache.org/newt/newt/newtutil"
	"mynewt.apache.org/newt/newt/pkg"
	"mynewt.apache.org/newt/util"
)

type initFunc struct {
	stage int
	name  string
	pkg   *pkg.LocalPackage
}

func buildStageMap(pkgs []*pkg.LocalPackage) map[int][]*initFunc {
	sm := map[int][]*initFunc{}

	for _, p := range pkgs {
		for name, stage := range p.Init() {
			initFunc := &initFunc{
				stage: stage,
				name:  name,
				pkg:   p,
			}
			sm[stage] = append(sm[stage], initFunc)
		}
	}

	return sm
}

func writePrototypes(pkgs []*pkg.LocalPackage, w io.Writer) {
	sorted := pkg.SortLclPkgs(pkgs)
	for _, p := range sorted {
		init := p.Init()
		for name, _ := range init {
			fmt.Fprintf(w, "void %s(void);\n", name)
		}
	}
}

func writeStage(stage int, initFuncs []*initFunc, w io.Writer) {
	// Sort stage alphabetically by package name.
	pkgNames := make([]string, len(initFuncs))
	funcMap := make(map[string]*initFunc, len(initFuncs))
	for i, initFunc := range initFuncs {
		name := initFunc.pkg.Name()
		pkgNames[i] = name
		funcMap[name] = initFunc
	}
	sort.Strings(pkgNames)

	fmt.Fprintf(w, "    /*** Stage %d */\n", stage)
	for i, pkgName := range pkgNames {
		initFunc := funcMap[pkgName]
		fmt.Fprintf(w, "    /* %d.%d: %s */\n", stage, i, pkgName)
		fmt.Fprintf(w, "    %s();\n", initFunc.name)
	}
}

func write(pkgs []*pkg.LocalPackage, isLoader bool,
	w io.Writer) {

	stageMap := buildStageMap(pkgs)

	i := 0
	stages := make([]int, len(stageMap))
	for k, _ := range stageMap {
		stages[i] = k
		i++
	}
	sort.Ints(stages)

	fmt.Fprintf(w, newtutil.GeneratedPreamble())

	if isLoader {
		fmt.Fprintf(w, "#if SPLIT_LOADER\n\n")
	} else {
		fmt.Fprintf(w, "#if !SPLIT_LOADER\n\n")
	}

	writePrototypes(pkgs, w)

	var fnName string
	if isLoader {
		fnName = "sysinit_loader"
	} else {
		fnName = "sysinit_app"
	}

	fmt.Fprintf(w, "\n")
	fmt.Fprintf(w, "void\n%s(void)\n{\n", fnName)

	for _, s := range stages {
		fmt.Fprintf(w, "\n")
		writeStage(s, stageMap[s], w)
	}

	fmt.Fprintf(w, "}\n\n")
	fmt.Fprintf(w, "#endif\n")
}

func writeRequired(contents []byte, path string) (bool, error) {
	oldSrc, err := ioutil.ReadFile(path)
	if err != nil {
		if os.IsNotExist(err) {
			// File doesn't exist; write required.
			return true, nil
		}

		return true, util.NewNewtError(err.Error())
	}

	rc := bytes.Compare(oldSrc, contents)
	return rc != 0, nil
}

func EnsureWritten(pkgs []*pkg.LocalPackage, srcDir string, targetName string,
	isLoader bool) error {

	buf := bytes.Buffer{}
	write(pkgs, isLoader, &buf)

	var path string
	if isLoader {
		path = fmt.Sprintf("%s/%s-sysinit-loader.c", srcDir, targetName)
	} else {
		path = fmt.Sprintf("%s/%s-sysinit-app.c", srcDir, targetName)
	}

	writeReqd, err := writeRequired(buf.Bytes(), path)
	if err != nil {
		return err
	}

	if !writeReqd {
		log.Debugf("sysinit unchanged; not writing src file (%s).", path)
		return nil
	}

	log.Debugf("sysinit changed; writing src file (%s).", path)

	if err := os.MkdirAll(filepath.Dir(path), 0755); err != nil {
		return util.NewNewtError(err.Error())
	}

	if err := ioutil.WriteFile(path, buf.Bytes(), 0644); err != nil {
		return util.NewNewtError(err.Error())
	}

	return nil
}
<|endoftext|>


<commit_before>package TF2RconWrapper

import (
	"fmt"
	"log"
	"math/rand"
	"net"
	"strconv"
	"sync"
	"time"
)

type RawMessage struct {
	data []byte
	n    int //length
}

func (r RawMessage) String() string {
	return string(r.data)
}

// RconChatListener maintains an UDP server that receives redirected chat messages from TF2 servers
type RconChatListener struct {
	conn        *net.UDPConn
	servers     map[string]*ServerListener
	serversLock *sync.RWMutex
	wait        *sync.WaitGroup
	addr        *net.UDPAddr
	localip     string
	port        string
	rng         *rand.Rand
}

// NewRconChatListener builds a new RconChatListener. Its arguments are localip (the ip of this server) and
// port (the port the listener will use)
func NewRconChatListener(localip, port string) (*RconChatListener, error) {
	addr, err := net.ResolveUDPAddr("udp4", ":"+port)
	if err != nil {
		return nil, err
	}

	servers := make(map[string]*ServerListener)

	rng := rand.New(rand.NewSource(time.Now().Unix()))

	listener := &RconChatListener{nil, servers, new(sync.RWMutex), new(sync.WaitGroup), addr, localip, port, rng}
	listener.startListening()
	return listener, nil
}

func (r *RconChatListener) startListening() {
	conn, err := net.ListenUDP("udp", r.addr)
	r.conn = conn
	if err != nil {
		log.Fatal(err)
		return
	}
	conn.SetReadBuffer(1048576)
	go r.readStrings()
}

func (r *RconChatListener) readStrings() {
	rawMessageC := make(chan RawMessage, 500)

	go func() {
		for {
			raw := <-rawMessageC
			r.wait.Wait()
			message := raw.data[0:raw.n]
			secret, err := getSecret(message)
			if err != nil {
				continue
			}

			r.serversLock.RLock()
			s, ok := r.servers[secret]
			r.serversLock.RUnlock()
			if ok {
				s.RawMessages <- raw
			}
		}
	}()

	for {
		buff := make([]byte, 65000)
		n, err := r.conn.Read(buff)
		//log.Println(string(buff[0:n]))

		if err != nil {
			fmt.Println("Error receiving server chat data: ", err)
			continue
		}

		rawMessageC <- RawMessage{buff, n}
	}
}

// Close stops the RconChatListener
func (s *ServerListener) Close(m *TF2RconConnection) {
	s.listener.wait.Add(1)
	delete(s.listener.servers, s.secret)
	s.listener.wait.Done()

	m.StopLogRedirection(s.listener.localip, s.listener.port)
}

// CreateServerListener creates a ServerListener that receives chat messages from a
// particular TF2 server
func (r *RconChatListener) CreateServerListener(m *TF2RconConnection) *ServerListener {

	secret := strconv.Itoa(r.rng.Intn(999998) + 1)

	r.serversLock.RLock()
	_, ok := r.servers[secret]
	for ok {
		secret = strconv.Itoa(r.rng.Intn(999998) + 1)
		_, ok = r.servers[secret]
	}
	r.serversLock.RUnlock()

	s := &ServerListener{make(chan RawMessage, 10), m.host, secret, r}

	r.serversLock.Lock()
	r.servers[secret] = s
	//log.Println(r.servers)
	r.serversLock.Unlock()

	m.Query("sv_logsecret " + secret)
	m.RedirectLogs(r.localip, r.port)
	return s
}

// ServerListener represents a listener that receives chat messages from a particular
// TF2 server. It's built and managed by an RconChatListener instance.
type ServerListener struct {
	RawMessages chan RawMessage

	host     string
	secret   string
	listener *RconChatListener
}
<commit_msg>Add CreateListenerWithSecret, use it.<commit_after>package TF2RconWrapper

import (
	"fmt"
	"log"
	"math/rand"
	"net"
	"strconv"
	"sync"
	"time"
)

type RawMessage struct {
	data []byte
	n    int //length
}

func (r RawMessage) String() string {
	return string(r.data)
}

// RconChatListener maintains an UDP server that receives redirected chat messages from TF2 servers
type RconChatListener struct {
	conn        *net.UDPConn
	servers     map[string]*ServerListener
	serversLock *sync.RWMutex
	wait        *sync.WaitGroup
	addr        *net.UDPAddr
	localip     string
	port        string
	rng         *rand.Rand
}

// NewRconChatListener builds a new RconChatListener. Its arguments are localip (the ip of this server) and
// port (the port the listener will use)
func NewRconChatListener(localip, port string) (*RconChatListener, error) {
	addr, err := net.ResolveUDPAddr("udp4", ":"+port)
	if err != nil {
		return nil, err
	}

	servers := make(map[string]*ServerListener)

	rng := rand.New(rand.NewSource(time.Now().Unix()))

	listener := &RconChatListener{nil, servers, new(sync.RWMutex), new(sync.WaitGroup), addr, localip, port, rng}
	listener.startListening()
	return listener, nil
}

func (r *RconChatListener) startListening() {
	conn, err := net.ListenUDP("udp", r.addr)
	r.conn = conn
	if err != nil {
		log.Fatal(err)
		return
	}
	conn.SetReadBuffer(1048576)
	go r.readStrings()
}

func (r *RconChatListener) readStrings() {
	rawMessageC := make(chan RawMessage, 500)

	go func() {
		for {
			raw := <-rawMessageC
			r.wait.Wait()
			message := raw.data[0:raw.n]
			secret, err := getSecret(message)
			if err != nil {
				continue
			}

			r.serversLock.RLock()
			s, ok := r.servers[secret]
			r.serversLock.RUnlock()
			if ok {
				s.RawMessages <- raw
			}
		}
	}()

	for {
		buff := make([]byte, 65000)
		n, err := r.conn.Read(buff)
		//log.Println(string(buff[0:n]))

		if err != nil {
			fmt.Println("Error receiving server chat data: ", err)
			continue
		}

		rawMessageC <- RawMessage{buff, n}
	}
}

// Close stops the RconChatListener
func (s *ServerListener) Close(m *TF2RconConnection) {
	s.listener.wait.Add(1)
	delete(s.listener.servers, s.secret)
	s.listener.wait.Done()

	m.StopLogRedirection(s.listener.localip, s.listener.port)
}

// CreateServerListener creates a ServerListener that receives chat messages from a
// particular TF2 server
func (r *RconChatListener) CreateServerListener(m *TF2RconConnection) *ServerListener {

	secret := strconv.Itoa(r.rng.Intn(999998) + 1)

	r.serversLock.RLock()
	_, ok := r.servers[secret]
	for ok {
		secret = strconv.Itoa(r.rng.Intn(999998) + 1)
		_, ok = r.servers[secret]
	}
	r.serversLock.RUnlock()

	return r.CreateListenerWithSecret(m, secret)
}

func (r *RconChatListener) CreateListenerWithSecret(m *TF2RconConnection, secret string) *ServerListener {
	s := &ServerListener{make(chan RawMessage, 10), m.host, secret, r}

	r.serversLock.Lock()
	r.servers[secret] = s
	//log.Println(r.servers)
	r.serversLock.Unlock()

	m.Query("sv_logsecret " + secret)
	m.RedirectLogs(r.localip, r.port)
	return s
}

// ServerListener represents a listener that receives chat messages from a particular
// TF2 server. It's built and managed by an RconChatListener instance.
type ServerListener struct {
	RawMessages chan RawMessage

	host     string
	secret   string
	listener *RconChatListener
}
<|endoftext|>


<commit_before>package beater

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
)

const CLUSTER_HEALTH = "/_cluster/health"
const CLUSTER_STATS = "/_cluster/stats"

type ClusterHealth struct {
	Cluster_name                     string  `json:"cluster_name"`
	Status                           string  `json:"status"`
	Status_num                       int64   `json:"status_num"`
	Timed_out                        bool    `json:"timed_out"`
	Number_of_nodes                  uint64  `json:"number_of_nodes"`
	Number_of_data_nodes             uint64  `json:"number_of_data_nodes"`
	Active_primary_shards            uint64  `json:"active_primary_shards"`
	Active_shards                    uint64  `json:"active_shards"`
	Relocating_shards                uint64  `json:"relocating_shards"`
	Initializing_shards              uint64  `json:"intializing_shards"`
	Unassigned_shards                uint64  `json:"unassigned_shards"`
	Delayed_unassigned_shards        uint64  `json:"delayed_unassigned_shards"`
	Number_of_pending_tasks          uint64  `json:"number_of_pending_tasks"`
	Number_of_in_flight_fetch        uint64  `json:"number_of_in_flight_fetch"`
	Task_max_waiting_in_queue_millis uint64  `json:"task_max_waiting_in_queue_millis"`
	Active_shards_percent_as_number  float64 `json:"active_shards_percent_as_number"`
}

type ClusterStats struct {
	Timestamp    uint64 `json:"timestamp"`
	Cluster_name string `json:"cluster_name"`
	Status       string `json:"status"`
	Status_num   int64  `json:"status_num"`
	Indices      struct {
		Count  uint64 `json:"count"`
		Shards struct {
			Total       uint64  `json:"total"`
			Primaries   uint64  `json:"primaries"`
			Replication float64 `json:"replication"`
			Index       struct {
				Shards struct {
					Min float64 `json:"nin"`
					Max float64 `json:"max"`
					Avg float64 `json:"avg"`
				}
				Primaries struct {
					Min float64 `json:"nin"`
					Max float64 `json:"max"`
					Avg float64 `json:"avg"`
				}
				Replication struct {
					Min float64 `json:"nin"`
					Max float64 `json:"max"`
					Avg float64 `json:"avg"`
				}
			}
		}
		Docs struct {
			Count   uint64 `json:"count"`
			Deleted uint64 `json:"deleted"`
		}
		Store struct {
			Size_in_bytes           uint64 `json:"size_in_bytes"`
			Throttle_time_in_millis uint64 `json:"throttle_time_in_millis"`
		}
		Fielddata struct {
			Memory_size_in_bytes uint64 `json:"memory_size_in_bytes"`
			Evictions            uint64 `json:"evictions"`
		}
		Query_cache struct {
			Memory_size_in_bytes uint64 `json:"memory_size_in_bytes"`
			Total_count          uint64 `json:"total_count"`
			Hit_count            uint64 `json:"hit_count"`
			Miss_count           uint64 `json:"miss_count"`
			Cache_size           uint64 `json:"cache_size"`
			Cache_count          uint64 `json:"cache_count"`
			Evictions            uint64 `json:"evictions"`
		}
		Completion struct {
			Size_in_bytes uint64 `json:"size_in_bytes"`
		}
		Segments struct {
			Count                            uint64 `json:"count"`
			Memory_in_bytes                  uint64 `json:"memory_in_bytes"`
			Terms_memory_in_bytes            uint64 `json:"terms_memory_in_bytes"`
			Stored_fields_memory_in_bytes    uint64 `json:"stored_fields_memory_in_bytes"`
			Term_vectors_memory_in_bytes     uint64 `json:"term_vectors_memory_in_bytes"`
			Norms_memory_in_bytes            uint64 `json:"norms_memory_in_bytes"`
			Doc_values_memory_in_bytes       uint64 `json:"doc_values_memory_in_bytes"`
			Index_writer_memory_in_bytes     uint64 `json:"index_writer_memory_in_bytes"`
			Index_writer_max_memory_in_bytes uint64 `json:"index_writer_max_memory_in_bytes"`
			Version_map_memory_in_bytes      uint64 `json:"version_map_memory_in_bytes"`
			Fixed_bit_set_memory_in_bytes    uint64 `json:"fixed_bit_set_memory_in_bytes"`
		}
		Percolate struct {
			Total                uint64 `json:"total"`
			Time_in_millis       uint64 `json:"time_in_millis"`
			Current              uint64 `json:"current"`
			Memory_size_in_bytes int64  `json:"memory_size_in_bytes"`
			Memory_size          string `json:"memory_size"` // TODO: check value
			Queries              uint64 `json:"queries"`
		}
	}
	Nodes struct {
		Count struct {
			Total       uint64 `json:"total"`
			Master_only uint64 `json:"master_only"`
			Data_only   uint64 `json:"data_only"`
			Master_data uint64 `json:"master_data"`
			Client      uint64 `json:"client"`
		}
		//Versions - not implemented
		Os struct {
			Available_processors uint64 `json:"available_processors"`
			Allocated_processors uint64 `json:"allocated_processors"`
			Mem                  struct {
				Total_in_bytes uint64 `json:"total_in_bytes"`
			}
			//Names - not implemented
		}
		Process struct {
			Cpu struct {
				Percent uint64 `json:"percent"`
			}
			Open_file_descriptors struct {
				Min float64 `json:"min"`
				Max float64 `json:"max"`
				Avg float64 `json:"avg"`
			}
		}
		Jvm struct {
			Max_uptime_in_millis uint64 `json:"max_uptime_in_millis"`
			// Versions - not implemented
			Mem struct {
				Heap_used_in_bytes uint64 `json:"heap_used_in_bytes"`
				Heap_max_in_bytes  uint64 `json:"heap_max_in_bytes"`
			}
			Threads uint64 `json:"threads"`
		}
		Fs struct {
			Total_in_bytes     uint64 `json:"total_in_bytes"`
			Free_in_bytes      uint64 `json:"free_in_bytes"`
			Available_in_bytes uint64 `json:"available_in_bytes"`
			Spins              string `json:"spins"`
		}
		// Plugins - not implemented
	}
}

func (eb *Elasticbeat) GetCLusterHealth(u url.URL) (ClusterHealth, error) {
	health := ClusterHealth{}

	res, err := http.Get(TrimSuffix(u.String(), "/") + CLUSTER_HEALTH)
	if err != nil {
		return health, err
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		return health, fmt.Errorf("HTTP%s", res.Status)
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return health, err
	}

	err = json.Unmarshal([]byte(body), &health)
	if err != nil {
		return health, err
	}
	health.Status_num = GetNumericalClusterStatus(health.Status)

	return health, nil
}

func (eb *Elasticbeat) GetCLusterStats(u url.URL) (ClusterStats, error) {
	stats := ClusterStats{}

	res, err := http.Get(TrimSuffix(u.String(), "/") + CLUSTER_STATS)
	if err != nil {
		return stats, err
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		return stats, fmt.Errorf("HTTP%s", res.Status)
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return stats, err
	}

	err = json.Unmarshal([]byte(body), &stats)
	if err != nil {
		return stats, err
	}

	stats.Status_num = GetNumericalClusterStatus(stats.Status)

	return stats, nil
}
<commit_msg>Fixing typo for min values<commit_after>package beater

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"net/url"
)

const CLUSTER_HEALTH = "/_cluster/health"
const CLUSTER_STATS = "/_cluster/stats"

type ClusterHealth struct {
	Cluster_name                     string  `json:"cluster_name"`
	Status                           string  `json:"status"`
	Status_num                       int64   `json:"status_num"`
	Timed_out                        bool    `json:"timed_out"`
	Number_of_nodes                  uint64  `json:"number_of_nodes"`
	Number_of_data_nodes             uint64  `json:"number_of_data_nodes"`
	Active_primary_shards            uint64  `json:"active_primary_shards"`
	Active_shards                    uint64  `json:"active_shards"`
	Relocating_shards                uint64  `json:"relocating_shards"`
	Initializing_shards              uint64  `json:"intializing_shards"`
	Unassigned_shards                uint64  `json:"unassigned_shards"`
	Delayed_unassigned_shards        uint64  `json:"delayed_unassigned_shards"`
	Number_of_pending_tasks          uint64  `json:"number_of_pending_tasks"`
	Number_of_in_flight_fetch        uint64  `json:"number_of_in_flight_fetch"`
	Task_max_waiting_in_queue_millis uint64  `json:"task_max_waiting_in_queue_millis"`
	Active_shards_percent_as_number  float64 `json:"active_shards_percent_as_number"`
}

type ClusterStats struct {
	Timestamp    uint64 `json:"timestamp"`
	Cluster_name string `json:"cluster_name"`
	Status       string `json:"status"`
	Status_num   int64  `json:"status_num"`
	Indices      struct {
		Count  uint64 `json:"count"`
		Shards struct {
			Total       uint64  `json:"total"`
			Primaries   uint64  `json:"primaries"`
			Replication float64 `json:"replication"`
			Index       struct {
				Shards struct {
					Min float64 `json:"min"`
					Max float64 `json:"max"`
					Avg float64 `json:"avg"`
				}
				Primaries struct {
					Min float64 `json:"min"`
					Max float64 `json:"max"`
					Avg float64 `json:"avg"`
				}
				Replication struct {
					Min float64 `json:"min"`
					Max float64 `json:"max"`
					Avg float64 `json:"avg"`
				}
			}
		}
		Docs struct {
			Count   uint64 `json:"count"`
			Deleted uint64 `json:"deleted"`
		}
		Store struct {
			Size_in_bytes           uint64 `json:"size_in_bytes"`
			Throttle_time_in_millis uint64 `json:"throttle_time_in_millis"`
		}
		Fielddata struct {
			Memory_size_in_bytes uint64 `json:"memory_size_in_bytes"`
			Evictions            uint64 `json:"evictions"`
		}
		Query_cache struct {
			Memory_size_in_bytes uint64 `json:"memory_size_in_bytes"`
			Total_count          uint64 `json:"total_count"`
			Hit_count            uint64 `json:"hit_count"`
			Miss_count           uint64 `json:"miss_count"`
			Cache_size           uint64 `json:"cache_size"`
			Cache_count          uint64 `json:"cache_count"`
			Evictions            uint64 `json:"evictions"`
		}
		Completion struct {
			Size_in_bytes uint64 `json:"size_in_bytes"`
		}
		Segments struct {
			Count                            uint64 `json:"count"`
			Memory_in_bytes                  uint64 `json:"memory_in_bytes"`
			Terms_memory_in_bytes            uint64 `json:"terms_memory_in_bytes"`
			Stored_fields_memory_in_bytes    uint64 `json:"stored_fields_memory_in_bytes"`
			Term_vectors_memory_in_bytes     uint64 `json:"term_vectors_memory_in_bytes"`
			Norms_memory_in_bytes            uint64 `json:"norms_memory_in_bytes"`
			Doc_values_memory_in_bytes       uint64 `json:"doc_values_memory_in_bytes"`
			Index_writer_memory_in_bytes     uint64 `json:"index_writer_memory_in_bytes"`
			Index_writer_max_memory_in_bytes uint64 `json:"index_writer_max_memory_in_bytes"`
			Version_map_memory_in_bytes      uint64 `json:"version_map_memory_in_bytes"`
			Fixed_bit_set_memory_in_bytes    uint64 `json:"fixed_bit_set_memory_in_bytes"`
		}
		Percolate struct {
			Total                uint64 `json:"total"`
			Time_in_millis       uint64 `json:"time_in_millis"`
			Current              uint64 `json:"current"`
			Memory_size_in_bytes int64  `json:"memory_size_in_bytes"`
			Memory_size          string `json:"memory_size"` // TODO: check value
			Queries              uint64 `json:"queries"`
		}
	}
	Nodes struct {
		Count struct {
			Total       uint64 `json:"total"`
			Master_only uint64 `json:"master_only"`
			Data_only   uint64 `json:"data_only"`
			Master_data uint64 `json:"master_data"`
			Client      uint64 `json:"client"`
		}
		//Versions - not implemented
		Os struct {
			Available_processors uint64 `json:"available_processors"`
			Allocated_processors uint64 `json:"allocated_processors"`
			Mem                  struct {
				Total_in_bytes uint64 `json:"total_in_bytes"`
			}
			//Names - not implemented
		}
		Process struct {
			Cpu struct {
				Percent uint64 `json:"percent"`
			}
			Open_file_descriptors struct {
				Min float64 `json:"min"`
				Max float64 `json:"max"`
				Avg float64 `json:"avg"`
			}
		}
		Jvm struct {
			Max_uptime_in_millis uint64 `json:"max_uptime_in_millis"`
			// Versions - not implemented
			Mem struct {
				Heap_used_in_bytes uint64 `json:"heap_used_in_bytes"`
				Heap_max_in_bytes  uint64 `json:"heap_max_in_bytes"`
			}
			Threads uint64 `json:"threads"`
		}
		Fs struct {
			Total_in_bytes     uint64 `json:"total_in_bytes"`
			Free_in_bytes      uint64 `json:"free_in_bytes"`
			Available_in_bytes uint64 `json:"available_in_bytes"`
			Spins              string `json:"spins"`
		}
		// Plugins - not implemented
	}
}

func (eb *Elasticbeat) GetCLusterHealth(u url.URL) (ClusterHealth, error) {
	health := ClusterHealth{}

	res, err := http.Get(TrimSuffix(u.String(), "/") + CLUSTER_HEALTH)
	if err != nil {
		return health, err
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		return health, fmt.Errorf("HTTP%s", res.Status)
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return health, err
	}

	err = json.Unmarshal([]byte(body), &health)
	if err != nil {
		return health, err
	}
	health.Status_num = GetNumericalClusterStatus(health.Status)

	return health, nil
}

func (eb *Elasticbeat) GetCLusterStats(u url.URL) (ClusterStats, error) {
	stats := ClusterStats{}

	res, err := http.Get(TrimSuffix(u.String(), "/") + CLUSTER_STATS)
	if err != nil {
		return stats, err
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		return stats, fmt.Errorf("HTTP%s", res.Status)
	}

	body, err := ioutil.ReadAll(res.Body)
	if err != nil {
		return stats, err
	}

	err = json.Unmarshal([]byte(body), &stats)
	if err != nil {
		return stats, err
	}

	stats.Status_num = GetNumericalClusterStatus(stats.Status)

	return stats, nil
}
<|endoftext|>


<commit_before>package hashmap

import (
	"strconv"
	"sync"
	"testing"
	"unsafe"

	"golang.org/x/sync/syncmap"
)

const benchmarkItemCount = 1 << 10 // 1024

func setupHashMap(b *testing.B) *HashMap {
	m := New()
	for i := uint64(0); i < benchmarkItemCount; i++ {
		j := uintptr(i)
		m.Set(i, unsafe.Pointer(&j))
	}

	b.ResetTimer()
	return m
}

func setupHashMapString(b *testing.B) *HashMap {
	m := New()
	for i := 0; i < benchmarkItemCount; i++ {
		s := strconv.Itoa(i)
		m.Set(s, unsafe.Pointer(&s))
	}

	b.ResetTimer()
	return m
}

func writeHashMap(m *HashMap, i uint64) {
	j := uintptr(i)
	m.Set(i, unsafe.Pointer(&j))
}

func setupHashMapHashedKey(b *testing.B) *HashMap {
	m := New()
	log := log2(uint64(benchmarkItemCount))
	for i := uint64(0); i < benchmarkItemCount; i++ {
		hash := i << (64 - log)
		j := uintptr(i)
		m.SetHashedKey(hash, unsafe.Pointer(&j))
	}

	b.ResetTimer()
	return m
}

func setupGoMap(b *testing.B) map[uint64]uint64 {
	m := make(map[uint64]uint64)
	for i := uint64(0); i < benchmarkItemCount; i++ {
		m[i] = i
	}

	b.ResetTimer()
	return m
}

func writeGoMap(m map[uint64]uint64, i uint64, mtx *sync.RWMutex) {
	mtx.Lock()
	m[i] = i
	mtx.Unlock()
}

func setupGoSyncMap(b *testing.B) *syncmap.Map {
	m := &syncmap.Map{}
	for i := uint64(0); i < benchmarkItemCount; i++ {
		m.Store(i, i)
	}

	b.ResetTimer()
	return m
}

func writeGoSyncMap(m *syncmap.Map, i uint64) {
	m.Store(i, i)
}

func setupGoMapString(b *testing.B) map[string]string {
	m := make(map[string]string)
	for i := 0; i < benchmarkItemCount; i++ {
		s := strconv.Itoa(i)
		m[s] = s
	}
	b.ResetTimer()
	return m
}

func BenchmarkReadHashMapUint(b *testing.B) {
	m := setupHashMap(b)

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m.GetUintKey(i)
				if *(*uint64)(j) != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadHashMapWithWritesUint(b *testing.B) {
	m := setupHashMap(b)

	go func() {
		for n := 0; n < b.N; n++ {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				writeHashMap(m, i)
			}
		}
	}()

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m.GetUintKey(i)
				if *(*uint64)(j) != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadHashMapString(b *testing.B) {
	m := setupHashMapString(b)

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := 0; i < benchmarkItemCount; i++ {
				s := strconv.Itoa(i)
				sVal, _ := m.GetStringKey(s)
				if *(*string)(sVal) != s {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadHashMapInterface(b *testing.B) {
	m := setupHashMap(b)

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m.Get(i)
				if *(*uint64)(j) != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadHashMapHashedKey(b *testing.B) {
	m := setupHashMapHashedKey(b)
	log := log2(uint64(benchmarkItemCount))

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				hash := i << (64 - log)
				j, _ := m.GetHashedKey(hash)
				if *(*uint64)(j) != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoMapUintUnsafe(b *testing.B) {
	m := setupGoMap(b)
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m[i]
				if j != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoMapUintMutex(b *testing.B) {
	m := setupGoMap(b)
	l := &sync.RWMutex{}
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				l.RLock()
				j, _ := m[i]
				l.RUnlock()
				if j != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoMapWithWritesUintMutex(b *testing.B) {
	m := setupGoMap(b)
	l := &sync.RWMutex{}

	go func() {
		for n := 0; n < b.N; n++ {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				writeGoMap(m, i, l)
			}
		}
	}()

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				l.RLock()
				j, _ := m[i]
				l.RUnlock()
				if j != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoSyncMapUint(b *testing.B) {
	m := setupGoSyncMap(b)
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m.Load(i)
				if j != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoSyncMapWithWritesUint(b *testing.B) {
	m := setupGoSyncMap(b)

	go func() {
		for n := 0; n < b.N; n++ {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				writeGoSyncMap(m, i)
			}
		}
	}()

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m.Load(i)
				if j != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoMapStringUnsafe(b *testing.B) {
	m := setupGoMapString(b)
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := 0; i < benchmarkItemCount; i++ {
				s := strconv.Itoa(i)
				sVal, _ := m[s]
				if s != sVal {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoMapStringMutex(b *testing.B) {
	m := setupGoMapString(b)
	l := &sync.RWMutex{}
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := 0; i < benchmarkItemCount; i++ {
				s := strconv.Itoa(i)
				l.RLock()
				sVal, _ := m[s]
				l.RUnlock()
				if s != sVal {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkWriteHashMapUint(b *testing.B) {
	m := New()

	for n := 0; n < b.N; n++ {
		for i := uint64(0); i < benchmarkItemCount; i++ {
			j := uintptr(i)
			m.Set(i, unsafe.Pointer(&j))
		}
	}
}

func BenchmarkWriteHashMapHashedKey(b *testing.B) {
	m := New()
	log := log2(uint64(benchmarkItemCount))

	for n := 0; n < b.N; n++ {
		for i := uint64(0); i < benchmarkItemCount; i++ {
			hash := i << (64 - log)
			j := uintptr(i)
			m.SetHashedKey(hash, unsafe.Pointer(&j))
		}
	}
}

func BenchmarkWriteGoMapMutexUint(b *testing.B) {
	m := make(map[uint64]uint64)
	l := &sync.RWMutex{}

	for n := 0; n < b.N; n++ {
		for i := uint64(0); i < benchmarkItemCount; i++ {
			l.Lock()
			m[i] = i
			l.Unlock()
		}
	}
}

func BenchmarkWriteGoSyncMapUint(b *testing.B) {
	m := &syncmap.Map{}

	for n := 0; n < b.N; n++ {
		for i := uint64(0); i < benchmarkItemCount; i++ {
			m.Store(i, i)
		}
	}
}

<commit_msg>go fmt<commit_after>package hashmap

import (
	"strconv"
	"sync"
	"testing"
	"unsafe"

	"golang.org/x/sync/syncmap"
)

const benchmarkItemCount = 1 << 10 // 1024

func setupHashMap(b *testing.B) *HashMap {
	m := New()
	for i := uint64(0); i < benchmarkItemCount; i++ {
		j := uintptr(i)
		m.Set(i, unsafe.Pointer(&j))
	}

	b.ResetTimer()
	return m
}

func setupHashMapString(b *testing.B) *HashMap {
	m := New()
	for i := 0; i < benchmarkItemCount; i++ {
		s := strconv.Itoa(i)
		m.Set(s, unsafe.Pointer(&s))
	}

	b.ResetTimer()
	return m
}

func writeHashMap(m *HashMap, i uint64) {
	j := uintptr(i)
	m.Set(i, unsafe.Pointer(&j))
}

func setupHashMapHashedKey(b *testing.B) *HashMap {
	m := New()
	log := log2(uint64(benchmarkItemCount))
	for i := uint64(0); i < benchmarkItemCount; i++ {
		hash := i << (64 - log)
		j := uintptr(i)
		m.SetHashedKey(hash, unsafe.Pointer(&j))
	}

	b.ResetTimer()
	return m
}

func setupGoMap(b *testing.B) map[uint64]uint64 {
	m := make(map[uint64]uint64)
	for i := uint64(0); i < benchmarkItemCount; i++ {
		m[i] = i
	}

	b.ResetTimer()
	return m
}

func writeGoMap(m map[uint64]uint64, i uint64, mtx *sync.RWMutex) {
	mtx.Lock()
	m[i] = i
	mtx.Unlock()
}

func setupGoSyncMap(b *testing.B) *syncmap.Map {
	m := &syncmap.Map{}
	for i := uint64(0); i < benchmarkItemCount; i++ {
		m.Store(i, i)
	}

	b.ResetTimer()
	return m
}

func writeGoSyncMap(m *syncmap.Map, i uint64) {
	m.Store(i, i)
}

func setupGoMapString(b *testing.B) map[string]string {
	m := make(map[string]string)
	for i := 0; i < benchmarkItemCount; i++ {
		s := strconv.Itoa(i)
		m[s] = s
	}
	b.ResetTimer()
	return m
}

func BenchmarkReadHashMapUint(b *testing.B) {
	m := setupHashMap(b)

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m.GetUintKey(i)
				if *(*uint64)(j) != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadHashMapWithWritesUint(b *testing.B) {
	m := setupHashMap(b)

	go func() {
		for n := 0; n < b.N; n++ {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				writeHashMap(m, i)
			}
		}
	}()

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m.GetUintKey(i)
				if *(*uint64)(j) != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadHashMapString(b *testing.B) {
	m := setupHashMapString(b)

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := 0; i < benchmarkItemCount; i++ {
				s := strconv.Itoa(i)
				sVal, _ := m.GetStringKey(s)
				if *(*string)(sVal) != s {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadHashMapInterface(b *testing.B) {
	m := setupHashMap(b)

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m.Get(i)
				if *(*uint64)(j) != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadHashMapHashedKey(b *testing.B) {
	m := setupHashMapHashedKey(b)
	log := log2(uint64(benchmarkItemCount))

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				hash := i << (64 - log)
				j, _ := m.GetHashedKey(hash)
				if *(*uint64)(j) != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoMapUintUnsafe(b *testing.B) {
	m := setupGoMap(b)
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m[i]
				if j != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoMapUintMutex(b *testing.B) {
	m := setupGoMap(b)
	l := &sync.RWMutex{}
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				l.RLock()
				j, _ := m[i]
				l.RUnlock()
				if j != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoMapWithWritesUintMutex(b *testing.B) {
	m := setupGoMap(b)
	l := &sync.RWMutex{}

	go func() {
		for n := 0; n < b.N; n++ {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				writeGoMap(m, i, l)
			}
		}
	}()

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				l.RLock()
				j, _ := m[i]
				l.RUnlock()
				if j != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoSyncMapUint(b *testing.B) {
	m := setupGoSyncMap(b)
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m.Load(i)
				if j != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoSyncMapWithWritesUint(b *testing.B) {
	m := setupGoSyncMap(b)

	go func() {
		for n := 0; n < b.N; n++ {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				writeGoSyncMap(m, i)
			}
		}
	}()

	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := uint64(0); i < benchmarkItemCount; i++ {
				j, _ := m.Load(i)
				if j != i {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoMapStringUnsafe(b *testing.B) {
	m := setupGoMapString(b)
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := 0; i < benchmarkItemCount; i++ {
				s := strconv.Itoa(i)
				sVal, _ := m[s]
				if s != sVal {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkReadGoMapStringMutex(b *testing.B) {
	m := setupGoMapString(b)
	l := &sync.RWMutex{}
	b.RunParallel(func(pb *testing.PB) {
		for pb.Next() {
			for i := 0; i < benchmarkItemCount; i++ {
				s := strconv.Itoa(i)
				l.RLock()
				sVal, _ := m[s]
				l.RUnlock()
				if s != sVal {
					b.Fail()
				}
			}
		}
	})
}

func BenchmarkWriteHashMapUint(b *testing.B) {
	m := New()

	for n := 0; n < b.N; n++ {
		for i := uint64(0); i < benchmarkItemCount; i++ {
			j := uintptr(i)
			m.Set(i, unsafe.Pointer(&j))
		}
	}
}

func BenchmarkWriteHashMapHashedKey(b *testing.B) {
	m := New()
	log := log2(uint64(benchmarkItemCount))

	for n := 0; n < b.N; n++ {
		for i := uint64(0); i < benchmarkItemCount; i++ {
			hash := i << (64 - log)
			j := uintptr(i)
			m.SetHashedKey(hash, unsafe.Pointer(&j))
		}
	}
}

func BenchmarkWriteGoMapMutexUint(b *testing.B) {
	m := make(map[uint64]uint64)
	l := &sync.RWMutex{}

	for n := 0; n < b.N; n++ {
		for i := uint64(0); i < benchmarkItemCount; i++ {
			l.Lock()
			m[i] = i
			l.Unlock()
		}
	}
}

func BenchmarkWriteGoSyncMapUint(b *testing.B) {
	m := &syncmap.Map{}

	for n := 0; n < b.N; n++ {
		for i := uint64(0); i < benchmarkItemCount; i++ {
			m.Store(i, i)
		}
	}
}
<|endoftext|>