Blame bus/test-stress.c

Packit 3ff832
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
Packit 3ff832
/* vim:set et sts=4: */
Packit 3ff832
/* bus - The Input Bus
Packit 3ff832
 * Copyright (C) 2010 Google Inc.
Packit 3ff832
 *
Packit 3ff832
 * This library is free software; you can redistribute it and/or
Packit 3ff832
 * modify it under the terms of the GNU Lesser General Public
Packit 3ff832
 * License as published by the Free Software Foundation; either
Packit 3ff832
 * version 2.1 of the License, or (at your option) any later version.
Packit 3ff832
 *
Packit 3ff832
 * This library is distributed in the hope that it will be useful,
Packit 3ff832
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff832
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 3ff832
 * Lesser General Public License for more details.
Packit 3ff832
 *
Packit 3ff832
 * You should have received a copy of the GNU Lesser General Public
Packit 3ff832
 * License along with this library; if not, write to the Free Software
Packit 3ff832
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
Packit 3ff832
 * USA
Packit 3ff832
 */
Packit 3ff832
#include <stdlib.h>
Packit 3ff832
#include <time.h>
Packit 3ff832
Packit 3ff832
#include <ibus.h>
Packit 3ff832
#include <locale.h>
Packit 3ff832
#include <glib.h>
Packit 3ff832
#include "test-client.h"
Packit 3ff832
Packit 3ff832
#define MAX_SEND_KEY_NUM 100
Packit 3ff832
#define MAX_RANDOM_SPACE 5
Packit 3ff832
Packit 3ff832
static gboolean
Packit 3ff832
_sleep_cb (gpointer user_data)
Packit 3ff832
{
Packit 3ff832
    ibus_quit ();
Packit 3ff832
    return FALSE;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
static void
Packit 3ff832
_sleep (guint millisecond)
Packit 3ff832
{
Packit 3ff832
    g_timeout_add (millisecond, (GSourceFunc) _sleep_cb, NULL);
Packit 3ff832
    ibus_main ();
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
/* ibus stress test
Packit 3ff832
   Send random key press and release event message to ibus-daemon.
Packit 3ff832
   Key kind are a-z and space.
Packit 3ff832
   Check ibus-daemon and ibus engine crash.
Packit 3ff832
*/
Packit 3ff832
gint
Packit 3ff832
main (gint argc, gchar **argv)
Packit 3ff832
{
Packit 3ff832
    GTimer *timer;
Packit 3ff832
    GRand *rnd;
Packit 3ff832
    BusTestClient *client;
Packit 3ff832
    /* num of send space key */
Packit 3ff832
    guint32 seed = (guint32) time (NULL);
Packit 3ff832
    int count = 0;
Packit 3ff832
    int send_key_num = 0;
Packit 3ff832
Packit 3ff832
    setlocale (LC_ALL, "");
Packit 3ff832
    ibus_init ();
Packit 3ff832
Packit 3ff832
    /* need to set active engine */
Packit 3ff832
    client = bus_test_client_new ();
Packit 3ff832
    if (client == NULL) {
Packit 3ff832
        g_printerr ("don't create test-client instance.");
Packit 3ff832
        exit(1);
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    timer = g_timer_new ();
Packit 3ff832
    rnd = g_rand_new ();
Packit 3ff832
    g_rand_set_seed (rnd, seed);
Packit 3ff832
    g_print("random seed:%u\n",seed);
Packit 3ff832
    g_timer_start (timer);
Packit 3ff832
Packit 3ff832
    while (1) {
Packit 3ff832
        guint keysym;
Packit 3ff832
        if (send_key_num > MAX_SEND_KEY_NUM) {
Packit 3ff832
            break;
Packit 3ff832
        }
Packit 3ff832
        if (!bus_test_client_is_connected (client)) {
Packit 3ff832
            g_printerr ("ibus-daemon is disconnected\n");
Packit 3ff832
            break;
Packit 3ff832
        }
Packit 3ff832
        if (!bus_test_client_is_enabled (client)) {
Packit 3ff832
            g_printerr ("ibus engine is enabled\n");
Packit 3ff832
            break;
Packit 3ff832
        }
Packit 3ff832
Packit 3ff832
        if(count>0 || g_rand_int_range (rnd, 0, 5) == 0) {
Packit 3ff832
            /* send space key 20% */
Packit 3ff832
            if (count == 0) {
Packit 3ff832
                count = g_rand_int_range (rnd, 0, MAX_RANDOM_SPACE) + 1;
Packit 3ff832
            }
Packit 3ff832
            if (count-- == 1) {
Packit 3ff832
                keysym = IBUS_KEY_Return;
Packit 3ff832
            } else {
Packit 3ff832
                keysym = IBUS_KEY_space;
Packit 3ff832
            }
Packit 3ff832
        } else {
Packit 3ff832
            /* send random a-z key */
Packit 3ff832
            keysym = g_rand_int_range (rnd, 0, 'z'-'a'+1) + 'a';
Packit 3ff832
        }
Packit 3ff832
        bus_test_client_send_key (client, keysym);
Packit 3ff832
        send_key_num += 1;
Packit 3ff832
        /* limit the typing rate to 800 hits/minutes */
Packit 3ff832
        _sleep (1000 * 60 / 800);
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    g_print ("%f sec\n", g_timer_elapsed (timer, NULL));
Packit 3ff832
Packit 3ff832
    return 0;
Packit 3ff832
}