Пример #1
0
int main() {
    const int n {5};
    double data[n] {1.2, 2.3, 3.4, 4.5, 5.6};
    Stats stats;
    for (int i = 0; i < n; i++)
        stats.add(data[i]);
    std::cout << "average = " << stats.avg() << " for "
              << stats.n() << " data points" << std::endl;
    return 0;
}
Пример #2
0
 void add(StatsData &d, bool enable_first, bool enable_last) {
     if (first_latency_ms == NULL) {
         initStats(enable_first, enable_last);
     }
     if (first_latency_ms) {
         first_latency_ms->add(*d.first_latency_ms);
     }
     if (last_latency_ms) {
         last_latency_ms->add(*d.last_latency_ms);
     }
     duplicates->add(*d.duplicates);
 }
Пример #3
0
    void execute(ConnectionPool::Entry & connection, Query & query)
    {
        Stopwatch watch;
        RemoteBlockInputStream stream(*connection, query, &settings, nullptr, Tables(), query_processing_stage);

        Progress progress;
        stream.setProgressCallback([&progress](const Progress & value) { progress.incrementPiecewiseAtomically(value); });

        stream.readPrefix();
        while (Block block = stream.read())
            ;
        stream.readSuffix();

        const BlockStreamProfileInfo & info = stream.getProfileInfo();

        double seconds = watch.elapsedSeconds();

        std::lock_guard<std::mutex> lock(mutex);
        info_per_interval.add(seconds, progress.rows, progress.bytes, info.rows, info.bytes);
        info_total.add(seconds, progress.rows, progress.bytes, info.rows, info.bytes);
    }
Пример #4
0
int main() {
    double value;
    Stats stats;
    while (std::cin >> value) {
        stats.add(value);
    }
    try {
        std::cout << "mean: " << stats.mean() << std::endl;
        std::cout << "stddev: " << stats.stddev() << std::endl;
        std::cout << "min: " << stats.min() << std::endl;
        std::cout << "max: " << stats.max() << std::endl;
        std::cout << "median: " << stats.median() << std::endl;
        std::cout << "n: " << stats.nr_values() << std::endl;
    } catch (std::out_of_range& e) {
        std::cerr << "### error: " << e.what() << std::endl;
        std::exit(1);
    }
    return 0;
}
Пример #5
0
// worker requests items from the queue which are either file or
// directory paths. If item is
// 1) a filepath: computes and prints the hash of the file
// 2) a directory path: adds contained files and directories contained to
//    the queue
void worker(StringQueue& fileQueue, const Printer& printer, RefData& rd,
  Stats& stats, CmdLineOpts& opts) {

  // if we receive a non-empty refMap we compare against it
  bool compare = false;
  if (!rd.refMap.empty()) {
    compare = true;
  }

  while (!fileQueue.done()) {
    auto path = fileQueue.try_and_wait();
    if (path.empty()) {
      break;
    }

    // check if path is a directory or a file
    struct stat info;
    if (lstat(path.c_str(), &info) < 0) {
      printer.cerr("lstat failed on " + path);
      continue;
    }
    if (S_ISREG(info.st_mode)) {
      std::string hash = hasher(opts.hashMethod, path);
      if (opts.collectStats) {
        stats.add(info.st_size);
      }
      if (compare) {
        compare_to_reference(path, hash, printer, rd);
      } else {
        printer.cout(opts.hashMethod + " , " + path + " , " + hash);
      }
    } else if (S_ISDIR(info.st_mode)) {
      add_directory(fileQueue, path, printer);
    }
  }
}
Пример #6
0
int main()
{
  {
    Stats stats(empty_usage, -1, empty_args);
    stats.add(empty_usage, 0, empty_args);
    assert(stats.buffer_max == 1);
    assert(stats.options_max == 1);
    Option buffer[stats.buffer_max];
    Option options[stats.options_max];
    Parser parse(empty_usage, 99, empty_args, options, buffer);
    parse.parse(empty_usage, -1, empty_args, options, buffer);
    assert(parse.optionsCount() == 0);
    assert(parse.nonOptionsCount() == 0);
    assert(!buffer[0]);
    assert(!options[0]);
    assert(buffer[0].count()==0);
    assert(parse.nonOptions()==0);

    stats.add(empty_usage, 3, non_options);
    assert(stats.buffer_max == 1);
    assert(stats.options_max == 1);
    parse.parse(empty_usage, 3, non_options, options, buffer);
    assert(parse.optionsCount() == 0);
    assert(parse.nonOptionsCount() == 3);
    assert(!buffer[0]);
    assert(!options[0]);
    assert(parse.nonOptions()==&non_options[0]);

    stats.add(minimal_usage, -1, unknown_option);
    assert(stats.buffer_max == 1);
    assert(stats.options_max == 2);
    parse.parse(minimal_usage, -1, unknown_option, options, buffer);
    assert(parse.optionsCount() == 0);
    assert(parse.nonOptionsCount() == 1);
    assert(!buffer[0]);
    assert(!options[0]);
    assert(parse.nonOptions()==&unknown_option[1]);
  }
  {
    Stats stats(gettext_usage, -1, lone_minus);
    Stats stats2;
    stats2.add(gettext_usage, -1, lone_minus);
    assert(stats.buffer_max == 2);
    assert(stats.options_max == 2);
    assert(stats2.buffer_max == 2);
    assert(stats2.options_max == 2);
    Option buffer[stats.buffer_max];
    Option options[stats.options_max];
    Parser parse;
    parse.parse(gettext_usage, -1, lone_minus, options, buffer);
    assert(parse.optionsCount() == 1);
    assert(parse.nonOptionsCount() == 1);
    assert(parse.nonOptions()==&lone_minus[2]);
    assert(options[0]);
    assert(buffer[0]);
    assert(options[0].count()==1);
    assert(options[0].isFirst());
    assert(options[0].isLast());
    assert(options[0].first() == options[0]);
    assert(options[0].last() == options[0]);
    assert(options[0].prevwrap() == &options[0]);
    assert(options[0].nextwrap() == &options[0]);
    assert(options[0].prev() == 0);
    assert(options[0].next() == 0);
    assert(options[0].desc == &gettext_usage[0]);
    assert(eq(options[0].name, "f"));
    assert(eq(options[0].arg, "-"));
  }
  {
    Stats stats(optional_usage, -1, lone_minus);
    Stats stats2;
    stats2.add(optional_usage, -1, lone_minus);
    assert(stats.buffer_max == 2);
    assert(stats.options_max == 2);
    assert(stats2.buffer_max == 2);
    assert(stats2.options_max == 2);
    Option buffer[stats.buffer_max];
    Option options[stats.options_max];
    Parser parse;
    parse.parse(optional_usage, -1, lone_minus, options, buffer);
    assert(parse.optionsCount() == 1);
    assert(parse.nonOptionsCount() == 1);
    assert(parse.nonOptions()==&lone_minus[2]);
    assert(options[0]);
    assert(buffer[0]);
    assert(options[0].count()==1);
    assert(options[0].isFirst());
    assert(options[0].isLast());
    assert(options[0].first() == options[0]);
    assert(options[0].last() == options[0]);
    assert(options[0].prevwrap() == &options[0]);
    assert(options[0].nextwrap() == &options[0]);
    assert(options[0].prev() == 0);
    assert(options[0].next() == 0);
    assert(options[0].desc == &optional_usage[0]);
    assert(eq(options[0].name, "f"));
    assert(eq(options[0].arg, "-"));
  }
  {
    Stats stats;
    stats.add(minimal_usage, -1, lone_doubleminus);
    assert(stats.buffer_max == 1);
    assert(stats.options_max == 2);
    Option buffer[stats.buffer_max];
    Option options[stats.options_max];
    Parser parse(minimal_usage, -1, lone_doubleminus, options, buffer);
    assert(parse.optionsCount() == 0);
    assert(parse.nonOptionsCount() == 0);
    assert(!buffer[0]);
    assert(!options[0]);
    assert(parse.nonOptions()==0);
  }
  {
    Stats stats;
    stats.add(multi_usage, count(multi1), multi1, 4, true);
    assert(stats.buffer_max == 6);
    assert(stats.options_max == 7);
    stats.add(multi_usage, count(multi2), multi2, 4, true);
    assert(stats.buffer_max == 14);
    assert(stats.options_max == 7);
    stats.add(multi_usage, count(multi3), multi3, 4, true);
    assert(stats.buffer_max == 22);
    assert(stats.options_max == 7);
    Option buffer[stats.buffer_max];
    Option options[stats.options_max];
    assert(options[FOO].last()->type() == UNUSED);
    assert(options[ABBREVIATE].count()==0);
    Parser parse;
    assert(!parse.error());

    parse.parse(multi_usage, count(multi1), multi1, options, buffer, 4, true);
    assert(!parse.error());
    assert(parse.optionsCount() == 5);
    assert(parse.nonOptionsCount() == 1);
    assert(eq(parse.nonOptions()[0],"--strangefilename"));
    assert(options[FOO].last()->type() == ENABLED);
    assert(eq(options[FOO].last()->name, "--enable-foo"));
    assert(options[FOO].last()->arg == 0);
    assert(options[UNKNOWN].count() == 2);
    assert(eq(options[UNKNOWN].first()->name,"--unknown1"));
    assert(eq(options[UNKNOWN].last()->name,"u"));
    assert(options[UNKNOWN].first()->arg == 0);
    assert(options[UNKNOWN].last()->arg == 0);
    assert(options[VERBOSE].count()==1);
    assert(options[VERBOSE].arg==0);
    assert(options[VERBOSE].name[0] == 'v' && options[VERBOSE].namelen == 1);
    assert(eq(options[X].arg,"xyzzy"));
    assert(eq(options[X].name,"X"));

    parse.parse(multi_usage, count(multi2), multi2, options, buffer, 4, true);
    assert(!parse.error());
    assert(parse.optionsCount() == 13);
    assert(parse.nonOptionsCount() == 1);
    assert(eq(parse.nonOptions()[0],"--strangefilename"));
    assert(options[FOO].last()->type() == DISABLED);
    assert(options[FOO].last()->arg == 0);
    assert(options[UNKNOWN].count() == 2);
    assert(eq(options[UNKNOWN].first()->name,"--unknown1"));
    assert(eq(options[UNKNOWN].last()->name,"u"));
    assert(options[VERBOSE].count()==4);
    assert(options[X].count()==5);
    const char* Xargs[] = { "xyzzy", "foo", "bar", "foobar", "", "sentinel" };
    const char** Xarg = &Xargs[0];
    for (Option* Xiter = options[X]; Xiter != 0; Xiter = Xiter->next())
      assert(eq(Xiter->arg, *Xarg++));

    assert(!options[ABBREVIATE]);
    parse.parse(multi_usage, count(multi3), multi3, options, buffer, 4, true);
    assert(!parse.error());
    assert(parse.optionsCount() == 21);
    assert(parse.nonOptionsCount() == 2);
    assert(eq(parse.nonOptions()[0],"nonoption1"));
    assert(eq(parse.nonOptions()[1],"nonoption2"));
    assert(options[ABBREVIATE]);
    assert(options[EMPTY].count()==3);
    assert(options[EMPTY].first()->arg==0);
    assert(eq(options[EMPTY].last()->arg,""));
    assert(eq(options[EMPTY].last()->prev()->arg,""));
    assert(options[FOO].last()->type() == DISABLED);
    assert(options[UNKNOWN].count() == 5);
    assert(eq(options[UNKNOWN].first()->name,"--unknown1"));
    assert(options[UNKNOWN].first()->arg == 0);
    assert(eq(options[UNKNOWN].last()->name,"b"));
    assert(options[VERBOSE].count()==5);
    assert(options[X].count()==5);
    Xarg = &Xargs[0];
    for (Option* Xiter = options[X]; Xiter != 0; Xiter = Xiter->next())
      assert(eq(Xiter->arg, *Xarg++));

    for (Option* opt = buffer[0]; *opt; ++opt)
      if (opt->desc->check_arg != Arg::Required && opt->desc->check_arg != Arg::Empty)
        assert(opt->arg == 0);
  }
  {
    Option buffer[2];
    Option options[20];
    Parser parse;
    assert(!parse.error());
    parse.parse(multi_usage, -1, illegal, options, buffer, 0, false, 2);
    assert(parse.error());
  }
  {
    Stats stats(multi_usage, count(multi3), multi3, 0, true);
    const int bufmax = 3;
    Option buffer[bufmax];
    Option options[stats.options_max];
    assert(!options[ABBREVIATE]);
    Parser parse(multi_usage, count(multi3), multi3, options, buffer, 4, true, bufmax);
    assert(!parse.error());
    assert(parse.optionsCount() == bufmax);
    assert(parse.nonOptionsCount() == 2);
    assert(eq(parse.nonOptions()[0],"nonoption1"));
    assert(eq(parse.nonOptions()[1],"nonoption2"));
    assert(options[ABBREVIATE]);
    assert(options[UNKNOWN].count() == 2); // because of buxmax the 2nd 'b' cannot be stored
    assert(options[UNKNOWN].first()->name[0] == 'a' && options[UNKNOWN].first()->namelen == 1);
    assert(options[UNKNOWN].first()->arg == 0);
    assert(eq(options[UNKNOWN].last()->name,"bb"));
  }
  {
    Stats stats(true, multi_usage, -1, reorder);
    Option buffer[stats.buffer_max];
    Option options[stats.options_max];
    Parser parse(true, multi_usage, -1, reorder, options, buffer);
    assert(!parse.error());
    assert(parse.optionsCount() == 3);
    assert(parse.nonOptionsCount() == 4);
    assert(parse.nonOptions() == &reorder[6]);
  }
  {
    Option buffer[10];
    Option options[10];
    Parser parse(true, multi_usage, 666, reorder2, options, buffer, 0, false, 10);
    assert(!parse.error());
    assert(parse.optionsCount() == 2);
    assert(parse.nonOptionsCount() == 2);
  }

  fprintf(stdout, "All tests passed.\n");
  return 0;
}