<commit_before>package operators

import (
	"context"

	g "github.com/onsi/ginkgo"
	o "github.com/onsi/gomega"
	"github.com/stretchr/objx"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/dynamic"
	e2e "k8s.io/kubernetes/test/e2e/framework"
	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"

	"github.com/openshift/origin/test/extended/util/ibmcloud"
)

var _ = g.Describe("[sig-cluster-lifecycle][Feature:Machines][Early] Managed cluster should", func() {
	defer g.GinkgoRecover()

	g.It("have same number of Machines and Nodes", func() {
		if e2e.TestContext.Provider == ibmcloud.ProviderName {
			e2eskipper.Skipf("IBM Cloud clusters do not contain machineset resources")
		}

		cfg, err := e2e.LoadConfig()
		o.Expect(err).NotTo(o.HaveOccurred())
		c, err := e2e.LoadClientset()
		o.Expect(err).NotTo(o.HaveOccurred())
		dc, err := dynamic.NewForConfig(cfg)
		o.Expect(err).NotTo(o.HaveOccurred())

		g.By("getting Node list")
		nodeList, err := c.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
		o.Expect(err).NotTo(o.HaveOccurred())
		nodeItems := nodeList.Items

		g.By("getting Machine list")
		machineClient := dc.Resource(schema.GroupVersionResource{Group: "machine.openshift.io", Resource: "machines", Version: "v1beta1"})
		obj, err := machineClient.List(context.Background(), metav1.ListOptions{})
		o.Expect(err).NotTo(o.HaveOccurred())
		machineList := objx.Map(obj.UnstructuredContent())
		machineItems := objects(machineList.Get("items"))

		g.By("ensure number of Machines and Nodes are equal")
		o.Expect(len(nodeItems)).To(o.Equal(len(machineItems)))
	})
})
<commit_msg>test: extended: only run new machines test against platforms that have machines<commit_after>package operators

import (
	"context"

	g "github.com/onsi/ginkgo"
	o "github.com/onsi/gomega"
	"github.com/stretchr/objx"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/apimachinery/pkg/runtime/schema"
	"k8s.io/client-go/dynamic"
	e2e "k8s.io/kubernetes/test/e2e/framework"
	e2eskipper "k8s.io/kubernetes/test/e2e/framework/skipper"
)

var _ = g.Describe("[sig-cluster-lifecycle][Feature:Machines][Early] Managed cluster should", func() {
	defer g.GinkgoRecover()

	g.It("have same number of Machines and Nodes", func() {
		if e2e.TestContext.Provider != "aws" &&
			e2e.TestContext.Provider != "gce" &&
			e2e.TestContext.Provider != "openstack" &&
			e2e.TestContext.Provider != "azure" {
			e2eskipper.Skipf("clusters on platform '%s' do not contain machineset resources", e2e.TestContext.Provider)
		}

		cfg, err := e2e.LoadConfig()
		o.Expect(err).NotTo(o.HaveOccurred())
		c, err := e2e.LoadClientset()
		o.Expect(err).NotTo(o.HaveOccurred())
		dc, err := dynamic.NewForConfig(cfg)
		o.Expect(err).NotTo(o.HaveOccurred())

		g.By("getting Node list")
		nodeList, err := c.CoreV1().Nodes().List(context.Background(), metav1.ListOptions{})
		o.Expect(err).NotTo(o.HaveOccurred())
		nodeItems := nodeList.Items

		g.By("getting Machine list")
		machineClient := dc.Resource(schema.GroupVersionResource{Group: "machine.openshift.io", Resource: "machines", Version: "v1beta1"})
		obj, err := machineClient.List(context.Background(), metav1.ListOptions{})
		o.Expect(err).NotTo(o.HaveOccurred())
		machineList := objx.Map(obj.UnstructuredContent())
		machineItems := objects(machineList.Get("items"))

		g.By("ensure number of Machines and Nodes are equal")
		o.Expect(len(nodeItems)).To(o.Equal(len(machineItems)))
	})
})
<|endoftext|>


<commit_before>package tests

import (
	"bytes"
	"fmt"
	"net/url"
	//"os"
	"path"
	"strings"
	"testing"

	apimodels "github.com/fnproject/fn/api/models"
	apiutils "github.com/fnproject/fn/test/fn-api-tests"
	sdkmodels "github.com/fnproject/fn_go/models"
)

func LB() (string, error) {
	lbURL := "http://127.0.0.1:8081"

	u, err := url.Parse(lbURL)
	if err != nil {
		return "", err
	}
	return u.Host, nil
}

func TestCanExecuteFunction(t *testing.T) {
	s := apiutils.SetupHarness()
	s.GivenAppExists(t, &sdkmodels.App{Name: s.AppName})
	defer s.Cleanup()

	rt := s.BasicRoute()
	rt.Type = "sync"

	s.GivenRouteExists(t, s.AppName, rt)

	lb, err := LB()
	if err != nil {
		t.Fatalf("Got unexpected error: %v", err)
	}
	u := url.URL{
		Scheme: "http",
		Host:   lb,
	}
	u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)

	content := &bytes.Buffer{}
	output := &bytes.Buffer{}
	_, err = apiutils.CallFN(u.String(), content, output, "POST", []string{})
	if err != nil {
		t.Errorf("Got unexpected error: %v", err)
	}
	expectedOutput := "Hello World!\n"
	if !strings.Contains(expectedOutput, output.String()) {
		t.Errorf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
	}
}

func TestBasicConcurrentExecution(t *testing.T) {
	SystemTweaker().ChangeNodeCapacities(512)
	defer SystemTweaker().RestoreInitialNodeCapacities()

	s := apiutils.SetupHarness()

	s.GivenAppExists(t, &sdkmodels.App{Name: s.AppName})
	defer s.Cleanup()

	rt := s.BasicRoute()
	rt.Type = "sync"

	s.GivenRouteExists(t, s.AppName, rt)

	lb, err := LB()
	if err != nil {
		t.Fatalf("Got unexpected error: %v", err)
	}
	u := url.URL{
		Scheme: "http",
		Host:   lb,
	}
	u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)

	results := make(chan error)
	concurrentFuncs := 10
	for i := 0; i < concurrentFuncs; i++ {
		go func() {
			content := &bytes.Buffer{}
			output := &bytes.Buffer{}
			_, err = apiutils.CallFN(u.String(), content, output, "POST", []string{})
			if err != nil {
				results <- fmt.Errorf("Got unexpected error: %v", err)
				return
			}
			expectedOutput := "Hello World!\n"
			if !strings.Contains(expectedOutput, output.String()) {
				results <- fmt.Errorf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
				return
			}
			results <- nil
		}()
	}
	for i := 0; i < concurrentFuncs; i++ {
		err := <-results
		if err != nil {
			t.Errorf("Error in basic concurrency execution test: %v", err)
		}
	}

}

func TestSaturatedSystem(t *testing.T) {
	// Set the capacity to 0 so we always look out of capacity.
	SystemTweaker().ChangeNodeCapacities(0)
	defer SystemTweaker().RestoreInitialNodeCapacities()

	s := apiutils.SetupHarness()

	s.GivenAppExists(t, &sdkmodels.App{Name: s.AppName})
	defer s.Cleanup()

	rt := s.BasicRoute()
	rt.Type = "sync"

	s.GivenRouteExists(t, s.AppName, rt)

	lb, err := LB()
	if err != nil {
		t.Fatalf("Got unexpected error: %v", err)
	}
	u := url.URL{
		Scheme: "http",
		Host:   lb,
	}
	u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)

	content := &bytes.Buffer{}
	output := &bytes.Buffer{}
	_, err = apiutils.CallFN(u.String(), content, output, "POST", []string{})
	if err != nil {
		if err != apimodels.ErrCallTimeoutServerBusy {
			t.Errorf("Got unexpected error: %v", err)
		}
	}
	expectedOutput := "{\"error\":{\"message\":\"Timed out - server too busy\"}}\n"
	if !strings.Contains(expectedOutput, output.String()) {
		t.Errorf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
	}
}
<commit_msg>Fix assert check in system tests (#952)<commit_after>package tests

import (
	"bytes"
	"fmt"
	"net/url"
	//"os"
	"path"
	"strings"
	"testing"

	apimodels "github.com/fnproject/fn/api/models"
	apiutils "github.com/fnproject/fn/test/fn-api-tests"
	sdkmodels "github.com/fnproject/fn_go/models"
)

func LB() (string, error) {
	lbURL := "http://127.0.0.1:8081"

	u, err := url.Parse(lbURL)
	if err != nil {
		return "", err
	}
	return u.Host, nil
}

func TestCanExecuteFunction(t *testing.T) {
	s := apiutils.SetupHarness()
	s.GivenAppExists(t, &sdkmodels.App{Name: s.AppName})
	defer s.Cleanup()

	rt := s.BasicRoute()
	rt.Type = "sync"

	s.GivenRouteExists(t, s.AppName, rt)

	lb, err := LB()
	if err != nil {
		t.Fatalf("Got unexpected error: %v", err)
	}
	u := url.URL{
		Scheme: "http",
		Host:   lb,
	}
	u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)

	content := &bytes.Buffer{}
	output := &bytes.Buffer{}
	_, err = apiutils.CallFN(u.String(), content, output, "POST", []string{})
	if err != nil {
		t.Errorf("Got unexpected error: %v", err)
	}
	expectedOutput := "Hello World!\n"
	actual := output.String()
	if !strings.Contains(expectedOutput, actual) || len(expectedOutput) != len(actual) {
		t.Errorf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
	}
}

func TestBasicConcurrentExecution(t *testing.T) {
	SystemTweaker().ChangeNodeCapacities(512)
	defer SystemTweaker().RestoreInitialNodeCapacities()

	s := apiutils.SetupHarness()

	s.GivenAppExists(t, &sdkmodels.App{Name: s.AppName})
	defer s.Cleanup()

	rt := s.BasicRoute()
	rt.Type = "sync"

	s.GivenRouteExists(t, s.AppName, rt)

	lb, err := LB()
	if err != nil {
		t.Fatalf("Got unexpected error: %v", err)
	}
	u := url.URL{
		Scheme: "http",
		Host:   lb,
	}
	u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)

	results := make(chan error)
	concurrentFuncs := 10
	for i := 0; i < concurrentFuncs; i++ {
		go func() {
			content := &bytes.Buffer{}
			output := &bytes.Buffer{}
			_, err = apiutils.CallFN(u.String(), content, output, "POST", []string{})
			if err != nil {
				results <- fmt.Errorf("Got unexpected error: %v", err)
				return
			}
			expectedOutput := "Hello World!\n"
			actual := output.String()
			if !strings.Contains(expectedOutput, actual) || len(expectedOutput) != len(actual) {
				results <- fmt.Errorf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
				return
			}
			results <- nil
		}()
	}
	for i := 0; i < concurrentFuncs; i++ {
		err := <-results
		if err != nil {
			t.Errorf("Error in basic concurrency execution test: %v", err)
		}
	}

}

func TestSaturatedSystem(t *testing.T) {
	// Set the capacity to 0 so we always look out of capacity.
	SystemTweaker().ChangeNodeCapacities(0)
	defer SystemTweaker().RestoreInitialNodeCapacities()

	s := apiutils.SetupHarness()

	s.GivenAppExists(t, &sdkmodels.App{Name: s.AppName})
	defer s.Cleanup()

	rt := s.BasicRoute()
	rt.Type = "sync"

	s.GivenRouteExists(t, s.AppName, rt)

	lb, err := LB()
	if err != nil {
		t.Fatalf("Got unexpected error: %v", err)
	}
	u := url.URL{
		Scheme: "http",
		Host:   lb,
	}
	u.Path = path.Join(u.Path, "r", s.AppName, s.RoutePath)

	content := &bytes.Buffer{}
	output := &bytes.Buffer{}
	_, err = apiutils.CallFN(u.String(), content, output, "POST", []string{})
	if err != nil {
		if err != apimodels.ErrCallTimeoutServerBusy {
			t.Errorf("Got unexpected error: %v", err)
		}
	}
	expectedOutput := "{\"error\":{\"message\":\"Timed out - server too busy\"}}\n"
	actual := output.String()
	if !strings.Contains(expectedOutput, actual) || len(expectedOutput) != len(actual) {
		t.Errorf("Assertion error.\n\tExpected: %v\n\tActual: %v", expectedOutput, output.String())
	}
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/gbl08ma/disturbancesmlx/dataobjects"
	"github.com/gbl08ma/disturbancesmlx/scraper"
)

var annStore AnnouncementStore

// AnnouncementStore implements dataobjects.AnnouncementStore
type AnnouncementStore struct {
	scrapers map[string]scraper.AnnouncementScraper
}

// AddScraper registers all sources provided by this scraper
func (as *AnnouncementStore) AddScraper(scraper scraper.AnnouncementScraper) {
	for _, source := range scraper.Sources() {
		as.scrapers[source] = scraper
	}
}

// AllAnnouncements gets all announcements from all sources, unsorted
func (as *AnnouncementStore) AllAnnouncements() []*dataobjects.Announcement {
	ann := []*dataobjects.Announcement{}
	for source, scraper := range as.scrapers {
		ann = append(ann, scraper.Announcements(source)...)
	}
	return ann
}

// SourceAnnouncements gets all announcements from a specific source
func (as *AnnouncementStore) SourceAnnouncements(source string) []*dataobjects.Announcement {
	ann, ok := as.scrapers[source]
	if !ok {
		return []*dataobjects.Announcement{}
	}
	return ann.Announcements(source)
}
<commit_msg>Fix assignment to nil map<commit_after>package main

import (
	"github.com/gbl08ma/disturbancesmlx/dataobjects"
	"github.com/gbl08ma/disturbancesmlx/scraper"
)

var annStore AnnouncementStore

// AnnouncementStore implements dataobjects.AnnouncementStore
type AnnouncementStore struct {
	scrapers map[string]scraper.AnnouncementScraper
}

// AddScraper registers all sources provided by this scraper
func (as *AnnouncementStore) AddScraper(s scraper.AnnouncementScraper) {
	if as.scrapers == nil {
		as.scrapers = make(map[string]scraper.AnnouncementScraper)
	}
	for _, source := range s.Sources() {
		as.scrapers[source] = s
	}
}

// AllAnnouncements gets all announcements from all sources, unsorted
func (as *AnnouncementStore) AllAnnouncements() []*dataobjects.Announcement {
	ann := []*dataobjects.Announcement{}
	for source, scraper := range as.scrapers {
		ann = append(ann, scraper.Announcements(source)...)
	}
	return ann
}

// SourceAnnouncements gets all announcements from a specific source
func (as *AnnouncementStore) SourceAnnouncements(source string) []*dataobjects.Announcement {
	ann, ok := as.scrapers[source]
	if !ok {
		return []*dataobjects.Announcement{}
	}
	return ann.Announcements(source)
}
<|endoftext|>


<commit_before>package checkerlution

import (
	"encoding/json"
	"fmt"
	"github.com/couchbaselabs/logg"
	"github.com/nu7hatch/gouuid"
	"github.com/tleyden/dsallings-couch-go"
	ng "github.com/tleyden/neurgo"
	"io"
	"strings"
	"time"
)

const (
	SERVER_URL   = "http://localhost:4984/checkers"
	GAME_DOC_ID  = "game:checkers"
	VOTES_DOC_ID = "votes:checkers"
	RED_TEAM     = 0
	BLUE_TEAM    = 1
)

type Game struct {
	cortex               *ng.Cortex
	currentGameState     GameStateVector
	gameState            GameState
	currentPossibleMove  ValidMoveCortexInput
	latestActuatorOutput []float64
	ourTeamId            int
	db                   couch.Database
	user                 User
}

type Changes map[string]interface{}

func NewGame(ourTeamId int) *Game {
	game := &Game{ourTeamId: ourTeamId}
	return game
}

// Follow the changes feed and on each change callback
// call game.handleChanges() which will drive the game
func (game *Game) GameLoop() {

	game.InitGame()

	curSinceValue := "0"

	handleChange := func(reader io.Reader) string {
		changes := decodeChanges(reader)
		game.handleChanges(changes)
		curSinceValue = calculateNextSinceValue(curSinceValue, changes)
		time.Sleep(time.Second * 5)
		return curSinceValue
	}

	options := Changes{"since": "0"}
	game.db.Changes(handleChange, options)

}

func (game *Game) updateUserGameNumber(gameState GameState) {
	gameNumberChanged := (game.gameState.Number != gameState.Number)
	if gameNumberChanged {
		game.user.GameNumber = gameState.Number
		newRevision, err := game.db.Edit(game.user)
		if err != nil {
			logg.LogError(err)
			return
		}
		logg.LogTo("MAIN", "user update, rev: %v", newRevision)
	}

}

// - make sure one of the changes is a game, if not, ignore it
// - get the latest game document
// - if it's not our turn, do nothing
// - if it is our turn
//   - parse out the required data structures needed to pass to cortex
//   - call cortex to calculate next move
//   - make next move by inserting a new revision of votes doc
func (game *Game) handleChanges(changes Changes) {
	logg.LogTo("DEBUG", "handleChanges called with %v", changes)
	gameDocChanged := game.checkGameDocInChanges(changes)
	if gameDocChanged {
		gameState, err := game.fetchLatestGameState()
		game.updateUserGameNumber(gameState)
		game.gameState = gameState
		if err != nil {
			logg.LogError(err)
			return
		}
		logg.LogTo("DEBUG", "gameState: %v", gameState)
		if isOurTurn := game.isOurTurn(gameState); !isOurTurn {
			logg.LogTo("DEBUG", "It's not our turn, ignoring changes")
			return
		}

		gameStateVector := game.extractGameStateVector(gameState)

		logg.LogTo("DEBUG", "gameStateVector: %v", gameStateVector)

		possibleMoves := game.extractPossibleMoves(gameState)

		if len(possibleMoves) == 0 {
			logg.LogTo("MAIN", "No possibleMoves, ignoring changes")
			return
		}

		logg.LogTo("DEBUG", "possibleMoves: %v", possibleMoves)

		bestMove := game.ChooseBestMove(gameStateVector, possibleMoves)

		logg.LogTo("DEBUG", "bestMove: %v", bestMove)

		game.PostChosenMove(bestMove)

	}

}

func (game Game) extractPossibleMoves(gameState GameState) []ValidMoveCortexInput {

	moves := make([]ValidMoveCortexInput, 0)

	ourTeam := gameState.Teams[game.ourTeamId]

	for pieceIndex, piece := range ourTeam.Pieces {
		piece.PieceId = pieceIndex
		for _, validMove := range piece.ValidMoves {
			moveInput := NewValidMoveCortexInput(validMove, piece)
			moves = append(moves, moveInput)
		}
	}

	return moves
}

func (game Game) opponentTeamId() int {
	switch game.ourTeamId {
	case RED_TEAM:
		return BLUE_TEAM
	default:
		return RED_TEAM
	}
}

func (game Game) extractGameStateVector(gameState GameState) GameStateVector {
	gameStateVector := NewGameStateVector()
	gameStateVector.loadFromGameState(gameState, game.ourTeamId)
	return gameStateVector
}

func (game Game) isOurTurn(gameState GameState) bool {
	return gameState.ActiveTeam == game.ourTeamId
}

func (game Game) checkGameDocInChanges(changes Changes) bool {
	foundGameDoc := false
	changeResultsRaw := changes["results"]
	changeResults := changeResultsRaw.([]interface{})
	for _, changeResultRaw := range changeResults {
		changeResult := changeResultRaw.(map[string]interface{})
		docIdRaw := changeResult["id"]
		docId := docIdRaw.(string)
		if strings.Contains(docId, GAME_DOC_ID) {
			foundGameDoc = true
		}
	}
	return foundGameDoc
}

func (game Game) fetchLatestGameState() (gameState GameState, err error) {
	gameStateFetched := &GameState{}
	err = game.db.Retrieve(GAME_DOC_ID, gameStateFetched)
	if err == nil {
		gameState = *gameStateFetched
	}
	return
}

func (game *Game) InitGame() {
	game.CreateNeurgoCortex()
	cortex := game.cortex
	cortex.Run()
	game.InitDbConnection()
	game.CreateRemoteUser()
}

func (game *Game) CreateRemoteUser() {

	u4, err := uuid.NewV4()
	if err != nil {
		logg.LogPanic("Error generating uuid", err)
	}

	user := &User{
		Id:     fmt.Sprintf("user:%s", u4),
		TeamId: game.ourTeamId,
	}
	newId, newRevision, err := game.db.Insert(user)
	logg.LogTo("MAIN", "Inserted new user %v rev %v", newId, newRevision)

	user.Rev = newRevision
	game.user = *user

}

func (game *Game) InitDbConnection() {
	db, error := couch.Connect(SERVER_URL)
	if error != nil {
		logg.LogPanic("Error connecting to %v: %v", SERVER_URL, error)
	}
	game.db = db
}

func (game *Game) ChooseBestMove(gameStateVector GameStateVector, possibleMoves []ValidMoveCortexInput) (bestMove ValidMoveCortexInput) {

	// Todo: the code below is an implementation of a single MoveChooser
	// but an interface should be designed so this is pluggable

	game.currentGameState = gameStateVector
	logg.LogTo("MAIN", "gameStateVector: %v", gameStateVector)

	var bestMoveRating []float64
	bestMoveRating = []float64{-1000000000}

	for _, move := range possibleMoves {

		logg.LogTo("MAIN", "feed possible move to cortex: %v", move)

		// present it to the neural net
		game.currentPossibleMove = move
		game.cortex.SyncSensors()
		game.cortex.SyncActuators()

		logg.LogTo("MAIN", "done sync'ing actuators")

		logg.LogTo("MAIN", "actuator output %v bestMoveRating: %v", game.latestActuatorOutput[0], bestMoveRating[0])
		if game.latestActuatorOutput[0] > bestMoveRating[0] {
			logg.LogTo("MAIN", "actuator output > bestMoveRating")
			bestMove = move
			bestMoveRating[0] = game.latestActuatorOutput[0]
		} else {
			logg.LogTo("MAIN", "actuator output < bestMoveRating, ignoring")
		}

	}
	return

}

func (game *Game) PostChosenMove(move ValidMoveCortexInput) {

	logg.LogTo("MAIN", "post chosen move: %v", move.validMove)

	preMoveSleepSeconds := game.calculatePreMoveSleepSeconds()

	logg.LogTo("MAIN", "sleep %v (s) before posting move", preMoveSleepSeconds)

	time.Sleep(time.Second * time.Duration(preMoveSleepSeconds))

	if len(move.validMove.Locations) == 0 {
		logg.LogTo("MAIN", "invalid move, ignoring: %v", move.validMove)
	}

	u4, err := uuid.NewV4()
	if err != nil {
		logg.LogPanic("Error generating uuid", err)
	}

	votes := &OutgoingVotes{}
	votes.Id = fmt.Sprintf("vote:%s", u4)
	votes.Turn = game.gameState.Turn
	votes.PieceId = move.validMove.PieceId
	votes.TeamId = game.ourTeamId
	votes.GameId = game.gameState.Number

	// TODO: this is actually a bug, because if there is a
	// double jump it will only send the first jump move
	endLocation := move.validMove.Locations[0]
	locations := []int{move.validMove.StartLocation, endLocation}
	votes.Locations = locations

	newId, newRevision, err := game.db.Insert(votes)

	logg.LogTo("MAIN", "newId: %v, newRevision: %v err: %v", newId, newRevision, err)
	if err != nil {
		logg.LogError(err)
		return
	}

}

func (game *Game) CreateNeurgoCortex() {

	nodeId := ng.NewCortexId("cortex")
	game.cortex = &ng.Cortex{
		NodeId: nodeId,
	}
	game.CreateSensors()
	game.CreateActuator()
	game.CreateNeuron()
	game.ConnectNodes()
}

func (game *Game) ConnectNodes() {

	cortex := game.cortex

	cortex.Init()

	// connect sensors -> neuron(s)
	for _, sensor := range cortex.Sensors {
		for _, neuron := range cortex.Neurons {
			sensor.ConnectOutbound(neuron)
			weights := ng.RandomWeights(sensor.VectorLength)
			neuron.ConnectInboundWeighted(sensor, weights)
		}
	}

	// connect neuron to actuator
	for _, neuron := range cortex.Neurons {
		for _, actuator := range cortex.Actuators {
			neuron.ConnectOutbound(actuator)
			actuator.ConnectInbound(neuron)
		}
	}

}

func (game *Game) CreateNeuron() {
	neuron := &ng.Neuron{
		ActivationFunction: ng.EncodableSigmoid(),
		NodeId:             ng.NewNeuronId("Neuron", 0.25),
		Bias:               ng.RandomBias(),
	}
	game.cortex.SetNeurons([]*ng.Neuron{neuron})
}

func (game *Game) CreateActuator() {

	actuatorNodeId := ng.NewActuatorId("Actuator", 0.5)
	actuatorFunc := func(outputs []float64) {
		logg.LogTo("MAIN", "actuator func called with: %v", outputs)
		game.latestActuatorOutput = outputs
		game.cortex.SyncChan <- actuatorNodeId // TODO: this should be in actuator itself, not in this function
	}
	actuator := &ng.Actuator{
		NodeId:           actuatorNodeId,
		VectorLength:     1,
		ActuatorFunction: actuatorFunc,
	}
	game.cortex.SetActuators([]*ng.Actuator{actuator})

}

func (game *Game) CreateSensors() {

	sensorLayer := 0.0

	sensorFuncGameState := func(syncCounter int) []float64 {
		logg.LogTo("MAIN", "sensor func game state called")
		return game.currentGameState
	}
	sensorGameStateNodeId := ng.NewSensorId("SensorGameState", sensorLayer)
	sensorGameState := &ng.Sensor{
		NodeId:         sensorGameStateNodeId,
		VectorLength:   32,
		SensorFunction: sensorFuncGameState,
	}

	sensorFuncPossibleMove := func(syncCounter int) []float64 {
		logg.LogTo("MAIN", "sensor func possible move called")
		return game.currentPossibleMove.VectorRepresentation()
	}
	sensorPossibleMoveNodeId := ng.NewSensorId("SensorPossibleMove", sensorLayer)
	sensorPossibleMove := &ng.Sensor{
		NodeId:         sensorPossibleMoveNodeId,
		VectorLength:   5, // start_location, is_king, final_location, will_be_king, amt_would_capture
		SensorFunction: sensorFuncPossibleMove,
	}
	game.cortex.SetSensors([]*ng.Sensor{sensorGameState, sensorPossibleMove})

}

func decodeChanges(reader io.Reader) Changes {
	changes := make(Changes)
	decoder := json.NewDecoder(reader)
	decoder.Decode(&changes)
	return changes
}

func calculateNextSinceValue(curSinceValue string, changes Changes) string {
	lastSeq := changes["last_seq"]
	lastSeqAsString := lastSeq.(string)
	if lastSeq != nil && len(lastSeqAsString) > 0 {
		return lastSeqAsString
	}
	return curSinceValue
}

func (game *Game) calculatePreMoveSleepSeconds() float64 {

	// we don't want to make a move "too soon", so lets
	// cap the minimum amount we sleep at 10% of the move interval
	minSleep := float64(game.gameState.MoveInterval) * 0.10

	// likewise, don't want to cut it to close to the timeout
	maxSleep := float64(game.gameState.MoveInterval) * 0.90

	return ng.RandomInRange(minSleep, maxSleep)

}
<commit_msg>move method<commit_after>package checkerlution

import (
	"encoding/json"
	"fmt"
	"github.com/couchbaselabs/logg"
	"github.com/nu7hatch/gouuid"
	"github.com/tleyden/dsallings-couch-go"
	ng "github.com/tleyden/neurgo"
	"io"
	"strings"
	"time"
)

const (
	SERVER_URL   = "http://localhost:4984/checkers"
	GAME_DOC_ID  = "game:checkers"
	VOTES_DOC_ID = "votes:checkers"
	RED_TEAM     = 0
	BLUE_TEAM    = 1
)

type Game struct {
	cortex               *ng.Cortex
	currentGameState     GameStateVector
	gameState            GameState
	currentPossibleMove  ValidMoveCortexInput
	latestActuatorOutput []float64
	ourTeamId            int
	db                   couch.Database
	user                 User
}

type Changes map[string]interface{}

func NewGame(ourTeamId int) *Game {
	game := &Game{ourTeamId: ourTeamId}
	return game
}

// Follow the changes feed and on each change callback
// call game.handleChanges() which will drive the game
func (game *Game) GameLoop() {

	game.InitGame()

	curSinceValue := "0"

	handleChange := func(reader io.Reader) string {
		changes := decodeChanges(reader)
		game.handleChanges(changes)
		curSinceValue = calculateNextSinceValue(curSinceValue, changes)
		time.Sleep(time.Second * 5)
		return curSinceValue
	}

	options := Changes{"since": "0"}
	game.db.Changes(handleChange, options)

}

// - make sure one of the changes is a game, if not, ignore it
// - get the latest game document
// - if it's not our turn, do nothing
// - if it is our turn
//   - parse out the required data structures needed to pass to cortex
//   - call cortex to calculate next move
//   - make next move by inserting a new revision of votes doc
func (game *Game) handleChanges(changes Changes) {
	logg.LogTo("DEBUG", "handleChanges called with %v", changes)
	gameDocChanged := game.checkGameDocInChanges(changes)
	if gameDocChanged {
		gameState, err := game.fetchLatestGameState()
		game.updateUserGameNumber(gameState)
		game.gameState = gameState
		if err != nil {
			logg.LogError(err)
			return
		}
		logg.LogTo("DEBUG", "gameState: %v", gameState)
		if isOurTurn := game.isOurTurn(gameState); !isOurTurn {
			logg.LogTo("DEBUG", "It's not our turn, ignoring changes")
			return
		}

		gameStateVector := game.extractGameStateVector(gameState)

		logg.LogTo("DEBUG", "gameStateVector: %v", gameStateVector)

		possibleMoves := game.extractPossibleMoves(gameState)

		if len(possibleMoves) == 0 {
			logg.LogTo("MAIN", "No possibleMoves, ignoring changes")
			return
		}

		logg.LogTo("DEBUG", "possibleMoves: %v", possibleMoves)

		bestMove := game.ChooseBestMove(gameStateVector, possibleMoves)

		logg.LogTo("DEBUG", "bestMove: %v", bestMove)

		game.PostChosenMove(bestMove)

	}

}

func (game *Game) updateUserGameNumber(gameState GameState) {
	gameNumberChanged := (game.gameState.Number != gameState.Number)
	if gameNumberChanged {
		game.user.GameNumber = gameState.Number
		newRevision, err := game.db.Edit(game.user)
		if err != nil {
			logg.LogError(err)
			return
		}
		logg.LogTo("MAIN", "user update, rev: %v", newRevision)
	}

}

func (game Game) extractPossibleMoves(gameState GameState) []ValidMoveCortexInput {

	moves := make([]ValidMoveCortexInput, 0)

	ourTeam := gameState.Teams[game.ourTeamId]

	for pieceIndex, piece := range ourTeam.Pieces {
		piece.PieceId = pieceIndex
		for _, validMove := range piece.ValidMoves {
			moveInput := NewValidMoveCortexInput(validMove, piece)
			moves = append(moves, moveInput)
		}
	}

	return moves
}

func (game Game) opponentTeamId() int {
	switch game.ourTeamId {
	case RED_TEAM:
		return BLUE_TEAM
	default:
		return RED_TEAM
	}
}

func (game Game) extractGameStateVector(gameState GameState) GameStateVector {
	gameStateVector := NewGameStateVector()
	gameStateVector.loadFromGameState(gameState, game.ourTeamId)
	return gameStateVector
}

func (game Game) isOurTurn(gameState GameState) bool {
	return gameState.ActiveTeam == game.ourTeamId
}

func (game Game) checkGameDocInChanges(changes Changes) bool {
	foundGameDoc := false
	changeResultsRaw := changes["results"]
	changeResults := changeResultsRaw.([]interface{})
	for _, changeResultRaw := range changeResults {
		changeResult := changeResultRaw.(map[string]interface{})
		docIdRaw := changeResult["id"]
		docId := docIdRaw.(string)
		if strings.Contains(docId, GAME_DOC_ID) {
			foundGameDoc = true
		}
	}
	return foundGameDoc
}

func (game Game) fetchLatestGameState() (gameState GameState, err error) {
	gameStateFetched := &GameState{}
	err = game.db.Retrieve(GAME_DOC_ID, gameStateFetched)
	if err == nil {
		gameState = *gameStateFetched
	}
	return
}

func (game *Game) InitGame() {
	game.CreateNeurgoCortex()
	cortex := game.cortex
	cortex.Run()
	game.InitDbConnection()
	game.CreateRemoteUser()
}

func (game *Game) CreateRemoteUser() {

	u4, err := uuid.NewV4()
	if err != nil {
		logg.LogPanic("Error generating uuid", err)
	}

	user := &User{
		Id:     fmt.Sprintf("user:%s", u4),
		TeamId: game.ourTeamId,
	}
	newId, newRevision, err := game.db.Insert(user)
	logg.LogTo("MAIN", "Inserted new user %v rev %v", newId, newRevision)

	user.Rev = newRevision
	game.user = *user

}

func (game *Game) InitDbConnection() {
	db, error := couch.Connect(SERVER_URL)
	if error != nil {
		logg.LogPanic("Error connecting to %v: %v", SERVER_URL, error)
	}
	game.db = db
}

func (game *Game) ChooseBestMove(gameStateVector GameStateVector, possibleMoves []ValidMoveCortexInput) (bestMove ValidMoveCortexInput) {

	// Todo: the code below is an implementation of a single MoveChooser
	// but an interface should be designed so this is pluggable

	game.currentGameState = gameStateVector
	logg.LogTo("MAIN", "gameStateVector: %v", gameStateVector)

	var bestMoveRating []float64
	bestMoveRating = []float64{-1000000000}

	for _, move := range possibleMoves {

		logg.LogTo("MAIN", "feed possible move to cortex: %v", move)

		// present it to the neural net
		game.currentPossibleMove = move
		game.cortex.SyncSensors()
		game.cortex.SyncActuators()

		logg.LogTo("MAIN", "done sync'ing actuators")

		logg.LogTo("MAIN", "actuator output %v bestMoveRating: %v", game.latestActuatorOutput[0], bestMoveRating[0])
		if game.latestActuatorOutput[0] > bestMoveRating[0] {
			logg.LogTo("MAIN", "actuator output > bestMoveRating")
			bestMove = move
			bestMoveRating[0] = game.latestActuatorOutput[0]
		} else {
			logg.LogTo("MAIN", "actuator output < bestMoveRating, ignoring")
		}

	}
	return

}

func (game *Game) PostChosenMove(move ValidMoveCortexInput) {

	logg.LogTo("MAIN", "post chosen move: %v", move.validMove)

	preMoveSleepSeconds := game.calculatePreMoveSleepSeconds()

	logg.LogTo("MAIN", "sleep %v (s) before posting move", preMoveSleepSeconds)

	time.Sleep(time.Second * time.Duration(preMoveSleepSeconds))

	if len(move.validMove.Locations) == 0 {
		logg.LogTo("MAIN", "invalid move, ignoring: %v", move.validMove)
	}

	u4, err := uuid.NewV4()
	if err != nil {
		logg.LogPanic("Error generating uuid", err)
	}

	votes := &OutgoingVotes{}
	votes.Id = fmt.Sprintf("vote:%s", u4)
	votes.Turn = game.gameState.Turn
	votes.PieceId = move.validMove.PieceId
	votes.TeamId = game.ourTeamId
	votes.GameId = game.gameState.Number

	// TODO: this is actually a bug, because if there is a
	// double jump it will only send the first jump move
	endLocation := move.validMove.Locations[0]
	locations := []int{move.validMove.StartLocation, endLocation}
	votes.Locations = locations

	newId, newRevision, err := game.db.Insert(votes)

	logg.LogTo("MAIN", "newId: %v, newRevision: %v err: %v", newId, newRevision, err)
	if err != nil {
		logg.LogError(err)
		return
	}

}

func (game *Game) CreateNeurgoCortex() {

	nodeId := ng.NewCortexId("cortex")
	game.cortex = &ng.Cortex{
		NodeId: nodeId,
	}
	game.CreateSensors()
	game.CreateActuator()
	game.CreateNeuron()
	game.ConnectNodes()
}

func (game *Game) ConnectNodes() {

	cortex := game.cortex

	cortex.Init()

	// connect sensors -> neuron(s)
	for _, sensor := range cortex.Sensors {
		for _, neuron := range cortex.Neurons {
			sensor.ConnectOutbound(neuron)
			weights := ng.RandomWeights(sensor.VectorLength)
			neuron.ConnectInboundWeighted(sensor, weights)
		}
	}

	// connect neuron to actuator
	for _, neuron := range cortex.Neurons {
		for _, actuator := range cortex.Actuators {
			neuron.ConnectOutbound(actuator)
			actuator.ConnectInbound(neuron)
		}
	}

}

func (game *Game) CreateNeuron() {
	neuron := &ng.Neuron{
		ActivationFunction: ng.EncodableSigmoid(),
		NodeId:             ng.NewNeuronId("Neuron", 0.25),
		Bias:               ng.RandomBias(),
	}
	game.cortex.SetNeurons([]*ng.Neuron{neuron})
}

func (game *Game) CreateActuator() {

	actuatorNodeId := ng.NewActuatorId("Actuator", 0.5)
	actuatorFunc := func(outputs []float64) {
		logg.LogTo("MAIN", "actuator func called with: %v", outputs)
		game.latestActuatorOutput = outputs
		game.cortex.SyncChan <- actuatorNodeId // TODO: this should be in actuator itself, not in this function
	}
	actuator := &ng.Actuator{
		NodeId:           actuatorNodeId,
		VectorLength:     1,
		ActuatorFunction: actuatorFunc,
	}
	game.cortex.SetActuators([]*ng.Actuator{actuator})

}

func (game *Game) CreateSensors() {

	sensorLayer := 0.0

	sensorFuncGameState := func(syncCounter int) []float64 {
		logg.LogTo("MAIN", "sensor func game state called")
		return game.currentGameState
	}
	sensorGameStateNodeId := ng.NewSensorId("SensorGameState", sensorLayer)
	sensorGameState := &ng.Sensor{
		NodeId:         sensorGameStateNodeId,
		VectorLength:   32,
		SensorFunction: sensorFuncGameState,
	}

	sensorFuncPossibleMove := func(syncCounter int) []float64 {
		logg.LogTo("MAIN", "sensor func possible move called")
		return game.currentPossibleMove.VectorRepresentation()
	}
	sensorPossibleMoveNodeId := ng.NewSensorId("SensorPossibleMove", sensorLayer)
	sensorPossibleMove := &ng.Sensor{
		NodeId:         sensorPossibleMoveNodeId,
		VectorLength:   5, // start_location, is_king, final_location, will_be_king, amt_would_capture
		SensorFunction: sensorFuncPossibleMove,
	}
	game.cortex.SetSensors([]*ng.Sensor{sensorGameState, sensorPossibleMove})

}

func decodeChanges(reader io.Reader) Changes {
	changes := make(Changes)
	decoder := json.NewDecoder(reader)
	decoder.Decode(&changes)
	return changes
}

func calculateNextSinceValue(curSinceValue string, changes Changes) string {
	lastSeq := changes["last_seq"]
	lastSeqAsString := lastSeq.(string)
	if lastSeq != nil && len(lastSeqAsString) > 0 {
		return lastSeqAsString
	}
	return curSinceValue
}

func (game *Game) calculatePreMoveSleepSeconds() float64 {

	// we don't want to make a move "too soon", so lets
	// cap the minimum amount we sleep at 10% of the move interval
	minSleep := float64(game.gameState.MoveInterval) * 0.10

	// likewise, don't want to cut it to close to the timeout
	maxSleep := float64(game.gameState.MoveInterval) * 0.90

	return ng.RandomInRange(minSleep, maxSleep)

}
<|endoftext|>


<commit_before>package forwarder

import (
	"time"

	log "github.com/cihub/seelog"
)

func Start(traceChan chan []byte, numWorkers, bufferSize int) {

	log.Infof("Starting %v forwarders with buffer size of %v", numWorkers, bufferSize)

	for i := 0; i < numWorkers; i++ {
		f := &forwarder{
			id: i,
			ch: traceChan,
		}

		// Do something useful
		go f.work()
	}

}

type forwarder struct {
	id int
	ch chan []byte
}

func (f *forwarder) work() {

	log.Debugf("[Forwarder %v] started", f.id)

	// var b []byte
	var i int
	tick := time.NewTicker(5 * time.Second)

	for {
		// log.Debugf("[Forwarder %v] Waiting for message", f.id)
		select {
		case <-f.ch:
			i++
			// log.Debugf("[Forwarder %v] Received message", f.id)
		case <-tick.C:
			log.Debugf("[Forwarder %v] Processed %v messages", f.id, i)
			i = 0
		}
	}
}
<commit_msg>Store the buffer length, and send on traces when the buffer fills<commit_after>package forwarder

import (
	"time"

	log "github.com/cihub/seelog"
)

func Start(traceChan chan []byte, numWorkers, bufferSize int) {

	log.Infof("Starting %v forwarders with buffer size of %v", numWorkers, bufferSize)

	for i := 0; i < numWorkers; i++ {
		f := &forwarder{
			id:            i,
			ch:            traceChan,
			messageBuffer: make([][]byte, 0, bufferSize),
			bufferSize:    bufferSize,
		}

		// Do something useful
		go f.work()
	}

}

type forwarder struct {
	id            int
	ch            chan []byte
	messageBuffer [][]byte
	bufferSize    int
}

func (f *forwarder) work() {

	log.Debugf("[Forwarder %v] started", f.id)

	var b []byte
	var i int

	metricsTick := time.NewTicker(5 * time.Second)

	for {
		// log.Debugf("[Forwarder %v] Waiting for message", f.id)
		select {
		case b = <-f.ch:
			i++
			// log.Debugf("[Forwarder %v] Received message", f.id)

			// Add message to our buffer
			f.messageBuffer = append(f.messageBuffer, b)

			// Forward on if we're at our buffer size
			if len(f.messageBuffer) >= f.bufferSize {
				f.send()
			}
		case <-metricsTick.C:
			log.Debugf("[Forwarder %v] Processed %v messages", f.id, i)
			// i = 0
		}
	}
}

func (f *forwarder) send() {

	log.Infof("[Forwarder %v] Sent %v messages", f.id, len(f.messageBuffer))

	// Empty the buffer
	f.messageBuffer = nil
}
<|endoftext|>


<commit_before>package mackerelplugin

import (
	"math"
	"testing"
	"time"
)

func TestCalcDiff(t *testing.T) {
	var mp MackerelPlugin

	val1 := 10.0
	val2 := 0.0
	now := time.Now()
	last := time.Unix(now.Unix()-10, 0)

	diff, err := mp.calcDiff(val1, now, val2, last)
	if diff != 60 {
		t.Errorf("calcDiff: %f should be %f", diff, 60.0)
	}
	if err != nil {
		t.Error("calcDiff causes an error")
	}
}

func TestCalcDiffWithUInt32WithReset(t *testing.T) {
	var mp MackerelPlugin

	val := uint32(10)
	now := time.Now()
	lastval := uint32(12345)
	last := time.Unix(now.Unix()-60, 0)

	diff, err := mp.calcDiffUint32(val, now, lastval, last, 10)
	if err != nil {
	} else {
		t.Error("calcDiffUint32 with counter reset should cause an error: %f", diff)
	}
}

func TestCalcDiffWithUInt32Overflow(t *testing.T) {
	var mp MackerelPlugin

	val := uint32(10)
	now := time.Now()
	lastval := math.MaxUint32 - uint32(10)
	last := time.Unix(now.Unix()-60, 0)

	diff, err := mp.calcDiffUint32(val, now, lastval, last, 10)
	if diff != 21.0 {
		t.Errorf("calcDiff: last: %d, now: %d, %f should be %f", val, lastval, diff, 21.0)
	}
	if err != nil {
		t.Error("calcDiff causes an error")
	}
}

func TestCalcDiffWithUInt64Overflow(t *testing.T) {
	var mp MackerelPlugin

	val := uint64(10)
	now := time.Now()
	lastval := math.MaxUint64 - uint64(10)
	last := time.Unix(now.Unix()-60, 0)

	diff, err := mp.calcDiffUint64(val, now, lastval, last, 10)
	if diff != 21.0 {
		t.Errorf("calcDiff: last: %d, now: %d, %f should be %f", val, lastval, diff, 21.0)
	}
	if err != nil {
		t.Error("calcDiff causes an error")
	}
}
<commit_msg>add some tests<commit_after>package mackerelplugin

import (
	"bytes"
	"math"
	"testing"
	"time"
)

func TestCalcDiff(t *testing.T) {
	var mp MackerelPlugin

	val1 := 10.0
	val2 := 0.0
	now := time.Now()
	last := time.Unix(now.Unix()-10, 0)

	diff, err := mp.calcDiff(val1, now, val2, last)
	if diff != 60 {
		t.Errorf("calcDiff: %f should be %f", diff, 60.0)
	}
	if err != nil {
		t.Error("calcDiff causes an error")
	}
}

func TestCalcDiffWithUInt32WithReset(t *testing.T) {
	var mp MackerelPlugin

	val := uint32(10)
	now := time.Now()
	lastval := uint32(12345)
	last := time.Unix(now.Unix()-60, 0)

	diff, err := mp.calcDiffUint32(val, now, lastval, last, 10)
	if err != nil {
	} else {
		t.Errorf("calcDiffUint32 with counter reset should cause an error: %f", diff)
	}
}

func TestCalcDiffWithUInt32Overflow(t *testing.T) {
	var mp MackerelPlugin

	val := uint32(10)
	now := time.Now()
	lastval := math.MaxUint32 - uint32(10)
	last := time.Unix(now.Unix()-60, 0)

	diff, err := mp.calcDiffUint32(val, now, lastval, last, 10)
	if diff != 21.0 {
		t.Errorf("calcDiff: last: %d, now: %d, %f should be %f", val, lastval, diff, 21.0)
	}
	if err != nil {
		t.Error("calcDiff causes an error")
	}
}

func TestCalcDiffWithUInt64WithReset(t *testing.T) {
	var mp MackerelPlugin

	val := uint64(10)
	now := time.Now()
	lastval := uint64(12345)
	last := time.Unix(now.Unix()-60, 0)

	diff, err := mp.calcDiffUint64(val, now, lastval, last, 10)
	if err != nil {
	} else {
		t.Errorf("calcDiffUint64 with counter reset should cause an error: %f", diff)
	}
}

func TestCalcDiffWithUInt64Overflow(t *testing.T) {
	var mp MackerelPlugin

	val := uint64(10)
	now := time.Now()
	lastval := math.MaxUint64 - uint64(10)
	last := time.Unix(now.Unix()-60, 0)

	diff, err := mp.calcDiffUint64(val, now, lastval, last, 10)
	if diff != 21.0 {
		t.Errorf("calcDiff: last: %d, now: %d, %f should be %f", val, lastval, diff, 21.0)
	}
	if err != nil {
		t.Error("calcDiff causes an error")
	}
}

func TestPrintValueUint32(t *testing.T) {
	var mp MackerelPlugin
	s := new(bytes.Buffer)
	var now = time.Unix(1437227240, 0)
	mp.printValue(s, "test", uint32(10), now)

	expected := []byte("test\t10\t1437227240\n")

	if bytes.Compare(expected, s.Bytes()) != 0 {
		t.Fatalf("not matched, expected: %s, got: %s", expected, s)
	}
}

func TestPrintValueUint64(t *testing.T) {
	var mp MackerelPlugin
	s := new(bytes.Buffer)
	var now = time.Unix(1437227240, 0)
	mp.printValue(s, "test", uint64(10), now)

	expected := []byte("test\t10\t1437227240\n")

	if bytes.Compare(expected, s.Bytes()) != 0 {
		t.Fatalf("not matched, expected: %s, got: %s", expected, s)
	}
}

func TestPrintValueFloat64(t *testing.T) {
	var mp MackerelPlugin
	s := new(bytes.Buffer)
	var now = time.Unix(1437227240, 0)
	mp.printValue(s, "test", float64(10.0), now)

	expected := []byte("test\t10.000000\t1437227240\n")

	if bytes.Compare(expected, s.Bytes()) != 0 {
		t.Fatalf("not matched, expected: %s, got: %s", expected, s)
	}
}
<|endoftext|>


<commit_before>package system

import (
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
)

type Shell struct {
	coverage   bool
	gobin      string
	reports    string
	extraFlags []string
}

func (self *Shell) GoTest(directory, packageName string) (output string, err error) {
	output, err = self.compileDependencies(directory)
	if err == nil {
		output, err = self.goTest(directory, packageName)
	}
	return
}

func (self *Shell) compileDependencies(directory string) (output string, err error) {
	return self.execute(directory, self.gobin, "test", "-i")
}

func (self *Shell) goTest(directory, packageName string) (output string, err error) {
	reportFilename := strings.Replace(packageName, string(os.PathSeparator), "-", -1)
	reportPath := filepath.Join(self.reports, reportFilename)
	profile := reportPath + ".txt"
	output, err = self.runWithCoverage(directory, profile)

	if err != nil && self.coverage {
		output, err = self.runWithoutCoverage(directory)
	} else if self.coverage {
		self.generateCoverageReports(directory, profile, reportPath+".html")
	}
	return
}

func (self *Shell) runWithCoverage(directory, profile string) (string, error) {
	arguments := []string{"test", "-v", "-timeout=-42s", "-covermode=set", "-coverprofile=" + profile}
	arguments = append(arguments, self.extraFlags...)
	return self.execute(directory, self.gobin, arguments...)
}
func (self *Shell) runWithoutCoverage(directory string) (string, error) {
	arguments := []string{"test", "-v", "-timeout=-42s"}
	arguments = append(arguments, self.extraFlags...)
	return self.execute(directory, self.gobin, arguments...)
}

func (self *Shell) generateCoverageReports(directory, profile, html string) {
	self.execute(directory, self.gobin, "tool", "cover", "-html="+profile, "-o", html)
}

func (self *Shell) execute(directory, name string, args ...string) (output string, err error) {
	command := exec.Command(name, args...)
	command.Dir = directory
	rawOutput, err := command.CombinedOutput()
	output = string(rawOutput)
	return
}

func (self *Shell) Getenv(key string) string {
	return os.Getenv(key)
}

func (self *Shell) Setenv(key, value string) error {
	if self.Getenv(key) != value {
		return os.Setenv(key, value)
	}
	return nil
}

func NewShell(gobin string, extraFlags string, cover bool, reports string) *Shell {
	self := new(Shell)
	self.gobin = gobin
	self.extraFlags = strings.Split(extraFlags, " ")
	self.reports = reports
	if cover && goVersion_1_2_orGreater() {
		self.coverage = true
	}
	return self
}

func goVersion_1_2_orGreater() bool {
	version := runtime.Version() // 'go1.2....'
	major, minor := version[2], version[4]
	return major >= byte('1') && minor >= byte('2')
}
<commit_msg>We now ensure that the coverage report directory is created.<commit_after>package system

import (
	"log"
	"os"
	"os/exec"
	"path/filepath"
	"runtime"
	"strings"
)

type Shell struct {
	coverage   bool
	gobin      string
	reports    string
	extraFlags []string
}

func (self *Shell) GoTest(directory, packageName string) (output string, err error) {
	output, err = self.compileDependencies(directory)
	if err == nil {
		output, err = self.goTest(directory, packageName)
	}
	return
}

func (self *Shell) compileDependencies(directory string) (output string, err error) {
	return self.execute(directory, self.gobin, "test", "-i")
}

func (self *Shell) goTest(directory, packageName string) (output string, err error) {
	reportFilename := strings.Replace(packageName, string(os.PathSeparator), "-", -1)
	reportPath := filepath.Join(self.reports, reportFilename)
	profile := reportPath + ".txt"
	output, err = self.runWithCoverage(directory, profile)

	if err != nil && self.coverage {
		output, err = self.runWithoutCoverage(directory)
	} else if self.coverage {
		self.generateCoverageReports(directory, profile, reportPath+".html")
	}
	return
}

func (self *Shell) runWithCoverage(directory, profile string) (string, error) {
	arguments := []string{"test", "-v", "-timeout=-42s", "-covermode=set", "-coverprofile=" + profile}
	arguments = append(arguments, self.extraFlags...)
	return self.execute(directory, self.gobin, arguments...)
}
func (self *Shell) runWithoutCoverage(directory string) (string, error) {
	arguments := []string{"test", "-v", "-timeout=-42s"}
	arguments = append(arguments, self.extraFlags...)
	return self.execute(directory, self.gobin, arguments...)
}

func (self *Shell) generateCoverageReports(directory, profile, html string) {
	self.execute(directory, self.gobin, "tool", "cover", "-html="+profile, "-o", html)
}

func (self *Shell) execute(directory, name string, args ...string) (output string, err error) {
	command := exec.Command(name, args...)
	command.Dir = directory
	rawOutput, err := command.CombinedOutput()
	output = string(rawOutput)
	return
}

func (self *Shell) Getenv(key string) string {
	return os.Getenv(key)
}

func (self *Shell) Setenv(key, value string) error {
	if self.Getenv(key) != value {
		return os.Setenv(key, value)
	}
	return nil
}

func NewShell(gobin string, extraFlags string, cover bool, reports string) *Shell {
	self := new(Shell)
	self.gobin = gobin
	self.extraFlags = strings.Split(extraFlags, " ")
	self.reports = reports
	self.coverage = cover && goVersion_1_2_orGreater() && ensureReportDirectoryExists(self.reports)
	return self
}

func goVersion_1_2_orGreater() bool {
	version := runtime.Version() // 'go1.2....'
	major, minor := version[2], version[4]
	return major >= byte('1') && minor >= byte('2')
}

func ensureReportDirectoryExists(reports string) bool {
	if exists(reports) {
		return true
	}

	if err := os.Mkdir(reports, 0755); err == nil {
		return true
	}

	log.Printf(ReportDirectoryUnavailable, reports)
	return false
}

func exists(path string) bool {
	_, err := os.Stat(path)
	if err == nil {
		return true
	}
	if os.IsNotExist(err) {
		return false
	}
	return false
}

const ReportDirectoryUnavailable = "Could not find or create the coverage report directory (at: '%s'). You probably won't see any coverage statistics...\n"
<|endoftext|>


<commit_before>// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed 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 gcs

// A cache mapping from name to most recent known record for the object of that
// name. External synchronization must be provided.
type StatCache interface {
	// Insert an entry for the given object record. The entry will not replace any
	// entry with a newer generation number, and will not be available after the
	// supplied expiration time.
	Insert(o *storage.Object, expiration time.Time)

	// Erase the entry for the given object name, if any.
	Erase(name string)

	// Return the current entry for the given name, or nil if none. Use the
	// supplied time to decide whether entries have expired.
	LookUp(name string, now time.Time) (o *storage.Object)
}
<commit_msg>Fixed some build errors.<commit_after>// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed 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 gcs

import "time"

// A cache mapping from name to most recent known record for the object of that
// name. External synchronization must be provided.
type StatCache interface {
	// Insert an entry for the given object record. The entry will not replace any
	// entry with a newer generation number, and will not be available after the
	// supplied expiration time.
	Insert(o *Object, expiration time.Time)

	// Erase the entry for the given object name, if any.
	Erase(name string)

	// Return the current entry for the given name, or nil if none. Use the
	// supplied time to decide whether entries have expired.
	LookUp(name string, now time.Time) (o *Object)
}
<|endoftext|>


<commit_before>// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package size defines an event for the dimensions, physical resolution and
// orientation of the app's window.
//
// See the golang.org/x/mobile/app package for details on the event model.
package size // import "golang.org/x/mobile/event/size"

import (
	"image"

	"golang.org/x/mobile/geom"
)

// Event holds the dimensions, physical resolution and orientation of the app's
// window.
type Event struct {
	// WidthPx and HeightPx are the window's dimensions in pixels.
	WidthPx, HeightPx int

	// WidthPt and HeightPt are the window's dimensions in points (1/72 of an
	// inch).
	WidthPt, HeightPt geom.Pt

	// PixelsPerPt is the window's physical resolution. It is the number of
	// pixels in a single geom.Pt, from the golang.org/x/mobile/geom package.
	//
	// There are a wide variety of pixel densities in existing phones and
	// tablets, so apps should be written to expect various non-integer
	// PixelsPerPt values. In general, work in geom.Pt.
	PixelsPerPt float32

	// Orientation is the orientation of the device screen.
	Orientation Orientation
}

// Size returns the window's size in pixels, at the time this size event was
// sent.
func (e *Event) Size() image.Point {
	return image.Point{e.WidthPx, e.HeightPx}
}

// Bounds returns the window's bounds in pixels, at the time this size event
// was sent.
//
// The top-left pixel is always (0, 0). The bottom-right pixel is given by the
// width and height.
func (e *Event) Bounds() image.Rectangle {
	return image.Rectangle{Max: image.Point{e.WidthPx, e.HeightPx}}
}

// Orientation is the orientation of the device screen.
type Orientation int

const (
	// OrientationUnknown means device orientation cannot be determined.
	//
	// Equivalent on Android to Configuration.ORIENTATION_UNKNOWN
	// and on iOS to:
	//	UIDeviceOrientationUnknown
	//	UIDeviceOrientationFaceUp
	//	UIDeviceOrientationFaceDown
	OrientationUnknown Orientation = iota

	// OrientationPortrait is a device oriented so it is tall and thin.
	//
	// Equivalent on Android to Configuration.ORIENTATION_PORTRAIT
	// and on iOS to:
	//	UIDeviceOrientationPortrait
	//	UIDeviceOrientationPortraitUpsideDown
	OrientationPortrait

	// OrientationLandscape is a device oriented so it is short and wide.
	//
	// Equivalent on Android to Configuration.ORIENTATION_LANDSCAPE
	// and on iOS to:
	//	UIDeviceOrientationLandscapeLeft
	//	UIDeviceOrientationLandscapeRight
	OrientationLandscape
)
<commit_msg>event/size: make methods take values, not pointers.<commit_after>// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Package size defines an event for the dimensions, physical resolution and
// orientation of the app's window.
//
// See the golang.org/x/mobile/app package for details on the event model.
package size // import "golang.org/x/mobile/event/size"

import (
	"image"

	"golang.org/x/mobile/geom"
)

// Event holds the dimensions, physical resolution and orientation of the app's
// window.
type Event struct {
	// WidthPx and HeightPx are the window's dimensions in pixels.
	WidthPx, HeightPx int

	// WidthPt and HeightPt are the window's dimensions in points (1/72 of an
	// inch).
	WidthPt, HeightPt geom.Pt

	// PixelsPerPt is the window's physical resolution. It is the number of
	// pixels in a single geom.Pt, from the golang.org/x/mobile/geom package.
	//
	// There are a wide variety of pixel densities in existing phones and
	// tablets, so apps should be written to expect various non-integer
	// PixelsPerPt values. In general, work in geom.Pt.
	PixelsPerPt float32

	// Orientation is the orientation of the device screen.
	Orientation Orientation
}

// Size returns the window's size in pixels, at the time this size event was
// sent.
func (e Event) Size() image.Point {
	return image.Point{e.WidthPx, e.HeightPx}
}

// Bounds returns the window's bounds in pixels, at the time this size event
// was sent.
//
// The top-left pixel is always (0, 0). The bottom-right pixel is given by the
// width and height.
func (e Event) Bounds() image.Rectangle {
	return image.Rectangle{Max: image.Point{e.WidthPx, e.HeightPx}}
}

// Orientation is the orientation of the device screen.
type Orientation int

const (
	// OrientationUnknown means device orientation cannot be determined.
	//
	// Equivalent on Android to Configuration.ORIENTATION_UNKNOWN
	// and on iOS to:
	//	UIDeviceOrientationUnknown
	//	UIDeviceOrientationFaceUp
	//	UIDeviceOrientationFaceDown
	OrientationUnknown Orientation = iota

	// OrientationPortrait is a device oriented so it is tall and thin.
	//
	// Equivalent on Android to Configuration.ORIENTATION_PORTRAIT
	// and on iOS to:
	//	UIDeviceOrientationPortrait
	//	UIDeviceOrientationPortraitUpsideDown
	OrientationPortrait

	// OrientationLandscape is a device oriented so it is short and wide.
	//
	// Equivalent on Android to Configuration.ORIENTATION_LANDSCAPE
	// and on iOS to:
	//	UIDeviceOrientationLandscapeLeft
	//	UIDeviceOrientationLandscapeRight
	OrientationLandscape
)
<|endoftext|>


<commit_before><commit_msg>v23.x.lib: add envutil to get machine's architecture.<commit_after><|endoftext|>


<commit_before>package mppuma

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"net/http"
	"time"

	mp "github.com/mackerelio/go-mackerel-plugin-helper"
)

var graphdef = map[string]mp.Graphs{
	"workers": {
		Label: "Puma workers",
		Unit:  "integer",
		Metrics: []mp.Metrics{
			{Name: "workers", Label: "Active workers", Diff: false},
		},
	},
	"phase": {
		Label: "Puma phase",
		Unit:  "integer",
		Metrics: []mp.Metrics{
			{Name: "phase", Label: "Active phase", Diff: false},
		},
	},
}

// PumaPlugin mackerel plugin for Puma
type PumaPlugin struct {
	Prefix string
	Host   string
	Port   string
	Token  string
}

// Stats is convered from /stats json
type Stats struct {
	Workers       int `json:"workers"`
	Phase         int `json:"phase"`
	BootedWorkers int `json:"booted_workers"`
	OldWorkers    int `json:"old_workers"`
	WorkerStatus  []struct {
		Pid         int       `json:"pid"`
		Index       int       `json:"index"`
		Phase       int       `json:"phase"`
		Booted      bool      `json:"booted"`
		LastCheckin time.Time `json:"last_checkin"`
		LastStatus  struct {
			Backlog int `json:"backlog"`
			Running int `json:"running"`
		} `json:"last_status"`
	} `json:"worker_status"`
}

// GCStats is convered from /gc-stats json
type GCStats struct {
	Count                               int `json:"count"`
	HeapAllocatedPages                  int `json:"heap_allocated_pages"`
	HeapSortedLength                    int `json:"heap_sorted_length"`
	HeapAllocatablePages                int `json:"heap_allocatable_pages"`
	HeapAvailableSlots                  int `json:"heap_available_slots"`
	HeapLiveSlots                       int `json:"heap_live_slots"`
	HeapFreeSlots                       int `json:"heap_free_slots"`
	HeapFinalSlots                      int `json:"heap_final_slots"`
	HeapMarkedSlots                     int `json:"heap_marked_slots"`
	HeapEdenPages                       int `json:"heap_eden_pages"`
	HeapTombPages                       int `json:"heap_tomb_pages"`
	TotalAllocatedPages                 int `json:"total_allocated_pages"`
	TotalFreedPages                     int `json:"total_freed_pages"`
	TotalAllocatedObjects               int `json:"total_allocated_objects"`
	TotalFreedObjects                   int `json:"total_freed_objects"`
	MallocIncreaseBytes                 int `json:"malloc_increase_bytes"`
	MallocIncreaseBytesLimit            int `json:"malloc_increase_bytes_limit"`
	MinorGcCount                        int `json:"minor_gc_count"`
	MajorGcCount                        int `json:"major_gc_count"`
	RememberedWbUnprotectedObjects      int `json:"remembered_wb_unprotected_objects"`
	RememberedWbUnprotectedObjectsLimit int `json:"remembered_wb_unprotected_objects_limit"`
	OldObjects                          int `json:"old_objects"`
	OldObjectsLimit                     int `json:"old_objects_limit"`
	OldmallocIncreaseBytes              int `json:"oldmalloc_increase_bytes"`
	OldmallocIncreaseBytesLimit         int `json:"oldmalloc_increase_bytes_limit"`
}

// FetchMetrics interface for mackerelplugin
func (p PumaPlugin) FetchMetrics() (map[string]interface{}, error) {
	ret := make(map[string]interface{})

	// Featch /stats
	uri := fmt.Sprintf("http://%s:%s/%s?token=%s", p.Host, p.Port, "stats", p.Token)
	resp, err := http.Get(uri)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return nil, errors.New(resp.Status)
	}

	var stats Stats
	if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil {
		return nil, err
	}

	ret["workers"] = float64(stats.Workers)
	ret["phase"] = float64(stats.Phase)
	return ret, nil

}

// GraphDefinition interface for mackerelplugin
func (p PumaPlugin) GraphDefinition() map[string]mp.Graphs {
	return graphdef
}

// MetricKeyPrefix interface for PluginWithPrefix
func (p PumaPlugin) MetricKeyPrefix() string {
	if p.Prefix == "" {
		p.Prefix = "puma"
	}
	return p.Prefix
}

// Do the plugin
func Do() {
	var (
		optPrefix   = flag.String("metric-key-prefix", "puma", "Metric key prefix")
		optHost     = flag.String("host", "127.0.0.1", "The bind url to use for the control server")
		optPort     = flag.String("port", "9293", "The bind port to use for the control server")
		optToken    = flag.String("token", "", "The token to use as authentication for the control server")
		optTempfile = flag.String("tempfile", "", "Temp file name")
	)
	flag.Parse()

	var puma PumaPlugin
	puma.Prefix = *optPrefix
	puma.Host = *optHost
	puma.Port = *optPort
	puma.Token = *optToken

	helper := mp.NewMackerelPlugin(puma)
	helper.Tempfile = *optTempfile
	helper.Run()
}
<commit_msg>Devide function<commit_after>package mppuma

import (
	"encoding/json"
	"errors"
	"flag"
	"fmt"
	"net/http"
	"time"

	mp "github.com/mackerelio/go-mackerel-plugin-helper"
)

var graphdef = map[string]mp.Graphs{
	"workers": {
		Label: "Puma workers",
		Unit:  "integer",
		Metrics: []mp.Metrics{
			{Name: "workers", Label: "Active workers", Diff: false},
		},
	},
	"phase": {
		Label: "Puma phase",
		Unit:  "integer",
		Metrics: []mp.Metrics{
			{Name: "phase", Label: "Active phase", Diff: false},
		},
	},
}

// PumaPlugin mackerel plugin for Puma
type PumaPlugin struct {
	Prefix string
	Host   string
	Port   string
	Token  string
}

// Stats is convered from /stats json
type Stats struct {
	Workers       int `json:"workers"`
	Phase         int `json:"phase"`
	BootedWorkers int `json:"booted_workers"`
	OldWorkers    int `json:"old_workers"`
	WorkerStatus  []struct {
		Pid         int       `json:"pid"`
		Index       int       `json:"index"`
		Phase       int       `json:"phase"`
		Booted      bool      `json:"booted"`
		LastCheckin time.Time `json:"last_checkin"`
		LastStatus  struct {
			Backlog int `json:"backlog"`
			Running int `json:"running"`
		} `json:"last_status"`
	} `json:"worker_status"`
}

// GCStats is convered from /gc-stats json
type GCStats struct {
	Count                               int `json:"count"`
	HeapAllocatedPages                  int `json:"heap_allocated_pages"`
	HeapSortedLength                    int `json:"heap_sorted_length"`
	HeapAllocatablePages                int `json:"heap_allocatable_pages"`
	HeapAvailableSlots                  int `json:"heap_available_slots"`
	HeapLiveSlots                       int `json:"heap_live_slots"`
	HeapFreeSlots                       int `json:"heap_free_slots"`
	HeapFinalSlots                      int `json:"heap_final_slots"`
	HeapMarkedSlots                     int `json:"heap_marked_slots"`
	HeapEdenPages                       int `json:"heap_eden_pages"`
	HeapTombPages                       int `json:"heap_tomb_pages"`
	TotalAllocatedPages                 int `json:"total_allocated_pages"`
	TotalFreedPages                     int `json:"total_freed_pages"`
	TotalAllocatedObjects               int `json:"total_allocated_objects"`
	TotalFreedObjects                   int `json:"total_freed_objects"`
	MallocIncreaseBytes                 int `json:"malloc_increase_bytes"`
	MallocIncreaseBytesLimit            int `json:"malloc_increase_bytes_limit"`
	MinorGcCount                        int `json:"minor_gc_count"`
	MajorGcCount                        int `json:"major_gc_count"`
	RememberedWbUnprotectedObjects      int `json:"remembered_wb_unprotected_objects"`
	RememberedWbUnprotectedObjectsLimit int `json:"remembered_wb_unprotected_objects_limit"`
	OldObjects                          int `json:"old_objects"`
	OldObjectsLimit                     int `json:"old_objects_limit"`
	OldmallocIncreaseBytes              int `json:"oldmalloc_increase_bytes"`
	OldmallocIncreaseBytesLimit         int `json:"oldmalloc_increase_bytes_limit"`
}

// Fetch /stats
func (p PumaPlugin) fetchStats() (*Stats, error) {

	var stats Stats

	uri := fmt.Sprintf("http://%s:%s/%s?token=%s", p.Host, p.Port, "stats", p.Token)
	resp, err := http.Get(uri)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode != 200 {
		return nil, errors.New(resp.Status)
	}
	if err := json.NewDecoder(resp.Body).Decode(&stats); err != nil {
		return nil, err
	}

	return &stats, nil
}

// FetchMetrics interface for mackerelplugin
func (p PumaPlugin) FetchMetrics() (map[string]interface{}, error) {
	ret := make(map[string]interface{})

	stats, err := p.fetchStats()

	if err != nil {
		return nil, err
	}

	ret["workers"] = float64(stats.Workers)
	ret["phase"] = float64(stats.Phase)
	return ret, nil

}

// GraphDefinition interface for mackerelplugin
func (p PumaPlugin) GraphDefinition() map[string]mp.Graphs {
	return graphdef
}

// MetricKeyPrefix interface for PluginWithPrefix
func (p PumaPlugin) MetricKeyPrefix() string {
	if p.Prefix == "" {
		p.Prefix = "puma"
	}
	return p.Prefix
}

// Do the plugin
func Do() {
	var (
		optPrefix   = flag.String("metric-key-prefix", "puma", "Metric key prefix")
		optHost     = flag.String("host", "127.0.0.1", "The bind url to use for the control server")
		optPort     = flag.String("port", "9293", "The bind port to use for the control server")
		optToken    = flag.String("token", "", "The token to use as authentication for the control server")
		optTempfile = flag.String("tempfile", "", "Temp file name")
	)
	flag.Parse()

	var puma PumaPlugin
	puma.Prefix = *optPrefix
	puma.Host = *optHost
	puma.Port = *optPort
	puma.Token = *optToken

	helper := mp.NewMackerelPlugin(puma)
	helper.Tempfile = *optTempfile
	helper.Run()
}
<|endoftext|>


<commit_before>// Pings DHT nodes with the given network addresses.
package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"os"
	"time"

	"github.com/anacrolix/torrent/dht"
)

type pingResponse struct {
	addr  string
	krpc  dht.Msg
	msgOk bool
	rtt   time.Duration
}

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	timeout := flag.Duration("timeout", -1, "maximum timeout")
	flag.Parse()
	pingStrAddrs := flag.Args()
	if len(pingStrAddrs) == 0 {
		os.Stderr.WriteString("u must specify addrs of nodes to ping e.g. router.bittorrent.com:6881\n")
		os.Exit(2)
	}
	s, err := dht.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("dht server on %s", s.Addr())
	pingResponses := make(chan pingResponse)
	timeoutChan := make(chan struct{})
	go func() {
		for i, netloc := range pingStrAddrs {
			if i != 0 {
				time.Sleep(1 * time.Millisecond)
			}
			addr, err := net.ResolveUDPAddr("udp4", netloc)
			if err != nil {
				log.Fatal(err)
			}
			t, err := s.Ping(addr)
			if err != nil {
				log.Fatal(err)
			}
			start := time.Now()
			t.SetResponseHandler(func(addr string) func(dht.Msg, bool) {
				return func(resp dht.Msg, ok bool) {
					pingResponses <- pingResponse{
						addr:  addr,
						krpc:  resp,
						rtt:   time.Now().Sub(start),
						msgOk: ok,
					}
				}
			}(netloc))
		}
		if *timeout >= 0 {
			time.Sleep(*timeout)
			close(timeoutChan)
		}
	}()
	responses := 0
pingResponses:
	for _ = range pingStrAddrs {
		select {
		case resp := <-pingResponses:
			if !resp.msgOk {
				break
			}
			responses++
			fmt.Printf("%-65s %s\n", fmt.Sprintf("%x (%s):", resp.krpc.SenderID(), resp.addr), resp.rtt)
		case <-timeoutChan:
			break pingResponses
		}
	}
	// timeouts := len(pingStrAddrs) - responses
	fmt.Printf("%d/%d responses (%f%%)\n", responses, len(pingStrAddrs), 100*float64(responses)/float64(len(pingStrAddrs)))
}
<commit_msg>Fix name ambiguity for dht-ping, too many things are called pingResponses<commit_after>// Pings DHT nodes with the given network addresses.
package main

import (
	"flag"
	"fmt"
	"log"
	"net"
	"os"
	"time"

	"github.com/anacrolix/torrent/dht"
)

type pingResponse struct {
	addr  string
	krpc  dht.Msg
	msgOk bool
	rtt   time.Duration
}

func main() {
	log.SetFlags(log.LstdFlags | log.Lshortfile)
	timeout := flag.Duration("timeout", -1, "maximum timeout")
	flag.Parse()
	pingStrAddrs := flag.Args()
	if len(pingStrAddrs) == 0 {
		os.Stderr.WriteString("u must specify addrs of nodes to ping e.g. router.bittorrent.com:6881\n")
		os.Exit(2)
	}
	s, err := dht.NewServer(nil)
	if err != nil {
		log.Fatal(err)
	}
	log.Printf("dht server on %s", s.Addr())
	pingResponsesChan := make(chan pingResponse)
	timeoutChan := make(chan struct{})
	go func() {
		for i, netloc := range pingStrAddrs {
			if i != 0 {
				time.Sleep(1 * time.Millisecond)
			}
			addr, err := net.ResolveUDPAddr("udp4", netloc)
			if err != nil {
				log.Fatal(err)
			}
			t, err := s.Ping(addr)
			if err != nil {
				log.Fatal(err)
			}
			start := time.Now()
			t.SetResponseHandler(func(addr string) func(dht.Msg, bool) {
				return func(resp dht.Msg, ok bool) {
					pingResponsesChan <- pingResponse{
						addr:  addr,
						krpc:  resp,
						rtt:   time.Now().Sub(start),
						msgOk: ok,
					}
				}
			}(netloc))
		}
		if *timeout >= 0 {
			time.Sleep(*timeout)
			close(timeoutChan)
		}
	}()
	responses := 0
pingResponsesLoop:
	for _ = range pingStrAddrs {
		select {
		case resp := <-pingResponsesChan:
			if !resp.msgOk {
				break
			}
			responses++
			fmt.Printf("%-65s %s\n", fmt.Sprintf("%x (%s):", resp.krpc.SenderID(), resp.addr), resp.rtt)
		case <-timeoutChan:
			break pingResponsesLoop
		}
	}
	// timeouts := len(pingStrAddrs) - responses
	fmt.Printf("%d/%d responses (%f%%)\n", responses, len(pingStrAddrs), 100*float64(responses)/float64(len(pingStrAddrs)))
}
<|endoftext|>


<commit_before>package makedb

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"time"
)

const testDir = "tests"

var testFileTime = time.Now().Add(-time.Hour)

// Create a file in the test directory, ensuring that its last modified time
// is one second after the last created file. This is necessary because Make
// only compares file times to the second.
func createTestFile(name string) {
	path := filepath.Join(testDir, name)
	file, err := os.Create(path)
	if err != nil {
		panic(err)
	}
	file.Close()
	if err := os.Chtimes(path, testFileTime, testFileTime); err != nil {
		panic(err)
	}
	testFileTime = testFileTime.Add(time.Second)
}

func clearTestFiles() {
	files, err := ioutil.ReadDir(testDir)
	if err != nil {
		panic(err)
	}
	for _, info := range files {
		if !strings.HasPrefix(info.Name(), "Makefile") {
			os.Remove(filepath.Join(testDir, info.Name()))
		}
	}
}

func runMake() []byte {
	cmd := exec.Command(
		"make",
		"--question",
		"--print-data-base",
	)
	cmd.Dir = testDir
	out, _ := cmd.Output()
	return out
}

func getDatabase() Database {
	out := runMake()
	r := bytes.NewReader(out)
	db := NewDatabase()
	if err := db.Populate(r); err != nil {
		panic(err)
	}
	return db
}

func query(db Database, targetName string) (ok bool) {
	if t := db.GetTarget(targetName); !t.Phony {
		if t.DoesNotExist || t.NeedsUpdate {
			return false
		}
	}
	nDeps, oDeps := db.GetDeps(targetName)
	for _, name := range nDeps {
		t := db.GetTarget(name)
		if t.DoesNotExist || t.NeedsUpdate {
			return false
		}
	}
	for _, name := range oDeps {
		t := db.GetTarget(name)
		if t.DoesNotExist {
			return false
		}
	}
	return true
}

func targetIsMissing(db Database, t *Target) bool {
	// Target does not exist, needs update.
	ok := query(db, t.Name)
	return !ok && t.DoesNotExist && t.NeedsUpdate
}

func targetNeedsUpdate(db Database, t *Target) bool {
	// Target exists, needs update due to dependency.
	ok := query(db, t.Name)
	return !ok && !t.DoesNotExist && t.NeedsUpdate
}

func targetNotChecked(db Database, t *Target) bool {
	// Target was not checked because another dependency needs updating.
	// Target exists, is up to date.
	ok := query(db, t.Name)
	return ok && !t.DoesNotExist && !t.NeedsUpdate
}

func targetOK(db Database, t *Target) bool {
	// Target exists, needs update due to dependency.
	ok := query(db, t.Name)
	return ok && !t.DoesNotExist && !t.NeedsUpdate
}

type TargetAssertions map[string](func(db Database, t *Target) bool)

func (a TargetAssertions) Check() error {
	db := getDatabase()
	for name, checkFunc := range a {
		t := db.Targets[name]
		if !checkFunc(db, t) {
			ok := query(db, t.Name)
			return fmt.Errorf(
				"\nTarget: %s\nOK: %v\nDoesNotExist: %v\nNeedsUpdate: %v",
				t.Name, ok, t.DoesNotExist, t.NeedsUpdate,
			)
		}
	}
	return nil
}

func TestMakeFileTargets(t *testing.T) {
	clearTestFiles()
	defer clearTestFiles()

	// Every target is missing, except for f4, which doesn't get checked.
	// That is because f1 requires f2 which requires f3 and f4.
	// When f3 is found to be missing, there is no need to check f4.

	tests := TargetAssertions{
		"f1": targetIsMissing,
		"f2": targetIsMissing,
		"f3": targetIsMissing,
		"f4": targetNotChecked, // No need to check f4 when f3 is missing.
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f1")

	// Now that f1 exists, it should see that it needs updating.
	// That is because it requires f2, which is still missing.

	tests = TargetAssertions{
		"f1": targetNeedsUpdate,
		"f2": targetIsMissing,
		"f3": targetIsMissing,
		"f4": targetNotChecked,
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f2")

	// Now that f2 exists, it should now need updating, because it requires
	// the missing f3 and f4. Because f1 requires f2, and f2 needs updating,
	// it still needs updating.

	tests = TargetAssertions{
		"f1": targetNeedsUpdate,
		"f2": targetNeedsUpdate,
		"f3": targetIsMissing,
		"f4": targetNotChecked,
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f3")

	// Now that f3 exists, it should be OK because it has no dependencies.
	// It now checks f4 and finds it to be missing, so f1 -> f2 -> f4 is
	// still not OK.

	tests = TargetAssertions{
		"f1": targetNeedsUpdate,
		"f2": targetNeedsUpdate,
		"f3": targetOK,
		"f4": targetIsMissing, // Now f4 is being checked.
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f4")

	// Now that f4 exists, it should be OK because it has no dependencies.
	// Because f2 depends on f3 and f4, and they were created AFTER f2,
	// it still needs to be updated.

	tests = TargetAssertions{
		"f1": targetNeedsUpdate,
		"f2": targetNeedsUpdate,
		"f3": targetOK,
		"f4": targetOK,
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f2")

	// Now that f2 has been updated after f3 and f4, it is OK. Because f1
	// depends on f2, and f1 was created first, it still needs to be updated.

	tests = TargetAssertions{
		"f1": targetNeedsUpdate,
		"f2": targetOK,
		"f3": targetOK,
		"f4": targetOK,
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f1")

	// Now everything should be OK.

	tests = TargetAssertions{
		"f1": targetOK,
		"f2": targetOK,
		"f3": targetOK,
		"f4": targetOK,
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

}
<commit_msg>Added a comment about the integration test.<commit_after>package makedb

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
	"time"
)

const testDir = "tests"

var testFileTime = time.Now().Add(-time.Hour)

// Create a file in the test directory, ensuring that its last modified time
// is one second after the last created file. This is necessary because Make
// only compares file times to the second.
func createTestFile(name string) {
	path := filepath.Join(testDir, name)
	file, err := os.Create(path)
	if err != nil {
		panic(err)
	}
	file.Close()
	if err := os.Chtimes(path, testFileTime, testFileTime); err != nil {
		panic(err)
	}
	testFileTime = testFileTime.Add(time.Second)
}

func clearTestFiles() {
	files, err := ioutil.ReadDir(testDir)
	if err != nil {
		panic(err)
	}
	for _, info := range files {
		if !strings.HasPrefix(info.Name(), "Makefile") {
			os.Remove(filepath.Join(testDir, info.Name()))
		}
	}
}

func runMake() []byte {
	cmd := exec.Command(
		"make",
		"--question",
		"--print-data-base",
	)
	cmd.Dir = testDir
	out, _ := cmd.Output()
	return out
}

func getDatabase() Database {
	out := runMake()
	r := bytes.NewReader(out)
	db := NewDatabase()
	if err := db.Populate(r); err != nil {
		panic(err)
	}
	return db
}

func query(db Database, targetName string) (ok bool) {
	if t := db.GetTarget(targetName); !t.Phony {
		if t.DoesNotExist || t.NeedsUpdate {
			return false
		}
	}
	nDeps, oDeps := db.GetDeps(targetName)
	for _, name := range nDeps {
		t := db.GetTarget(name)
		if t.DoesNotExist || t.NeedsUpdate {
			return false
		}
	}
	for _, name := range oDeps {
		t := db.GetTarget(name)
		if t.DoesNotExist {
			return false
		}
	}
	return true
}

func targetIsMissing(db Database, t *Target) bool {
	// Target does not exist, needs update.
	ok := query(db, t.Name)
	return !ok && t.DoesNotExist && t.NeedsUpdate
}

func targetNeedsUpdate(db Database, t *Target) bool {
	// Target exists, needs update due to dependency.
	ok := query(db, t.Name)
	return !ok && !t.DoesNotExist && t.NeedsUpdate
}

func targetNotChecked(db Database, t *Target) bool {
	// Target was not checked because another dependency needs updating.
	// Target exists, is up to date.
	ok := query(db, t.Name)
	return ok && !t.DoesNotExist && !t.NeedsUpdate
}

func targetOK(db Database, t *Target) bool {
	// Target exists, needs update due to dependency.
	ok := query(db, t.Name)
	return ok && !t.DoesNotExist && !t.NeedsUpdate
}

type TargetAssertions map[string](func(db Database, t *Target) bool)

func (a TargetAssertions) Check() error {
	db := getDatabase()
	for name, checkFunc := range a {
		t := db.Targets[name]
		if !checkFunc(db, t) {
			ok := query(db, t.Name)
			return fmt.Errorf(
				"\nTarget: %s\nOK: %v\nDoesNotExist: %v\nNeedsUpdate: %v",
				t.Name, ok, t.DoesNotExist, t.NeedsUpdate,
			)
		}
	}
	return nil
}

// TestMakeFileTargets is an integration test to see exactly what running
// "make --question --print-data-base" does in various states, and also to
// ensure that the parsing and population code is reading it correctly.
// In particular, the make command will not check dependencies which
// are of no consequence (due to earlier dependencies needing to be
// updated) and that results in incorrect data. But that is of no
// consequence as long as it is understood.
func TestMakeFileTargets(t *testing.T) {
	clearTestFiles()
	defer clearTestFiles()

	// Every target is missing, except for f4, which doesn't get checked.
	// That is because f1 requires f2 which requires f3 and f4.

	tests := TargetAssertions{
		"f1": targetIsMissing,
		"f2": targetIsMissing,
		"f3": targetIsMissing,
		"f4": targetNotChecked, // No need to check f4 when f3 is missing.
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f1")

	// Now that f1 exists, it should see that it needs updating.
	// That is because it requires f2, which is still missing.

	tests = TargetAssertions{
		"f1": targetNeedsUpdate,
		"f2": targetIsMissing,
		"f3": targetIsMissing,
		"f4": targetNotChecked,
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f2")

	// Now that f2 exists, it should now need updating, because it requires
	// the missing f3 and f4. Because f1 requires f2, and f2 needs updating,
	// it still needs updating.

	tests = TargetAssertions{
		"f1": targetNeedsUpdate,
		"f2": targetNeedsUpdate,
		"f3": targetIsMissing,
		"f4": targetNotChecked,
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f3")

	// Now that f3 exists, it should be OK because it has no dependencies.
	// It now checks f4 and finds it to be missing, so f1 -> f2 -> f4 is
	// still not OK.

	tests = TargetAssertions{
		"f1": targetNeedsUpdate,
		"f2": targetNeedsUpdate,
		"f3": targetOK,
		"f4": targetIsMissing, // Now f4 is being checked.
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f4")

	// Now that f4 exists, it should be OK because it has no dependencies.
	// Because f2 depends on f3 and f4, and they were created AFTER f2,
	// it still needs to be updated.

	tests = TargetAssertions{
		"f1": targetNeedsUpdate,
		"f2": targetNeedsUpdate,
		"f3": targetOK,
		"f4": targetOK,
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f2")

	// Now that f2 has been updated after f3 and f4, it is OK. Because f1
	// depends on f2, and f1 was created first, it still needs to be updated.

	tests = TargetAssertions{
		"f1": targetNeedsUpdate,
		"f2": targetOK,
		"f3": targetOK,
		"f4": targetOK,
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

	createTestFile("f1")

	// Now everything should be OK.

	tests = TargetAssertions{
		"f1": targetOK,
		"f2": targetOK,
		"f3": targetOK,
		"f4": targetOK,
	}
	if err := tests.Check(); err != nil {
		t.Error(err)
	}

}
<|endoftext|>


<commit_before>package main

import (
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/knq/sdhook"
)

func main() {
	// create a logger with some fields
	logger := logrus.New().WithFields(logrus.Fields{
		"my_field":  115888,
		"my_field2": 898858,
	})

	// create stackdriver hook
	hook, err := sdhook.New(
		sdhook.GoogleServiceAccountCredentialsFile("./credentials.json"),
		sdhook.LogName("some_log"),
	)
	if err != nil {
		logger.Fatal(err)
	}

	// add to logrus
	logger.Hooks.Add(hook)

	// log some message
	logger.Printf("a random message @ %s", time.Now().Format("15:04:05"))

	// wait for the writes to finish
	time.Sleep(10 * time.Second)
}
<commit_msg>Make example compile<commit_after>package main

import (
	"time"

	"github.com/Sirupsen/logrus"
	"github.com/knq/sdhook"
)

func main() {
	// create a logger with some fields
	logger := logrus.New()
	logger.WithFields(logrus.Fields{
		"my_field":  115888,
		"my_field2": 898858,
	})

	// create stackdriver hook
	hook, err := sdhook.New(
		sdhook.GoogleServiceAccountCredentialsFile("./credentials.json"),
		sdhook.LogName("some_log"),
	)
	if err != nil {
		logger.Fatal(err)
	}

	// add to logrus
	logger.Hooks.Add(hook)

	// log some message
	logger.Printf("a random message @ %s", time.Now().Format("15:04:05"))

	// wait for the writes to finish
	time.Sleep(10 * time.Second)
}
<|endoftext|>


<commit_before>// Copyright 2014 Google Inc. All Rights Reserved.
//
// Licensed 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.

// +build test

package manager

import (
	"github.com/google/cadvisor/events"
	info "github.com/google/cadvisor/info/v1"
	"github.com/google/cadvisor/info/v2"

	"github.com/stretchr/testify/mock"
)

type ManagerMock struct {
	mock.Mock
}

func (c *ManagerMock) Start() error {
	args := c.Called()
	return args.Error(0)
}

func (c *ManagerMock) Stop() error {
	args := c.Called()
	return args.Error(0)
}

func (c *ManagerMock) GetContainerInfo(name string, query *info.ContainerInfoRequest) (*info.ContainerInfo, error) {
	args := c.Called(name, query)
	return args.Get(0).(*info.ContainerInfo), args.Error(1)
}

func (c *ManagerMock) SubcontainersInfo(containerName string, query *info.ContainerInfoRequest) ([]*info.ContainerInfo, error) {
	args := c.Called(containerName, query)
	return args.Get(0).([]*info.ContainerInfo), args.Error(1)
}

func (c *ManagerMock) AllDockerContainers(query *info.ContainerInfoRequest) (map[string]info.ContainerInfo, error) {
	args := c.Called(query)
	return args.Get(0).(map[string]info.ContainerInfo), args.Error(1)
}

func (c *ManagerMock) DockerContainer(name string, query *info.ContainerInfoRequest) (info.ContainerInfo, error) {
	args := c.Called(name, query)
	return args.Get(0).(info.ContainerInfo), args.Error(1)
}

func (c *ManagerMock) GetContainerSpec(containerName string, options v2.RequestOptions) (map[string]v2.ContainerSpec, error) {
	args := c.Called(containerName, options)
	return args.Get(0).(map[string]v2.ContainerSpec), args.Error(1)
}

func (c *ManagerMock) GetDerivedStats(containerName string, options v2.RequestOptions) (map[string]v2.DerivedStats, error) {
	args := c.Called(containerName, options)
	return args.Get(0).(map[string]v2.DerivedStats), args.Error(1)
}

func (c *ManagerMock) GetRequestedContainersInfo(containerName string, options v2.RequestOptions) (map[string]*info.ContainerInfo, error) {
	args := c.Called(containerName, options)
	return args.Get(0).(map[string]*info.ContainerInfo), args.Error(1)
}

func (c *ManagerMock) Exists(name string) bool {
	args := c.Called(name)
	return args.Get(0).(bool)
}

func (c *ManagerMock) WatchForEvents(queryuest *events.Request, passedChannel chan *info.Event) error {
	args := c.Called(queryuest, passedChannel)
	return args.Error(0)
}

func (c *ManagerMock) GetPastEvents(queryuest *events.Request) ([]*info.Event, error) {
	args := c.Called(queryuest)
	return args.Get(0).([]*info.Event), args.Error(1)
}

func (c *ManagerMock) GetMachineInfo() (*info.MachineInfo, error) {
	args := c.Called()
	return args.Get(0).(*info.MachineInfo), args.Error(1)
}

func (c *ManagerMock) GetVersionInfo() (*info.VersionInfo, error) {
	args := c.Called()
	return args.Get(0).(*info.VersionInfo), args.Error(1)
}

func (c *ManagerMock) GetFsInfo() ([]v2.FsInfo, error) {
	args := c.Called()
	return args.Get(0).([]v2.FsInfo), args.Error(1)
}

func (c *ManagerMock) GetProcessList(name string, options v2.RequestOptions) ([]v2.ProcessInfo, error) {
	args := c.Called()
	return args.Get(0).([]v2.ProcessInfo), args.Error(1)
}

func (c *ManagerMock) DockerInfo() (DockerStatus, error) {
	args := c.Called()
	return args.Get(0).(DockerStatus), args.Error(1)
}

func (c *ManagerMock) DockerImages() ([]DockerImage, error) {
	args := c.Called()
	return args.Get(0).([]DockerImage), args.Error(1)
}
<commit_msg>Delete unused ManagerMock<commit_after><|endoftext|>


<commit_before>package main

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/gonum/matrix/mat64"
	"github.com/gonum/stat/distuv"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func printMatrix(m mat64.Matrix, cols, decs int) {
	formatString := fmt.Sprintf("%%%d.%df", cols, decs)
	r, c := m.Dims()
	for i := 0; i < r; i++ {
		for j := 0; j < c; j++ {
			fmt.Printf(formatString, m.At(i, j))
		}
		fmt.Printf("\n")
	}
}

func generatePopulation(popSize, numQueens int) *mat64.Dense {
	m := mat64.NewDense(popSize, numQueens, nil)
	for i := 0; i < popSize; i++ {
		for j := 0; j < numQueens; j++ {
			m.Set(i, j, float64(rand.Intn(numQueens)))
		}
	}
	return m
}

func fitness(m *mat64.Vector) float64 {
	n, _ := m.Dims()
	attacks := 0
	for i := 0; i < n-1; i++ {
		queen := int(m.At(i, 0))
		for j := i + 1; j < n; j++ {
			other := int(m.At(j, 0))

			if queen == other || queen+i-j == other || queen-i+j == other {
				attacks++
			}
		}
	}
	return 1 - float64(attacks)*2/float64(n-1)/float64(n)
}

func fitnessVector(m *mat64.Dense) []float64 {
	n, _ := m.Dims()
	v := make([]float64, n)
	for i := 0; i < n; i++ {
		v[i] = fitness(m.RowView(i))
	}
	return v
}

func crossover(pop *mat64.Dense, mutationRate float64, fitvec []float64) *mat64.Dense {
	popSize, numQueens := pop.Dims()
	newPop := mat64.NewDense(popSize, numQueens, nil)

	// If the fitnessVector was not provided, calculate it
	if fitvec == nil {
		fitvec = fitnessVector(pop)
	}

	// Distribution from which the index of a parent is taken
	sampler := distuv.NewCategorical(fitvec, nil)

	for i := 0; i < popSize; i++ {
		// Get parents and produce child
		father := int(sampler.Rand())
		mother := int(sampler.Rand())
		crossoverPoint := rand.Intn(numQueens)
		for k := 0; k < crossoverPoint; k++ {
			newPop.Set(i, k, pop.At(father, k))
		}
		for k := crossoverPoint; k < numQueens; k++ {
			newPop.Set(i, k, pop.At(mother, k))
		}

		// Mutation
		if rand.Float64() < mutationRate {
			mutationIndex := rand.Intn(numQueens)
			newValue := rand.Intn(numQueens)
			newPop.Set(i, mutationIndex, float64(newValue))
		}
	}

	return newPop
}

func getMaxInfo(v []float64) (int, float64) {
	n := len(v)
	currentMax := -1.0
	currentIndex := 0

	for i := 0; i < n; i++ {
		if v[i] > currentMax {
			currentMax = v[i]
			currentIndex = i
		}
	}

	return currentIndex, currentMax
}

func main() {
	popSize := 10
	numQueens := 8
	mutationRate := 0.05

	fmt.Println("Initializing...")

	pop := generatePopulation(popSize, numQueens)
	fit := fitnessVector(pop)
	mIdx, mVal := getMaxInfo(fit)
	gen := 0

	fmt.Println("Evolving...")

	for mVal < 0.99 {
		pop = crossover(pop, mutationRate, fit)
		fit = fitnessVector(pop)
		mIdx, mVal = getMaxInfo(fit)
		gen++
	}

	fmt.Println("Evolution ended.")
	fmt.Printf("Generation: %d\nFitness value:%f\n", gen, mVal)
	printMatrix(pop.RowView(mIdx), 3, 0)
}
<commit_msg>Make result a row vector<commit_after>package main

import (
	"fmt"
	"math/rand"
	"time"

	"github.com/gonum/matrix/mat64"
	"github.com/gonum/stat/distuv"
)

func init() {
	rand.Seed(time.Now().UnixNano())
}

func printMatrix(m mat64.Matrix, cols, decs int) {
	formatString := fmt.Sprintf("%%%d.%df", cols, decs)
	r, c := m.Dims()
	for i := 0; i < r; i++ {
		for j := 0; j < c; j++ {
			fmt.Printf(formatString, m.At(i, j))
		}
		fmt.Printf("\n")
	}
}

func generatePopulation(popSize, numQueens int) *mat64.Dense {
	m := mat64.NewDense(popSize, numQueens, nil)
	for i := 0; i < popSize; i++ {
		for j := 0; j < numQueens; j++ {
			m.Set(i, j, float64(rand.Intn(numQueens)))
		}
	}
	return m
}

func fitness(m *mat64.Vector) float64 {
	n, _ := m.Dims()
	attacks := 0
	for i := 0; i < n-1; i++ {
		queen := int(m.At(i, 0))
		for j := i + 1; j < n; j++ {
			other := int(m.At(j, 0))

			if queen == other || queen+i-j == other || queen-i+j == other {
				attacks++
			}
		}
	}
	return 1 - float64(attacks)*2/float64(n-1)/float64(n)
}

func fitnessVector(m *mat64.Dense) []float64 {
	n, _ := m.Dims()
	v := make([]float64, n)
	for i := 0; i < n; i++ {
		v[i] = fitness(m.RowView(i))
	}
	return v
}

func crossover(pop *mat64.Dense, mutationRate float64, fitvec []float64) *mat64.Dense {
	popSize, numQueens := pop.Dims()
	newPop := mat64.NewDense(popSize, numQueens, nil)

	// If the fitnessVector was not provided, calculate it
	if fitvec == nil {
		fitvec = fitnessVector(pop)
	}

	// Distribution from which the index of a parent is taken
	sampler := distuv.NewCategorical(fitvec, nil)

	for i := 0; i < popSize; i++ {
		// Get parents and produce child
		father := int(sampler.Rand())
		mother := int(sampler.Rand())
		crossoverPoint := rand.Intn(numQueens)
		for k := 0; k < crossoverPoint; k++ {
			newPop.Set(i, k, pop.At(father, k))
		}
		for k := crossoverPoint; k < numQueens; k++ {
			newPop.Set(i, k, pop.At(mother, k))
		}

		// Mutation
		if rand.Float64() < mutationRate {
			mutationIndex := rand.Intn(numQueens)
			newValue := rand.Intn(numQueens)
			newPop.Set(i, mutationIndex, float64(newValue))
		}
	}

	return newPop
}

func getMaxInfo(v []float64) (int, float64) {
	n := len(v)
	currentMax := -1.0
	currentIndex := 0

	for i := 0; i < n; i++ {
		if v[i] > currentMax {
			currentMax = v[i]
			currentIndex = i
		}
	}

	return currentIndex, currentMax
}

func main() {
	popSize := 10
	numQueens := 8
	mutationRate := 0.05

	fmt.Println("Initializing...")

	pop := generatePopulation(popSize, numQueens)
	fit := fitnessVector(pop)
	mIdx, mVal := getMaxInfo(fit)
	gen := 0

	fmt.Println("Evolving...")

	for mVal < 0.99 {
		pop = crossover(pop, mutationRate, fit)
		fit = fitnessVector(pop)
		mIdx, mVal = getMaxInfo(fit)
		gen++
	}

	fmt.Println("Evolution ended.")
	fmt.Printf("Generation: %d\nFitness value:%f\n", gen, mVal)
	printMatrix(pop.RowView(mIdx).T(), 3, 0)
}
<|endoftext|>


<commit_before>package lfs

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"strconv"
)

// runCatFileBatchCheck uses 'git cat-file --batch-check' to get the type and
// size of a git object. Any object that isn't of type blob and under the
// blobSizeCutoff will be ignored. revs is a channel over which strings
// containing git sha1s will be sent. It returns a channel from which sha1
// strings can be read.
func runCatFileBatchCheck(smallRevCh chan string, revs *StringChannelWrapper, errCh chan error) error {
	cmd, err := startCommand("git", "cat-file", "--batch-check")
	if err != nil {
		return err
	}

	go func() {
		scanner := &catFileBatchCheckScanner{s: bufio.NewScanner(cmd.Stdout), limit: blobSizeCutoff}
		for r := range revs.Results {
			cmd.Stdin.Write([]byte(r + "\n"))
			hasNext := scanner.Scan()
			if b := scanner.BlobOID(); len(b) > 0 {
				smallRevCh <- b
			}

			if err := scanner.Err(); err != nil {
				errCh <- err
			}

			if !hasNext {
				break
			}
		}

		if err := revs.Wait(); err != nil {
			errCh <- err
		}
		cmd.Stdin.Close()

		stderr, _ := ioutil.ReadAll(cmd.Stderr)
		err := cmd.Wait()
		if err != nil {
			errCh <- fmt.Errorf("Error in git cat-file --batch-check: %v %v", err, string(stderr))
		}
		close(smallRevCh)
		close(errCh)
	}()

	return nil
}

type catFileBatchCheckScanner struct {
	s       *bufio.Scanner
	limit   int
	blobOID string
	err     error
}

func (s *catFileBatchCheckScanner) BlobOID() string {
	return s.blobOID
}

func (s *catFileBatchCheckScanner) Err() error {
	return s.err
}

func (s *catFileBatchCheckScanner) Scan() bool {
	s.blobOID, s.err = "", nil
	b, hasNext, err := s.next()
	s.blobOID = b
	s.err = err
	return hasNext
}

func (s *catFileBatchCheckScanner) next() (string, bool, error) {
	hasNext := s.s.Scan()
	line := s.s.Text()
	lineLen := len(line)

	// Format is:
	// <sha1> <type> <size>
	// type is at a fixed spot, if we see that it's "blob", we can avoid
	// splitting the line just to get the size.
	if lineLen < 46 {
		return "", hasNext, nil
	}

	if line[41:45] != "blob" {
		return "", hasNext, nil
	}

	size, err := strconv.Atoi(line[46:lineLen])
	if err != nil {
		return "", hasNext, nil
	}

	if size >= s.limit {
		return "", hasNext, nil
	}

	return line[0:40], hasNext, nil
}
<commit_msg>use the inner scanner err<commit_after>package lfs

import (
	"bufio"
	"fmt"
	"io/ioutil"
	"strconv"
)

// runCatFileBatchCheck uses 'git cat-file --batch-check' to get the type and
// size of a git object. Any object that isn't of type blob and under the
// blobSizeCutoff will be ignored. revs is a channel over which strings
// containing git sha1s will be sent. It returns a channel from which sha1
// strings can be read.
func runCatFileBatchCheck(smallRevCh chan string, revs *StringChannelWrapper, errCh chan error) error {
	cmd, err := startCommand("git", "cat-file", "--batch-check")
	if err != nil {
		return err
	}

	go func() {
		scanner := &catFileBatchCheckScanner{s: bufio.NewScanner(cmd.Stdout), limit: blobSizeCutoff}
		for r := range revs.Results {
			cmd.Stdin.Write([]byte(r + "\n"))
			hasNext := scanner.Scan()
			if b := scanner.BlobOID(); len(b) > 0 {
				smallRevCh <- b
			}

			if err := scanner.Err(); err != nil {
				errCh <- err
			}

			if !hasNext {
				break
			}
		}

		if err := revs.Wait(); err != nil {
			errCh <- err
		}
		cmd.Stdin.Close()

		stderr, _ := ioutil.ReadAll(cmd.Stderr)
		err := cmd.Wait()
		if err != nil {
			errCh <- fmt.Errorf("Error in git cat-file --batch-check: %v %v", err, string(stderr))
		}
		close(smallRevCh)
		close(errCh)
	}()

	return nil
}

type catFileBatchCheckScanner struct {
	s       *bufio.Scanner
	limit   int
	blobOID string
}

func (s *catFileBatchCheckScanner) BlobOID() string {
	return s.blobOID
}

func (s *catFileBatchCheckScanner) Err() error {
	return s.s.Err()
}

func (s *catFileBatchCheckScanner) Scan() bool {
	s.blobOID = ""
	b, hasNext := s.next()
	s.blobOID = b
	return hasNext
}

func (s *catFileBatchCheckScanner) next() (string, bool) {
	hasNext := s.s.Scan()
	line := s.s.Text()
	lineLen := len(line)

	// Format is:
	// <sha1> <type> <size>
	// type is at a fixed spot, if we see that it's "blob", we can avoid
	// splitting the line just to get the size.
	if lineLen < 46 {
		return "", hasNext
	}

	if line[41:45] != "blob" {
		return "", hasNext
	}

	size, err := strconv.Atoi(line[46:lineLen])
	if err != nil {
		return "", hasNext
	}

	if size >= s.limit {
		return "", hasNext
	}

	return line[0:40], hasNext
}
<|endoftext|>