1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#[macro_use]
extern crate async_trait;

use crate::queries::get_conference::{get_conference::Variables, GetConference};
use artemis::{
    exchange::{Client, Exchange, ExchangeFactory, ExchangeResult, Operation, OperationResult},
    ClientBuilder, GraphQLQuery, Response
};
use artemis_normalized_cache::NormalizedCacheExchange;
use rand::Rng;
use std::{
    any::Any,
    sync::{
        atomic::{AtomicU32, Ordering},
        Arc
    },
    thread,
    time::Duration
};
use tokio::runtime::Runtime;

mod queries;

pub(crate) type Long = String;

struct DummyFetchExchange;

impl<TNext: Exchange> ExchangeFactory<TNext> for DummyFetchExchange {
    type Output = DummyFetchExchange;

    fn build(self, _next: TNext) -> DummyFetchExchange {
        Self
    }
}

#[async_trait]
impl Exchange for DummyFetchExchange {
    async fn run<Q: GraphQLQuery, C: Client>(
        &self,
        operation: Operation<Q::Variables>,
        _client: C
    ) -> ExchangeResult<Q::ResponseData> {
        use crate::queries::get_conference::get_conference::{
            GetConferenceConference, ResponseData
        };

        let delay = Duration::from_millis(50);
        tokio::time::delay_for(delay).await;
        let variables: Box<dyn Any> = Box::new(operation.query.variables);
        let variables = (&variables).downcast_ref::<Variables>().unwrap();
        let data = ResponseData {
            conference: Some(GetConferenceConference {
                id: variables.id.clone(),
                city: Some("Test City".to_string()),
                name: "Test Conference".to_string(),
                talks: Some(Vec::new())
            })
        };

        let data: Box<dyn Any> = Box::new(data);
        let data = *data.downcast::<Q::ResponseData>().unwrap();

        let result = OperationResult {
            key: operation.key,
            meta: operation.meta,
            response: Response {
                data: Some(data),
                debug_info: None,
                errors: None
            }
        };

        Ok(result)
    }
}

#[cfg(target_os = "linux")]
fn begin() {
    coz::begin!("query");
}

#[cfg(not(target_os = "linux"))]
fn begin() {}

#[cfg(target_os = "linux")]
fn end() {
    coz::end!("query")
}

#[cfg(not(target_os = "linux"))]
fn end() {}

#[allow(clippy::infinite_iter)]
fn main() {
    let url = "http://localhost:8080/graphql";
    let builder = ClientBuilder::new(url)
        .with_exchange(DummyFetchExchange)
        .with_exchange(NormalizedCacheExchange::new());
    //.with_exchange(DedupExchange);
    let client = Arc::new(builder.build());

    println!("Started");

    let n = 25;
    let variable_set: Vec<Variables> = (0..n).map(|i| Variables { id: i.to_string() }).collect();

    let thread_count = 1;

    let query_count = Arc::new(AtomicU32::new(0));

    for _ in 0..thread_count {
        let client = client.clone();
        let variable_set = variable_set.clone();
        let query_count = query_count.clone();
        let mut runtime = Runtime::new().unwrap();
        thread::spawn(move || loop {
            let futs = (0..100).map(|_| {
                let var_id = rand::thread_rng().gen_range(0, n);
                let variables = variable_set.get(var_id).cloned().unwrap();
                let client = client.clone();
                async move {
                    begin();
                    client.query(GetConference, variables).await.unwrap();
                    end();
                }
            });

            runtime.block_on(futures::future::join_all(futs));
            query_count.fetch_add(100, Ordering::SeqCst);
        });
    }

    thread::spawn(move || {
        let mut seconds = 0;
        loop {
            println!(
                "Query Count: {} at {}s",
                query_count.load(Ordering::SeqCst),
                seconds
            );
            thread::sleep(Duration::from_secs(1));
            seconds += 1;
        }
    })
    .join()
    .unwrap();
}