package remotefileupload

import (
	"math/rand"
	"strconv"
	"testing"
	"time"
)

// without the goroutine, only sends 11
func BenchmarkAzureUpload(b *testing.B) {
	azureAccount = "fiskerclouddev"
	azureAccountKey = "REPLACE_ME"

	uploader, err := NewAzureUploader("trex-logs", ".txt")
	if err != nil {
		b.Error(err)
	}

	benchmarkUploadTime(uploader, b)
}

func BenchmarkAzureBatchUpload(b *testing.B) {
	azureAccount = "fiskerclouddev"
	azureAccountKey = "REPLACE_ME"

	uploader, err := NewAzureBatchUploader("trex-logs", ".txt", 1, "\n")
	if err != nil {
		b.Error(err)
	}

	benchmarkUploadTime(uploader, b)
}

// 1762456 messages,
func benchmarkUploadTime(uploader Uploader, b *testing.B) {
	endTimer := time.NewTimer(time.Second * 5)
	messagesSent := 0
Loop:
	for {
		select {
		case <-endTimer.C:
			break Loop
		default:
			SendMessage(time.Now(), uploader, b)
			messagesSent++
		}
	}

	time.Sleep(5 * time.Second)
	b.Logf("Benchmark %s 'sent' %d messages\n", b.Name(), messagesSent)
}

func SendMessage(t time.Time, uploader Uploader, b *testing.B) {
	thread := rand.Intn(10)
	_, err := uploader.Upload([]byte(t.Format(time.RubyDate)), LogPayload{
		Title: "This",
		Value: "Some",
	}, "/benchmark", strconv.Itoa(thread))
	if err != nil {
		b.Error(b)
	}
}
