example usage of WASM in RAGE:MP environment
This commit is contained in:
		
							parent
							
								
									ebb2364a67
								
							
						
					
					
						commit
						3e5e529924
					
				@ -1,3 +1,3 @@
 | 
			
		||||
import { fw } from '@entityseven/rage-fw-client'
 | 
			
		||||
 | 
			
		||||
fw.player.browser = mp.browsers.new('https://localhost:8080')
 | 
			
		||||
fw.player.browser = mp.browsers.new('https://localhost:5173')
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,7 @@
 | 
			
		||||
  "license": "CC BY-ND",
 | 
			
		||||
  "description": "Server side of rage-fw example",
 | 
			
		||||
  "scripts": {
 | 
			
		||||
    "build": "esbuild src/index.ts --bundle --platform=node --target=node10.4 --outfile=../../server/packages/server/index.js"
 | 
			
		||||
    "build": "esbuild src/index.ts --bundle --platform=node --target=node14.10 --format=cjs --outfile=../../server/packages/server/index.js"
 | 
			
		||||
  },
 | 
			
		||||
  "dependencies": {
 | 
			
		||||
    "@entityseven/rage-fw-server": "latest"
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										58
									
								
								apps/server/src/benchmark/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										58
									
								
								apps/server/src/benchmark/index.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,58 @@
 | 
			
		||||
/**
 | 
			
		||||
 * Calculates Pi using the Leibniz formula for a given number of iterations.
 | 
			
		||||
 * (Identical logic to the AssemblyScript version)
 | 
			
		||||
 * @param iterations Number of terms in the series.
 | 
			
		||||
 * @returns Approximate value of Pi.
 | 
			
		||||
 */
 | 
			
		||||
export function calculatePiLeibnizTs(iterations: number): number {
 | 
			
		||||
    let pi_div_4: number = 0.0
 | 
			
		||||
    let sign: number = 1.0
 | 
			
		||||
 | 
			
		||||
    for (let i: number = 0; i < iterations; i++) {
 | 
			
		||||
        let term = sign / (2.0 * i + 1.0)
 | 
			
		||||
        pi_div_4 += term
 | 
			
		||||
        sign = -sign // Flip the sign
 | 
			
		||||
    }
 | 
			
		||||
    return pi_div_4 * 4.0
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Helper function to check if a number is prime (Trial Division).
 | 
			
		||||
 * (Identical logic to the AssemblyScript version)
 | 
			
		||||
 * @param num The number to check.
 | 
			
		||||
 * @returns True if prime, false otherwise.
 | 
			
		||||
 */
 | 
			
		||||
function isPrimeTs(num: number): boolean {
 | 
			
		||||
    if (num <= 1) return false
 | 
			
		||||
    if (num <= 3) return true
 | 
			
		||||
    if (num % 2 == 0 || num % 3 == 0) return false
 | 
			
		||||
 | 
			
		||||
    // Only need to check up to sqrt(num)
 | 
			
		||||
    // Optimized loop: check 6k ± 1
 | 
			
		||||
    for (let i: number = 5; i * i <= num; i = i + 6) {
 | 
			
		||||
        if (num % i == 0 || num % (i + 2) == 0) {
 | 
			
		||||
            return false
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * Counts prime numbers up to a given limit using trial division.
 | 
			
		||||
 * (Identical logic to the AssemblyScript version)
 | 
			
		||||
 * @param limit The upper bound (exclusive) to search for primes.
 | 
			
		||||
 * @returns The count of prime numbers found.
 | 
			
		||||
 */
 | 
			
		||||
export function countPrimesTs(limit: number): number {
 | 
			
		||||
    let count: number = 0
 | 
			
		||||
    for (let i: number = 2; i < limit; i++) {
 | 
			
		||||
        if (isPrimeTs(i)) {
 | 
			
		||||
            count++
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
    return count
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
export function addTs(a: number, b: number): number {
 | 
			
		||||
    return a + b
 | 
			
		||||
}
 | 
			
		||||
@ -1,10 +0,0 @@
 | 
			
		||||
mp.events.addCommand('veh', async (player, fullText) => {
 | 
			
		||||
    const vehicleID = mp.joaat(fullText)
 | 
			
		||||
    const vehicle = mp.vehicles.new(vehicleID, player.position, {
 | 
			
		||||
        numberPlate: 'admin',
 | 
			
		||||
    })
 | 
			
		||||
 | 
			
		||||
    setTimeout(() => {
 | 
			
		||||
        player.putIntoVehicle(vehicle, 0)
 | 
			
		||||
    }, 250)
 | 
			
		||||
})
 | 
			
		||||
@ -1,9 +1,71 @@
 | 
			
		||||
import { fw } from '@entityseven/rage-fw-server'
 | 
			
		||||
import { InitWasm } from './wasm'
 | 
			
		||||
import { addTs, calculatePiLeibnizTs, countPrimesTs } from './benchmark'
 | 
			
		||||
 | 
			
		||||
import './commands'
 | 
			
		||||
import { performance } from 'perf_hooks'
 | 
			
		||||
/**
 | 
			
		||||
 * Runs a function, measures its execution time, and prints the result.
 | 
			
		||||
 * @param name Name of the benchmark.
 | 
			
		||||
 * @param func The function to execute.
 | 
			
		||||
 * @param args Arguments to pass to the function.
 | 
			
		||||
 */
 | 
			
		||||
function runBenchmark(
 | 
			
		||||
    name: string,
 | 
			
		||||
    func: (...args: any[]) => any,
 | 
			
		||||
    ...args: any[]
 | 
			
		||||
) {
 | 
			
		||||
    console.log(`\nRunning benchmark: ${name}`)
 | 
			
		||||
    console.log(`Arguments: ${args.join(', ')}`)
 | 
			
		||||
 | 
			
		||||
fw.event.register('playerJoin', async player => {
 | 
			
		||||
    fw.system.log.info(`Connected: ${player.socialClub}`)
 | 
			
		||||
    const startTime = performance.now()
 | 
			
		||||
    const result = func(...args)
 | 
			
		||||
    const endTime = performance.now()
 | 
			
		||||
    const duration = endTime - startTime
 | 
			
		||||
 | 
			
		||||
    fw.system.log.info(`Response from client: here respionse}`)
 | 
			
		||||
    console.log(`Result: ${result}`)
 | 
			
		||||
    console.log(`Execution Time: ${duration.toFixed(3)} ms`)
 | 
			
		||||
    return duration
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// --- Benchmark Parameters ---
 | 
			
		||||
const PI_ITERATIONS = 100_000_000_000 // High number for Pi calculation
 | 
			
		||||
const PRIME_LIMIT = 100_000_000 // Limit for prime counting
 | 
			
		||||
// --- End Benchmark Parameters ---
 | 
			
		||||
 | 
			
		||||
InitWasm().then(wasm => {
 | 
			
		||||
    // Verify AS module loaded
 | 
			
		||||
    const sumAs = wasm.addAs(5, 7)
 | 
			
		||||
    console.log(`AS Module Loaded Verification (addAs(5, 7)): ${sumAs}`)
 | 
			
		||||
    if (sumAs !== 12) {
 | 
			
		||||
        console.warn('AS addAs function did not return expected result!')
 | 
			
		||||
    }
 | 
			
		||||
    const sumTs = addTs(5, 7)
 | 
			
		||||
    console.log(`TS Implementation Verification (addTs(5, 7)): ${sumTs}`)
 | 
			
		||||
 | 
			
		||||
    // --- Run Benchmarks ---
 | 
			
		||||
 | 
			
		||||
    // Benchmark: Calculate Pi
 | 
			
		||||
    runBenchmark(
 | 
			
		||||
        'TypeScript: Calculate Pi (Leibniz)',
 | 
			
		||||
        calculatePiLeibnizTs,
 | 
			
		||||
        PI_ITERATIONS,
 | 
			
		||||
    )
 | 
			
		||||
    runBenchmark(
 | 
			
		||||
        'AssemblyScript: Calculate Pi (Leibniz)',
 | 
			
		||||
        wasm.calculatePiLeibnizAs,
 | 
			
		||||
        PI_ITERATIONS,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    // Benchmark: Count Primes
 | 
			
		||||
    runBenchmark(
 | 
			
		||||
        'TypeScript: Count Primes (Trial Division)',
 | 
			
		||||
        countPrimesTs,
 | 
			
		||||
        PRIME_LIMIT,
 | 
			
		||||
    )
 | 
			
		||||
    runBenchmark(
 | 
			
		||||
        'AssemblyScript: Count Primes (Trial Division)',
 | 
			
		||||
        wasm.countPrimesAs,
 | 
			
		||||
        PRIME_LIMIT,
 | 
			
		||||
    )
 | 
			
		||||
 | 
			
		||||
    console.log('\nBenchmark finished.')
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										21
									
								
								apps/server/src/wasm/index.ts
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										21
									
								
								apps/server/src/wasm/index.ts
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,21 @@
 | 
			
		||||
import { __AdaptedExports, instantiate } from './release'
 | 
			
		||||
 | 
			
		||||
import * as path from 'node:path'
 | 
			
		||||
import * as fs from 'node:fs/promises'
 | 
			
		||||
 | 
			
		||||
export async function InitWasm(): Promise<typeof __AdaptedExports> {
 | 
			
		||||
    return new Promise(async resolve => {
 | 
			
		||||
        const wasmFilePath = path.resolve(__dirname, 'release.wasm')
 | 
			
		||||
 | 
			
		||||
        const wasm: typeof __AdaptedExports = await instantiate(
 | 
			
		||||
            await (async () => {
 | 
			
		||||
                return globalThis.WebAssembly.compile(
 | 
			
		||||
                    await fs.readFile(wasmFilePath),
 | 
			
		||||
                )
 | 
			
		||||
            })(),
 | 
			
		||||
            { env: '' },
 | 
			
		||||
        )
 | 
			
		||||
 | 
			
		||||
        resolve(wasm)
 | 
			
		||||
    })
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										24
									
								
								apps/server/src/wasm/release.d.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								apps/server/src/wasm/release.d.ts
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
			
		||||
declare namespace __AdaptedExports {
 | 
			
		||||
  /**
 | 
			
		||||
   * assembly/index/calculatePiLeibnizAs
 | 
			
		||||
   * @param iterations `i32`
 | 
			
		||||
   * @returns `f64`
 | 
			
		||||
   */
 | 
			
		||||
  export function calculatePiLeibnizAs(iterations: number): number;
 | 
			
		||||
  /**
 | 
			
		||||
   * assembly/index/countPrimesAs
 | 
			
		||||
   * @param limit `i32`
 | 
			
		||||
   * @returns `i32`
 | 
			
		||||
   */
 | 
			
		||||
  export function countPrimesAs(limit: number): number;
 | 
			
		||||
  /**
 | 
			
		||||
   * assembly/index/addAs
 | 
			
		||||
   * @param a `i32`
 | 
			
		||||
   * @param b `i32`
 | 
			
		||||
   * @returns `i32`
 | 
			
		||||
   */
 | 
			
		||||
  export function addAs(a: number, b: number): number;
 | 
			
		||||
}
 | 
			
		||||
/** Instantiates the compiled WebAssembly module with the given imports. */
 | 
			
		||||
export declare function instantiate(module: WebAssembly.Module, imports: {
 | 
			
		||||
}): Promise<typeof __AdaptedExports>;
 | 
			
		||||
							
								
								
									
										4
									
								
								apps/server/src/wasm/release.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										4
									
								
								apps/server/src/wasm/release.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,4 @@
 | 
			
		||||
export async function instantiate(module, imports = {}) {
 | 
			
		||||
  const { exports } = await WebAssembly.instantiate(module, imports);
 | 
			
		||||
  return exports;
 | 
			
		||||
}
 | 
			
		||||
@ -4,6 +4,7 @@
 | 
			
		||||
    "resolveJsonModule": true,
 | 
			
		||||
    "baseUrl": "./src",
 | 
			
		||||
    "types": [
 | 
			
		||||
      "node",
 | 
			
		||||
      "../../node_modules/@ragempcommunity/types-server/",
 | 
			
		||||
      "../shared/declarations/rage-fw-shared-types/"
 | 
			
		||||
    ]
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										
											BIN
										
									
								
								server/release.wasm
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										
											BIN
										
									
								
								server/release.wasm
									
									
									
									
									
										Normal file
									
								
							
										
											Binary file not shown.
										
									
								
							@ -1,7 +1,7 @@
 | 
			
		||||
{
 | 
			
		||||
  "compilerOptions": {
 | 
			
		||||
    "target": "ESNext",
 | 
			
		||||
    "lib": ["ESNext", "ES2019", "DOM"],
 | 
			
		||||
    "lib": ["ESNext", "DOM"],
 | 
			
		||||
    "moduleResolution": "node",
 | 
			
		||||
    "module": "ESNext",
 | 
			
		||||
    "resolveJsonModule": true,
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user