blob: 48deb07c12740feb82be22b096bbe464a565539e [file] [log] [blame]
// Copyright 2019 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <string>
#include <vector>
#include "base/containers/flat_map.h"
#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/optional.h"
#include "base/time/time.h"
namespace perf_test {
// Generates TestResultEntry dict in LUCI Test Results format.
// See: go/luci-test-results-design
// //infra/go/src/
class LuciTestResult {
// Represents a test result status.
enum class Status {
// The test status is unspecified.
// The test has passed.
// The test has failed.
// The test did not complete because it crashed.
// The test did not complete because it was interrupted, e.g. timeout.
// The test or test framework decided not to run the test, or the test was
// not run due to previous tests timing out.
// Represents an artifact.
struct Artifact {
Artifact(const Artifact& other);
Artifact(const base::FilePath file_path, const std::string& content_type);
Artifact(const std::string& contents, const std::string& content_type);
// Use only one of the two fields below.
// Absolute path on the same machine running the test.
base::Optional<base::FilePath> file_path;
// The data of the artifact.
base::Optional<std::string> contents;
std::string content_type;
// Represents a tag.
struct Tag {
std::string key;
std::string value;
LuciTestResult(const LuciTestResult& other);
LuciTestResult(LuciTestResult&& other);
// Helper to create a LuciTestResult and fill in info for the current gtest.
static LuciTestResult CreateForGTest();
// Adds a variant key-value pair to |extra_variant_pairs_|. See VariantDef in
// //infra/go/src/
// for more details.
void AddVariant(const std::string& key, const std::string& value);
// Adds an output artifact.
void AddOutputArtifactFile(const std::string& artifact_name,
const base::FilePath& file_path,
const std::string& content_type);
void AddOutputArtifactContents(const std::string& artifact_name,
const std::string& contents,
const std::string& content_type);
// Adds a tag.
void AddTag(const std::string& key, const std::string& value);
// Writes to |result_file|.
void WriteToFile(const base::FilePath& result_file) const;
// Getters and setters.
const std::string& test_path() const { return test_path_; }
void set_test_path(const std::string& test_path) { test_path_ = test_path; }
const base::flat_map<std::string, std::string>& extra_variant_pairs() const {
return extra_variant_pairs_;
Status status() const { return status_; }
void set_status(Status status) { status_ = status; }
bool is_expected() const { return is_expected_; }
void set_is_expected(bool is_expcted) { is_expected_ = is_expcted; }
base::Time start_time() const { return start_time_; }
void set_start_time(base::Time start_time) { start_time_ = start_time; }
base::TimeDelta duration() const { return duration_; }
void set_duration(base::TimeDelta duration) { duration_ = duration; }
const base::flat_map<std::string, Artifact>& output_artifacts() const {
return output_artifacts_;
const std::vector<Tag>& tags() const { return tags_; }
// For gtest, |test_path_| is <test_suite_name>.<test_case_name>, without
// the param annotations. E.g. "InstantiationName/SuiteName.CaseName/0"
// will have "/0" stripped and be just "InstantiationName/SuiteName.CaseName".
std::string test_path_;
// For gtest, |extra_variant_pairs_| holds info about the type param and
// value param for typed/parameterized tests.
base::flat_map<std::string, std::string> extra_variant_pairs_;
// Status of the test result.
Status status_ = Status::kUnspecified;
// Whether |status| is expected.
bool is_expected_ = false;
// Test start time.
base::Time start_time_;
// Duration of the test.
base::TimeDelta duration_;
// Artifacts of the test run.
base::flat_map<std::string, Artifact> output_artifacts_;
// Tags of the test run.
std::vector<Tag> tags_;
} // namespace perf_test