[CmdletBinding()]
Param([switch]$ManageTomcat,
[String]$TomcatHome = "\apache-tomcat-9.0.0.M19",
[String]$User = "admin",
[String]$Pass = "nimda"
)


#Measure-Command { Invoke-WebRequest -URI http://localhost:8080/sbms/semantics/types/type }  Select-Object TotalSeconds

Function Get-Auth($user, $pass) {
  $pair = "${user}:${pass}"
  $bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
  $base64 = [System.Convert]::ToBase64String($bytes)
  $basicAuthValue = "Basic $base64"
  return @{ Authorization = $basicAuthValue }
}

Function Request($testName, $auth, $url) {
  #"Sending..." + [System.DateTime]::Now
  $start = [System.DateTime]::Now
  $r = Invoke-WebRequest -Method Get -URI $url -Headers $auth | Select-Object StatusCode, Content
  $end = [System.DateTime]::Now.Subtract($start) #| Select-Object TotalSeconds
  $dur = $end.TotalSeconds
  
  [System.String]::Format("{0:00.00}",$dur) + ";" + $testName + ";" + $r.StatusCode + ";" + $count
  
  
}

if($ManageTomcat) {
  If($env:JAVA_HOME -eq $null -or $env:JAVA_HOME -eq "") {
    "JAVA_HOME is not set - set the environment variable correctly to run Apache Tomcat"
    return;
  }
  $env:CATALINA_HOME=$TomcatHome
  & $TomcatHome\bin\startup.bat
  Pause

}

"duration;testName;statusCode;contentLength"

#initial request -- to init the tdb and mode
Request "0: Init" $auth "http://localhost:8080/sbms/semantics/types/type"

$auth = Get-Auth $User $Pass

Request "1: All information about a DP" $auth "http://localhost:8080/sbms/semantics/datapoints/?fields=bmsId%2Ctype%2Csource.bimId%2Csource.type%2Csource.location%2Cscope.bimId%2Cscope.type%2Cscope.location%2Csensing.type%2Csensing.window%2Cproperty.domain%2Cproperty.quality%2Cpublisher.bimId&bmsId=bacnet%3A%2F%2F04010306.AV1"

Request "2: All DPs according to strict criteria + grouping" $auth "http://localhost:8080/sbms/semantics/datapoints/?fields=bmsId&grouping=scope.floor&type=Input&source.type=TemperatureSensor&source.location=S01B04&scope.type=Room&property.domain=Air&property.quality=temperature"

Request "3: Generic query with large number of results + grouping" $auth "http://localhost:8080/sbms/semantics/datapoints/?fields=bmsId&grouping=scope.building&source.type=HumiditySensor&property.domain=Air"

Request "4: Generic query with large number of results + datapoint type + grouping" $auth "http://localhost:8080/sbms/semantics/datapoints/?fields=bmsId%2Ctype%2Cscope.bimId&grouping=scope.building&source.type=TemperatureSensor&property.domain=Air&property.quality=temperature"

Request "5: Generic query with large number of results + all available information + grouping" $auth "http://localhost:8080/sbms/semantics/datapoints/?fields=bmsId%2Ctype%2Csource.bimId%2Csource.type%2Csource.location%2Cscope.bimId%2Cscope.type%2Cscope.location%2Csensing.type%2Csensing.window%2Cproperty.domain%2Cproperty.quality%2Cpublisher.bimId&grouping=scope.building&source.type=TemperatureSensor&property.domain=Air&property.quality=temperature"


if($ManageTomcat) {
  Pause
  & $TomcatHome\bin\shutdown.bat
}