package kafka_test

import (
	"bytes"
	"compress/gzip"
	"io"
	"testing"

	"github.com/golang/snappy"
)

const totalPayloadSize = 10

func tryClose(w io.Writer) error {
	if w, ok := w.(io.Closer); ok {
		return w.Close()
	}
	return nil
}

func BenchmarkNoCompressionWrite(b *testing.B) {
	w := io.Writer(io.Discard)
	//payload := []byte("yes this is 25 bytes long")
	payload := make([]byte, 10000)

	b.ResetTimer()
	for i := 0; i < 1024*1024*totalPayloadSize/4; i++ {
		_, err := w.Write(payload)
		if err != nil {
			b.Error("error writing file")
		}
	}

	err := tryClose(w)
	if err != nil {
		b.Error("error closing file")
	}
}

func BenchmarkGZipCompressionWrite(b *testing.B) {
	w := gzip.NewWriter(io.Discard)
	//payload := []byte("yes this is 25 bytes long")
	payload := make([]byte, 10000)

	b.ResetTimer()
	for i := 0; i < 1024*1024*totalPayloadSize/4; i++ {
		_, err := w.Write(payload)
		if err != nil {
			b.Error("error writing file")
		}
	}

	err := w.Close()
	if err != nil {
		b.Error("error closing file")
	}
}

func BenchmarkSnappyCompressionWrite(b *testing.B) {
	w := snappy.NewWriter(io.Discard)
	//payload := []byte("yes this is 25 bytes long")
	payload := make([]byte, 10000)

	b.ResetTimer()
	for i := 0; i < 1024*1024*totalPayloadSize/4; i++ {
		_, err := w.Write(payload)
		if err != nil {
			b.Error("error writing file")
		}
	}

	err := w.Close()
	if err != nil {
		b.Error("error closing file")
	}
}

func BenchmarkNoCompressionRead(b *testing.B) {
	var buf bytes.Buffer

	w := io.Writer(&buf)
	//payload := []byte("yes this is 25 bytes long")
	payload := make([]byte, 10000)

	for i := 0; i < 1024*1024*totalPayloadSize/4; i++ {
		_, err := w.Write(payload)
		if err != nil {
			b.Error("error writing file")
		}
	}

	err := tryClose(w)
	if err != nil {
		b.Error("error closing file")
	}

	//r, err := io.NewReader(&buf)
	r := bytes.NewReader(payload)
	if err != nil {
		b.Error("error opening file")
	}

	b.ResetTimer()
	if _, err := io.Copy(io.Discard, r); err != nil {
		b.Error("error reading file")
	}

	// if err := r.Close(); err != nil {
	// 	b.Error("error closing file")
	// }
}

func BenchmarkGZipCompressionRead(b *testing.B) {
	var buf bytes.Buffer

	w := gzip.NewWriter(&buf)
	//payload := []byte("yes this is 25 bytes long")
	payload := make([]byte, 10000)

	for i := 0; i < 1024*1024*totalPayloadSize/4; i++ {
		_, err := w.Write(payload)
		if err != nil {
			b.Error("error writing file")
		}
	}

	err := w.Close()
	if err != nil {
		b.Error("error closing file")
	}

	r, err := gzip.NewReader(&buf)
	if err != nil {
		b.Error("error opening file")
	}

	b.ResetTimer()
	if _, err := io.Copy(io.Discard, r); err != nil {
		b.Error("error reading file")
	}

	if err := r.Close(); err != nil {
		b.Error("error closing file")
	}
}

func BenchmarkSnappyCompressionRead(b *testing.B) {
	var buf bytes.Buffer

	w := snappy.NewWriter(&buf)
	//payload := []byte("yes this is 25 bytes long")
	payload := make([]byte, 10000)

	for i := 0; i < 1024*1024*totalPayloadSize/4; i++ {
		_, err := w.Write(payload)
		if err != nil {
			b.Error("error writing file")
		}
	}

	err := w.Close()
	if err != nil {
		b.Error("error closing file")
	}

	r := snappy.NewReader(&buf)
	if err != nil {
		b.Error("error opening file")
	}

	b.ResetTimer()
	if _, err := io.Copy(io.Discard, r); err != nil {
		b.Error("error reading file")
	}
}
