// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "media/gpu/chromeos/perf_test_util.h"

#include "base/containers/span.h"
#include "base/files/file_util.h"
#include "base/json/json_writer.h"
#include "base/logging.h"
#include "base/no_destructor.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace media {

media::test::VideoTestEnvironment* g_env;

base::FilePath& GetOutputDir() {
  static base::NoDestructor<base::FilePath> dir(
      base::FilePath::kCurrentDirectory);
  return *dir;
}

base::FilePath& GetSourceDir() {
  static base::NoDestructor<base::FilePath> dir(
      base::FilePath::kCurrentDirectory);
  return *dir;
}

void WriteJsonResult(std::vector<std::pair<std::string, double>> data) {
  base::DictValue metrics;
  for (auto i : data) {
    metrics.Set(i.first, i.second);
  }

  const auto output_folder_path = GetOutputDir();
  std::string metrics_str;
  ASSERT_TRUE(base::JSONWriter::WriteWithOptions(
      metrics, base::JSONWriter::OPTIONS_PRETTY_PRINT, &metrics_str));
  const base::FilePath metrics_file_path = output_folder_path.Append(
      g_env->GetTestOutputFilePath().AddExtension(FILE_PATH_LITERAL(".json")));
  // Make sure that the directory into which json is saved is created.
  LOG_ASSERT(base::CreateDirectory(metrics_file_path.DirName()));
  base::File metrics_output_file(
      base::FilePath(metrics_file_path),
      base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
  ASSERT_TRUE(metrics_output_file.WriteAtCurrentPosAndCheck(
      base::as_byte_span(metrics_str)));
  LOG(INFO) << "Wrote performance metrics to: " << metrics_file_path;
}

}  // namespace media
