github.com/tetratelabs/wazero@v1.7.3-0.20240513003603-48f702e154b5/internal/integration_test/bench/testdata/case.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/base64"
     5  	"math/rand"
     6  	"strings"
     7  	"unsafe"
     8  )
     9  
    10  func main() {}
    11  
    12  //export allocate_buffer
    13  func allocateBuffer(size uint32) *byte {
    14  	// Allocate the in-Wasm memory region and returns its pointer to hosts.
    15  	// The region is supposed to store random strings generated in hosts,
    16  	// meaning that this is called "inside" of get_random_string.
    17  	buf := make([]byte, size)
    18  	return &buf[0]
    19  }
    20  
    21  //export get_random_string
    22  func getRandomStringRaw(retBufPtr **byte, retBufSize *int)
    23  
    24  // Get random string from the hosts.
    25  func getRandomString() string {
    26  	var bufPtr *byte
    27  	var bufSize int
    28  	getRandomStringRaw(&bufPtr, &bufSize)
    29  	return unsafe.String(bufPtr, bufSize)
    30  }
    31  
    32  //export base64
    33  func base64OnString(num uint32) {
    34  	// Get random strings from the host and
    35  	// do base64 encoding them for given times.
    36  	for i := uint32(0); i < num; i++ {
    37  		msg := getRandomString()
    38  		_ = base64.StdEncoding.EncodeToString([]byte(msg))
    39  	}
    40  }
    41  
    42  //export fibonacci
    43  func fibonacci(in uint32) uint32 {
    44  	if in <= 1 {
    45  		return in
    46  	}
    47  	return fibonacci(in-1) + fibonacci(in-2)
    48  }
    49  
    50  //export string_manipulation
    51  func stringManipulation(initialSize uint32) {
    52  	var str string
    53  	for i := 0; i < int(initialSize); i++ {
    54  		str += "a"
    55  		str += "b"
    56  	}
    57  	str2 := strings.Replace(str, "a", "b", -1)
    58  	str3 := str2[:3]
    59  	str4 := str2[3:]
    60  	lastStr := str4 + str3
    61  	lastStr = ""
    62  	print(lastStr)
    63  }
    64  
    65  //export reverse_array
    66  func reverseArray(size uint32) {
    67  	array := make([]int, size)
    68  	for i := 0; i < (len(array) >> 1); i++ {
    69  		j := len(array) - i - 1
    70  		array[j], array[i] = array[i], array[j]
    71  	}
    72  }
    73  
    74  //export random_mat_mul
    75  func randomMatMul(n uint32) {
    76  	var a, b, result [][]int
    77  	for i := uint32(0); i < n; i++ {
    78  		arow := make([]int, n)
    79  		brow := make([]int, n)
    80  		for j := uint32(0); j < n; j++ {
    81  			arow[j] = rand.Int()
    82  			brow[j] = rand.Int()
    83  		}
    84  		a = append(a, arow)
    85  		b = append(b, brow)
    86  		result = append(result, make([]int, n))
    87  	}
    88  
    89  	for i := uint32(0); i < n; i++ {
    90  		for j := uint32(0); j < n; j++ {
    91  			for k := uint32(0); k < n; k++ {
    92  				result[i][j] += a[i][k] * b[k][j]
    93  			}
    94  		}
    95  	}
    96  }