public IEnumerator DataLogger_FlushesToFileSystemOnlyWithMaxBufferSize()
    {
        var logger = new Logger("SimLog", bufferSize: 65536, maxElapsedSeconds: -1);

        logger.Log(new TestLog()
        {
            msg = "Test Simulation Log!"
        });
        yield return(new WaitForSeconds(5));

        var path = Path.Combine(Configuration.Instance.GetStoragePath(), "Logs", "SimLog_0.txt");

        Assert.IsTrue(!File.Exists(path));
        for (int i = 0; i < 100; i++)
        {
            logger.Log(new TestLog()
            {
                msg = "Test Simulation Log"
            });
        }
        logger.Flushall(true);
        Assert.IsTrue(File.Exists(path));

        var fileContents = File.ReadAllLines(path);

        Assert.AreEqual(fileContents.Length, 101);
    }
    public IEnumerator DataLogger_CustomFilenameSuffix()
    {
        var logger = new Logger("testLog", bufferSize: 10, maxElapsedSeconds: -1, customSuffix: () => DateTime.Now.ToString("yy-MM-dd"));
        var path   = Path.Combine(Configuration.Instance.GetStoragePath(), "Logs");

        logger.Log(new TestLog()
        {
            msg = "This is a test log with timestamp and seq"
        });
        logger.Log(new TestLog()
        {
            msg = "This is a another test log"
        });
        logger.Flushall(true);
        var files = Directory.GetFiles(path);

        Assert.IsTrue(files.Length > 0, "No Logs created");
        int start = 0;

        foreach (var file in files)
        {
            var fn = Path.GetFileName(file).Split('_');
            Assert.IsTrue(fn.Length == 3, "Invalid file name: " + file);
            var      timestamp = fn[1];
            var      seq       = fn[2].Split('.')[0];
            DateTime dt;
            Assert.IsTrue(DateTime.TryParseExact(timestamp, "yy-MM-dd",
                                                 CultureInfo.InvariantCulture,
                                                 DateTimeStyles.None, out dt), "Not a valid Data: " + timestamp);
            Assert.IsTrue(start++ == Int32.Parse(seq));
        }
        yield return(null);
    }
    public IEnumerator DataLogger_TimestampSuffix()
    {
        var logger = new Logger("testLog", maxElapsedSeconds: -1, suffixOption: LoggerSuffixOption.TIME_STAMP);
        var path   = Path.Combine(Configuration.Instance.GetStoragePath(), "Logs");

        logger.Log(new TestLog()
        {
            msg = "This is a test log with timestamp"
        });
        logger.Flushall(true);
        var files = Directory.GetFiles(path);

        Assert.IsTrue(files.Length > 0, "No Logs created");
        foreach (var file in files)
        {
            var fn        = Path.GetFileName(file).Split('_')[1];
            var timestamp = fn.Split('.')[0];
            Assert.IsTrue(timestamp.Length > 0, "Invalid file name: " + file);
            DateTime dt;
            Assert.IsTrue(DateTime.TryParseExact(timestamp, "yyyy-MM-ddThh-mm-ss",
                                                 CultureInfo.InvariantCulture,
                                                 DateTimeStyles.None, out dt), "Not a valid Data: " + timestamp);
        }
        yield return(null);
    }
Пример #4
0
    // Exit sim after simulation has ran for quitAfterSeconds defined in AppParams.
    private void Update()
    {
        simElapsedSeconds += Time.deltaTime;

        if (simElapsedSeconds >= quitAfterSeconds)
        {
            // Flush all Cube data point data to file before exiting
            cubeLogger.Flushall();
            Application.Quit();
        }
    }
Пример #5
0
    // Start is called before the first frame update
    void Start()
    {
        objDict = new Dictionary <string, GameObject>();
        objDict.Add("cube", CubePrefab);
        objDict.Add("sphere", SpherePrefab);

        // Create a specific logger for AppParams for debugging purposes
        paramLogger       = new Unity.Simulation.Logger("ParamReader");
        cubeLogger        = new Unity.Simulation.Logger("CubeLogger");
        simElapsedSeconds = 0;

        // NOTE: AppParams can be loaded anytime except during `RuntimeInitializeLoadType.BeforeSceneLoad`
        // If the simulation is running locally load app_param_0.json
        if (!Configuration.Instance.IsSimulationRunningInCloud())
        {
            string app_param_name = AppParamToggle ? "mountain_app_param_1.json" : "mountain_app_param_0.json";
            Configuration.Instance.SimulationConfig.app_param_uri =
                "file://" + Application.dataPath + "/StreamingAssets/" + app_param_name;
            Debug.Log(Configuration.Instance.SimulationConfig.app_param_uri);
        }

        appParams = Configuration.Instance.GetAppParams <MountainParam>();

        // Check if AppParam file was passed during command line execution
        if (appParams != null)
        {
            // Log AppParams to Player.Log file and Editor Console
            Debug.Log(appParams.ToString());

            // Log AppParams to DataLogger
            paramLogger.Log(appParams);
            paramLogger.Flushall();

            // Update the screen capture interval through an app-param
            float screenCaptureInterval = Mathf.Min(Mathf.Max(0, appParams.screenCaptureInterval), 100.0f);
            GameObject.FindGameObjectsWithTag("DataCapture")[0].GetComponent <CameraGrab>()._screenCaptureInterval =
                screenCaptureInterval;

            // Set the Simulation exit time.
            quitAfterSeconds = appParams.quitAfterSeconds;
            red          = appParams.red;
            green        = appParams.green;
            blue         = appParams.blue;
            whichObjects = appParams.whichObjects;
        }
        else
        {
            Debug.Log("NULL");
        }
    }
    // Create and Log a vector
    void Start()
    {
        // Print the location where data will be written
        Debug.Log(Application.persistentDataPath + "/" + Configuration.Instance.GetAttemptId());

        // Create new data logger with output files named DataCapture
        dataLogger = new Unity.Simulation.Logger("DataCapture");

        Vector3 examplePosition = new Vector3(0, 1, 2);

        //Create a new data point
        ObjectPosition objectPosition = new ObjectPosition(examplePosition, "ExampleObjectName");

        dataLogger.Log(objectPosition);

        // Flush written objectPosition to file
        dataLogger.Flushall();
    }
Пример #7
0
    // Exit sim after simulation has ran for quitAfterSeconds defined in AppParams.
    void Update()
    {
        simElapsedSeconds += Time.deltaTime;

        if (curNumCubes <= appParams.replicateCube && simElapsedSeconds - lastCubeCreated > cubeCadence)
        {
            ReplicateCube(curNumCubes);
            lastCubeCreated = simElapsedSeconds;
            curNumCubes    += 1;
        }

        if (simElapsedSeconds >= quitAfterSeconds)
        {
            Debug.Log("Quitting...");
            // Flush all Cube data point data to file before exiting
            cubeLogger.Flushall();
            Application.Quit();
        }
    }
Пример #8
0
    private void Update()
    {
        // Get total time sim elapsed
        simElapsed += Time.deltaTime;

        // Sim has hit duration, flush all data and quit application
        if (simElapsed >= duration && !quit)
        {
            dataLogger.Flushall();
            quit = true;
            Debug.Log("Quitting...");
            Application.Quit();
        }

        // Capture Data if last time capture time was over 1 second ago
        if ((int)simElapsed - lastCapture >= captureInterval && !quit)
        {
            Capture(lastCapture);
            lastCapture = (int)simElapsed;
        }
    }
Пример #9
0
    void Start()
    {
        // Create a specific logger for AppParams for debugging purposes
        paramLogger       = new Unity.Simulation.Logger("ParamReader");
        cubeLogger        = new Unity.Simulation.Logger("CubeLogger");
        simElapsedSeconds = 0;

        // NOTE: AppParams can be loaded anytime except during `RuntimeInitializeLoadType.BeforeSceneLoad`
        // If the simulation is running locally load app_param_0.json
        if (!Configuration.Instance.IsSimulationRunningInCloud())
        {
            Configuration.Instance.SimulationConfig.app_param_uri = "file://" + Application.dataPath + "/StreamingAssets/app_param_0.json";
            Debug.Log(Configuration.Instance.SimulationConfig.app_param_uri);
        }

        appParams = Configuration.Instance.GetAppParams <CubeAppParam>();

        // Check if AppParam file was passed during command line execution
        if (appParams != null)
        {
            // Log AppParams to Player.Log file and Editor Console
            Debug.Log(appParams.ToString());

            // Log AppParams to DataLogger
            paramLogger.Log(appParams);
            paramLogger.Flushall();

            // Update the screen capture interval through an app-param
            float screenCaptureInterval = Mathf.Min(Mathf.Max(0, appParams.screenCaptureInterval), 100.0f);
            GameObject.FindGameObjectsWithTag("DataCapture")[0].GetComponent <CameraGrab>()._screenCaptureInterval = screenCaptureInterval;
            // ReplicateCube
            ReplicateCube(GameObject.FindGameObjectsWithTag("Cube")[0], appParams.replicateCube);

            // Set the Simulation exit time.
            quitAfterSeconds = appParams.quitAfterSeconds;
        }
    }
Пример #10
0
    public IEnumerator CaptureTest_CaptureLogAlignment()
    {
        const int    NumFramesToCapture      = 10;
        const double RequestTimeoutInSeconds = NumFramesToCapture * 10.0;

        Assert.Greater(
            NumFramesToCapture,
            0,
            $"The number of frames to capture is set to {NumFramesToCapture}, but it must be greater than 0"
            );

        string screenCapturePath = Manager.Instance.GetDirectoryFor(DataCapturePaths.ScreenCapture);

        var camera   = SetupCameraTestWithMaterial(8, GraphicsFormat.R8G8B8A8_UNorm, new Vector3(0.0f, 0.0f, 500.5f));
        var logger   = new Unity.Simulation.Logger("DataCapture", suffixOption: LoggerSuffixOption.TIME_STAMP);
        var wrappers = new List <AsyncRequestWrapper>();
        var timer    = new Stopwatch();

        yield return(null);

        timer.Start();

        Debug.Log($"Rendering and capturing {NumFramesToCapture} frames ...");

        // Render the frame and capture them.
        for (int i = 0; i < NumFramesToCapture; ++i)
        {
            wrappers.Add(CaptureFrameWithLog(camera, logger, screenCapturePath, "image", i));
            camera.Render();

            yield return(null);
        }

        Debug.Log("Waiting for captures to complete ...");

        bool allCompleted = false;

        // Wait for the tasks to complete.
        while (timer.Elapsed.TotalSeconds < RequestTimeoutInSeconds)
        {
            // This has to a manual polling loop so we can yield, allowing async requests to complete.
            // Attempting to block the thread here will prevent the requests from progressing.
            yield return(null);

            // The default assumption is that all requests have completed.
            allCompleted = true;

            foreach (var wrapper in wrappers)
            {
                if (!wrapper.Request.completed)
                {
                    // We don't need to check the remainder of the requests if we
                    // encounter one that has not finished running.
                    allCompleted = false;
                    break;
                }
            }

            if (allCompleted)
            {
                // All requests have been completed, so we can proceed.
                break;
            }
        }

        Assert.IsTrue(allCompleted, "Timed out waiting for capture requests to complete");
        Debug.Log($"Time (in seconds) to complete all {NumFramesToCapture} capture requests: {timer.Elapsed.TotalSeconds}");

        // Check for errors that may have occured during the capture requests.
        foreach (var wrapper in wrappers)
        {
            Assert.IsFalse(wrapper.Request.error, $"Frame capture request ({wrapper.FrameIndex}) encountered an error");
            yield return(null);
        }

        // Flush the log to disk.
        logger.Flushall();

        // This yield must be here so the log is actually written to disk.
        yield return(null);

        // Read the full contents of the log file.
        string[] fileLines          = File.ReadAllLines(logger.GetPath());
        int      expectedImageIndex = 0;

        Debug.Log($"Validating log entries ...");

        // Inspect the lines of the log to make sure each one matches the expected image file.
        foreach (string line in fileLines)
        {
            // Decode the json, then extract the image index that was logged for this line.
            var data       = JsonUtility.FromJson <CaptureFrameWithLogDataPoint>(line);
            int imageIndex = int.Parse(data.imageName.Split('_')[1]);

            Assert.AreEqual(
                expectedImageIndex,
                imageIndex,
                $"Capture log line ({expectedImageIndex}) references incorrect image: {data.imageName}"
                );

            ++expectedImageIndex;

            yield return(null);
        }

        Assert.AreEqual(
            NumFramesToCapture,
            expectedImageIndex,
            $"Log contained {expectedImageIndex} lines, but expected: {NumFramesToCapture}"
            );
    }