github.com/ssetin/PenguinCast@v0.2.0/main_test.go (about)

     1  package main
     2  
     3  import (
     4  	"io"
     5  	"log"
     6  	"os"
     7  	"strconv"
     8  	"sync"
     9  	"testing"
    10  	"time"
    11  
    12  	iceclient "github.com/ssetin/PenguinCast/src/client"
    13  	iceserver "github.com/ssetin/PenguinCast/src/server"
    14  )
    15  
    16  var IcySrv iceserver.IceServer
    17  
    18  // ================================== Setup ========================================
    19  const (
    20  	runServer      = false
    21  	listenersCount = 5000 // total number of listeners
    22  	incStep        = 50   // number of listeners, to increase with each step
    23  	waitStep       = 5    // seconds between each step
    24  	secToListen    = 5400 // seconds to listen by each connection
    25  	mountName      = "RockRadio96"
    26  	hostAddr       = "192.168.10.2:8008"
    27  )
    28  
    29  func init() {
    30  	if runServer {
    31  		go startServer()
    32  		time.Sleep(time.Second * 1)
    33  	}
    34  	log.Println("Waiting for SOURCE to connect...")
    35  	time.Sleep(time.Second * 5)
    36  }
    37  
    38  func startServer() {
    39  	err := IcySrv.Init()
    40  	if err != nil {
    41  		log.Println(err.Error())
    42  		return
    43  	}
    44  	IcySrv.Start()
    45  }
    46  func TestMain(m *testing.M) {
    47  	runTests := m.Run()
    48  	if runServer {
    49  		IcySrv.Close()
    50  	}
    51  	os.Exit(runTests)
    52  }
    53  
    54  // ================================== Tests ===========================================
    55  func TestMonitoringListenersCount(b *testing.T) {
    56  	// run server in another process to monitor it separately from clients
    57  	log.Println("Start creating listeners...")
    58  
    59  	wg := &sync.WaitGroup{}
    60  
    61  	for i := 0; i < listenersCount/incStep; i++ {
    62  		wg.Add(incStep)
    63  		for k := 0; k < incStep; k++ {
    64  			go func(wg *sync.WaitGroup, i int) {
    65  				defer wg.Done()
    66  				time.Sleep(time.Millisecond * 200)
    67  				cl := &iceclient.PenguinClient{}
    68  				if i < 0 {
    69  					cl.Init(hostAddr, mountName, "dump/"+mountName+"."+strconv.Itoa(i)+".mp3")
    70  				} else {
    71  					cl.Init(hostAddr, mountName, "")
    72  				}
    73  				err := cl.Listen(secToListen)
    74  				if err != nil && err != io.EOF {
    75  					log.Println(err)
    76  				}
    77  			}(wg, i*incStep+k)
    78  		}
    79  		time.Sleep(time.Second * waitStep)
    80  
    81  	}
    82  	log.Println("Waiting for listeners to finito...")
    83  	wg.Wait()
    84  }
    85  
    86  func TestDump(b *testing.T) {
    87  	time.Sleep(time.Second * 5)
    88  	cl := &iceclient.PenguinClient{}
    89  	cl.Init(hostAddr, mountName, "dump/dump2.mp3")
    90  	cl.Listen(1)
    91  }
    92  
    93  // ================================== Benchmarks ===========================================
    94  func BenchmarkGeneral(b *testing.B) {
    95  
    96  	cl := &iceclient.PenguinClient{}
    97  	cl.Init(hostAddr, mountName, "")
    98  	b.ResetTimer()
    99  
   100  	for i := 0; i < b.N; i++ {
   101  		err := cl.Listen(2)
   102  		if err != nil && err != io.EOF {
   103  			log.Println(err)
   104  		}
   105  	}
   106  }
   107  
   108  func BenchmarkParallel(b *testing.B) {
   109  	log.Println("Start creating listeners...")
   110  
   111  	wg := &sync.WaitGroup{}
   112  
   113  	for i := 0; i < 500/incStep; i++ {
   114  		wg.Add(incStep)
   115  		for k := 0; k < incStep; k++ {
   116  			go func(wg *sync.WaitGroup, i int) {
   117  				defer wg.Done()
   118  				time.Sleep(time.Millisecond * 100)
   119  				cl := &iceclient.PenguinClient{}
   120  				cl.Init(hostAddr, mountName, "")
   121  				err := cl.Listen(120)
   122  				if err != nil && err != io.EOF {
   123  					log.Println(err)
   124  				}
   125  			}(wg, i)
   126  		}
   127  		time.Sleep(time.Second * waitStep)
   128  
   129  	}
   130  	log.Println("Waiting for listeners to finito...")
   131  	wg.Wait()
   132  }
   133  
   134  func BenchmarkNone(b *testing.B) {
   135  	log.Println("Waiting for listeners...")
   136  	time.Sleep(time.Second * 800)
   137  }
   138  
   139  /*
   140  	go test -bench General  -benchmem -benchtime 120s -cpuprofile=cpu.out -memprofile=mem.out main_test.go -run notests
   141  	go test -bench Parallel -race -benchmem -cpuprofile=cpu.out -memprofile=mem.out main_test.go -run notests
   142  	go test -bench None -benchmem -timeout 20m -cpuprofile=cpu.out -memprofile=mem.out main_test.go -run notests
   143  
   144  	go tool pprof main.test cpu.out
   145  	go tool pprof -alloc_objects main.test mem.out
   146  	go tool pprof main.test block.out
   147  
   148  	go test -v -run MonitoringListenersCount -timeout 150m main_test.go
   149  	go test -v -timeout 60s -run Dump main_test.go
   150  
   151  	go-torch main.test cpu.out
   152  
   153  	mp3check -e -a -S -T -E -v dump/*.mp3
   154  	ulimit -n 63000
   155  */