github.com/eth-easl/loader@v0.0.0-20230908084258-8a37e1d94279/pkg/trace/parser_test.go (about)

     1  /*
     2   * MIT License
     3   *
     4   * Copyright (c) 2023 EASL and the vHive community
     5   *
     6   * Permission is hereby granted, free of charge, to any person obtaining a copy
     7   * of this software and associated documentation files (the "Software"), to deal
     8   * in the Software without restriction, including without limitation the rights
     9   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    10   * copies of the Software, and to permit persons to whom the Software is
    11   * furnished to do so, subject to the following conditions:
    12   *
    13   * The above copyright notice and this permission notice shall be included in all
    14   * copies or substantial portions of the Software.
    15   *
    16   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    17   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    18   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    19   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    20   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    21   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
    22   * SOFTWARE.
    23   */
    24  
    25  package trace
    26  
    27  import (
    28  	"math"
    29  	"strings"
    30  	"testing"
    31  )
    32  
    33  func floatEqual(n, expected float64) bool {
    34  	return math.Abs(n-expected) < 1e-3
    35  }
    36  
    37  func TestParseInvocationTrace(t *testing.T) {
    38  	duration := 10
    39  	invocationTrace := *parseInvocationTrace("test_data/invocations.csv", duration)
    40  
    41  	if len(invocationTrace) != 1 {
    42  		t.Error("Invalid invocations trace provided.")
    43  	}
    44  
    45  	function := invocationTrace[0]
    46  
    47  	if function.HashOwner != "c455703077a17a9b8d0fc655d939fcc6d24d819fa9a1066b74f710c35a43cbc8" ||
    48  		function.HashApp != "68baea05aa0c3619b6feb78c80a07e27e4e68f921d714b8125f916c3b3370bf2" ||
    49  		function.HashFunction != "c13acdc7567b225971cef2416a3a2b03c8a4d8d154df48afe75834e2f5c59ddf" ||
    50  		function.Trigger != "queue" {
    51  
    52  		t.Error("Unexpected data has been read.")
    53  	}
    54  
    55  	if len(function.Invocations) != duration {
    56  		t.Error("Invalid invocations trace for length.")
    57  	}
    58  
    59  	for i := 0; i < duration; i++ {
    60  		if function.Invocations[i] != i+1 {
    61  			t.Error("Invalid number of invocations has been read.")
    62  		}
    63  	}
    64  }
    65  
    66  func TestParseRuntimeTrace(t *testing.T) {
    67  	runtimeTrace := *parseRuntimeTrace("test_data/durations.csv")
    68  
    69  	if len(runtimeTrace) != 1 {
    70  		t.Error("Invalid runtime trace provided.")
    71  	}
    72  
    73  	function := runtimeTrace[0]
    74  
    75  	if function.HashOwner != "c455703077a17a9b8d0fc655d939fcc6d24d819fa9a1066b74f710c35a43cbc8" ||
    76  		function.HashApp != "68baea05aa0c3619b6feb78c80a07e27e4e68f921d714b8125f916c3b3370bf2" ||
    77  		function.HashFunction != "c13acdc7567b225971cef2416a3a2b03c8a4d8d154df48afe75834e2f5c59ddf" ||
    78  		!floatEqual(function.Average, 100.0) ||
    79  		!floatEqual(function.Count, 57523.0) ||
    80  		!floatEqual(function.Minimum, 1.0) ||
    81  		!floatEqual(function.Maximum, 7.0) ||
    82  		!floatEqual(function.Percentile0, 1) ||
    83  		!floatEqual(function.Percentile1, 2) ||
    84  		!floatEqual(function.Percentile25, 3) ||
    85  		!floatEqual(function.Percentile50, 4) ||
    86  		!floatEqual(function.Percentile75, 5) ||
    87  		!floatEqual(function.Percentile99, 6) ||
    88  		!floatEqual(function.Percentile100, 7) {
    89  
    90  		t.Error("Unexpected data has been read.")
    91  	}
    92  }
    93  
    94  func TestParseMemoryTrace(t *testing.T) {
    95  	memoryTrace := *parseMemoryTrace("test_data/memory.csv")
    96  
    97  	if len(memoryTrace) != 1 {
    98  		t.Error("Invalid memory trace provided.")
    99  	}
   100  
   101  	function := memoryTrace[0]
   102  
   103  	if function.HashOwner != "c455703077a17a9b8d0fc655d939fcc6d24d819fa9a1066b74f710c35a43cbc8" ||
   104  		function.HashApp != "68baea05aa0c3619b6feb78c80a07e27e4e68f921d714b8125f916c3b3370bf2" ||
   105  		function.HashFunction != "c13acdc7567b225971cef2416a3a2b03c8a4d8d154df48afe75834e2f5c59ddf" ||
   106  		!floatEqual(function.Count, 19342.0) ||
   107  		!floatEqual(function.Average, 120.0) ||
   108  		!floatEqual(function.Percentile1, 95) ||
   109  		!floatEqual(function.Percentile5, 96) ||
   110  		!floatEqual(function.Percentile25, 97) ||
   111  		!floatEqual(function.Percentile50, 98) ||
   112  		!floatEqual(function.Percentile75, 99) ||
   113  		!floatEqual(function.Percentile95, 100) ||
   114  		!floatEqual(function.Percentile99, 101) ||
   115  		!floatEqual(function.Percentile100, 102) {
   116  
   117  		t.Error("Unexpected data has been read.")
   118  	}
   119  }
   120  
   121  func TestParserWrapper(t *testing.T) {
   122  	parser := NewAzureParser("test_data", 10)
   123  	functions := parser.Parse()
   124  
   125  	if len(functions) != 1 {
   126  		t.Error("Invalid function array length.")
   127  	}
   128  	if !strings.HasPrefix(functions[0].Name, "trace-func") ||
   129  		functions[0].InvocationStats == nil ||
   130  		functions[0].RuntimeStats == nil ||
   131  		functions[0].MemoryStats == nil {
   132  
   133  		t.Error("Unexpected results.")
   134  	}
   135  }