package cn.fateverse.query;

import cn.fateverse.common.code.engine.JavaScriptEngine;
import cn.fateverse.common.code.model.EngineResult;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class SystemOut {
    public static void outTest() {
        ByteArrayOutputStream baoStream = new ByteArrayOutputStream(1024);
        PrintStream cacheStream = new PrintStream(baoStream);//临时输出
        PrintStream oldStream = System.out;//缓存系统输出
        System.setOut(cacheStream);
        System.out.print("...");//不会打印到控制台
        String result = baoStream.toString();//存放控制台输出的字符串
        System.setOut(oldStream);//还原到系统输出
        System.out.println("result");
    }

    public static void main(String[] args) {
//        outTest();

        String finalCode = "function execute1(data) {\n" +
                "    console.log(data)\n" +
                "    console.log(\"test console!\")\n" +
                "    let add = (a, b) => a + b; // 使用箭头函数\n" +
                "    console.log(\"Adding 5 and 3:\", add(5, 3));\n\n" +
                "    for (let i = 0; i < 1000; i++) {\n" +
                "        console.log(i)\n" +
                "    }" +
                "    return \"success\";\n" +
                "}";
//        String finalCode = "function myFunction() { return 'Hello, world!'; }";
        long start1 = System.currentTimeMillis();
        EngineResult execute = JavaScriptEngine.execute(finalCode, "execute1", false, "测试控制台输出");
        long start2 = System.currentTimeMillis();
        EngineResult execute1 = JavaScriptEngine.execute(finalCode, "execute1", false, "测试控制台输出");
        long start3 = System.currentTimeMillis();
        System.out.println("耗时1：" + (start2 - start1) + "ms");
        System.out.println("耗时2：" + (start3 - start2) + "ms");
        System.out.println(execute);

    }
}